Basics

Programming Languages

Variables

Functions

Moving Points on the Screen

Vectors and Motion

Storing polygons


Languages

Programming languages are code that is compiled into the "0's" and "1's" that a processor understands. There are many different programming languages such as C,C++,QBasic,VisualBasic,VisualC++,and Java. Beginning programmers should start with QBasic or VisualBasic so they can familiarize themselves with programming. The nice thing about QBasic is that it comes with M-DOS but it does not have a compiler. Without the compiler, the software that you program can only be run if the file is open. Eventually though you should learn C and then C++ because these are usually industry standards. The good thing about C and C++ is that they are low-level programming languages when compared to QBasic. This means that they are faster and can usually do more.

Variables

Variables are numbers that are put into memory and can be changed. You will use variables for anything that is dynamic and not static such as position of moveabke objects. In other words, anything in your program that can change will utilize variables. There are different kinds of variables. There are integers and characters, each of which can be a number without a decimal value. The difference is that an integer can have more values than a character. There are also floating-points which are numbers with decimal values. You can declare arrays, which are a list of variables, with as many dimensions as memory will allow. Strings are arrays that represent letters with numerical values.

Variables can be changed by performing mathematical operations on them such as multiplying or addition. Usually a variable is declared at the beginning of aprogram and a value is given to it or it begins with a value of 0. The only place I have seen this not occur is the simplistic language of the TI-83+ calculator. On the TI-83+ calculator only lists(arrays) have to be declared if they do not exist. In order to change a variable's value, all that has to be done is add, subtract, multiply, or divide by a constant or another variable.

Functions

Functions are like mini-programs within your main program. Almost all languages allow functions. Programming can be done without functions but they make life alot easier. For instance, to raise a number to a certain power, a FOR statement can be used to multiply a variable by itself for so-many times. If your programs use exponents alot, then it can become very tiring to write the FOR statement over and over again. But if you were to put the FOR statement in a function then you wouldn't have to write as much. All you have to do is declare the function and where ever you want an exponent, all you have to do is write a function call. Function calls and declarations can be different forevery language so make sure you learn a language from a realiable source. For instance, again if you look at the TI-83+ calculator, you will find no functions commands. The functions have to be other programs that you call from your main program.

Moving Points on the Screen

Putting points on the screen is fairly simple. Programming languages usually contain commands to draw points on the screen. Moving points requires the use of variables. Two variables may be used, one for the x value and one for the y value. When you make the command to draw the points, the x and y values you supply will be these variables. In order to move the points, addition, subtraction, multiplication, or division operations should be performed on the points x and y values. When the user presses a key, the input is tested to see if it matches a certain one with an IF statement. If it does, then a mathematical operation is performed.

Vectors and Motion

Moving points with addition and subtraction of a constant value may not achiece the realistic movement you want. Instead vectors may be used.

Vectors have a certain length and a certain direction. They are defined as (x1-x2,y1-y2)=(a,b). a can be added to the x value of a point you want to move while b is added to its y value. Vectors are different from adding by constant values because they can be rotated and their magnitude(length) can be altered. The vectors direction is the direction the point will move and the magnitude is the speed at which it will move.

To change the magnitude of a vector without changing direction, all that has to be done is multiply a and b by the same number or . <5a,5b> would result in a larger magnitude. <0.5a,0.5b> would result in a smaller magnitude.

Rotation of a vector by x degrees involves trigonometric functions and so is harder on the processor. the trigonometric functions sine and cosine are utilized. To rotate a vector in the clockwise direction, use a and b in this formula:

a2=a1*cos(x degrees)-b1*sin(x degrees)

b2=b1*cos(x degrees)+a1*sin(x degrees)

If you are wondering where the proof of this formula is just use the given that:

x'=M*cos(A+A')

y'=M*sin(A+A')

A'=arccos(x/M)=arcsin(y/M)

The whole entire purpose of using vectors is to simulate reality. This can be observed in a racing game. Cars do not stop exactly where they are when they hit the brake. Instead, their speed decreases until they stop. This can be simulated by decreasing the magnitude of the vector desribing its direction until it equals 0 or less(The magnitude of a vector= the square root of the qantity of a squared + b squared). Also vectors may be rotated to simulate turning because no car can turn 180 degrees at a time.

Storing Polygons

Polygons are closed shapes with all straight edges such as triangles and rectangles. When storing polygons, they should be stored as an array of points. When the polygon is drawn a line is made between each point including the first and the last. This can be accomplished easily with a FOR statement from the first point to the second to last. A line is drawn imbetween the point the FOR statement is incremented to and the point coming after the incrementation. After the FOR statement has completed its cycle, a line is drawn from the first point to the last.

A problem arises if you want to store a figure made of many polygons because you will have many arrays. First, store a variable i with the value 1 inorder to use it as an index.This can be solved by storing all of the polygons for the figure in one array. The first item(i) in the array contains the number of points in the first polygon. The second item(i+1) contains the x value of the first point and the third item contains the y value of the first point. If n=the number of the point you want then the point you want is indexed in the array at x value=i+2(n-1)+1 and y value=i+2(n-1)+2. You can use a FOR statement from n=1 to n=array_name[i]-1+2n. A line is drawn with endpoints (array_name[i+2(n-1)+1],array_name[i+2(n-1)+2]) and (array_name[i+2n+1],array_name[i+2n+2]). Then when the FOR statement is complete, a line is drawn from the first point to the last point. This line has the coordinates (array_name[i+1],arrayname[i+2]) and (array_name[i+2*(arrayname[i]-1)+1],array_name[i+2*(array_name[i]-1)+2).Then array_name[i+2*(array_name[i]-1)+3] is tested to see if it is 0. If it is 0 then the figure is done drawing. If it is greater than 0 then i=i+2*(array_name[i]-1)+3 and the process repeats itself.

Main