LECTURE LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in, zoom-out
2 Objects in the Game Tank Alien Clouds Bullet Ledge We would want to develop the game by writing classes, and creating instances of them to enable us to keep track of each object’s property isolated from the others. The game is too complex to be written using just the traditional structured approach. OOP is the most practical approach in writing this game. Why develop this game?
3 System of Coordinates Tank, Alien & Clouds movement follow this system of coordinates. World System of Coordinates +x +y 0 To move upwards: + DispY To move downwards: - DispY To move to the right: + DispX To move to the left: - DispX All Physics equations work with this system of coordinates. Disp – stands for Displacement
4 System of Coordinates EARTH_Y: For example: 100 To respond to Left/Right movement commands from the user, simple statements such as the following would suffice: Left: if(Boundary checking here…) TankX –= 2; Right: if(Boundary checking here…) TankX += 2; +x +y 0
5 System of Coordinates The Bullet object is plotted using Physics equations and transformation Equations. TransformationX(PhysicsX(t,..)) t – dictates the next position of the bullet. +x +y 0
6 Transformation Equations WORLD-to-DEVICE COORDINATES x 1024 pixels 100,000,000 miles x 500,000 miles World System of CoordinatesDevice System of Coordinates +x +y +x +y 0 0 (X world,Y world ) (X Device,Y Device )
7 World Boundaries SETTING THE BOUNDARIES Use the upper-left and bottom-right coordinates to set the boundaries +x +y +x +y 0 0 (X world,Y world ) (X Device,Y Device ) Top-left: x1, y1 Bottom-right: x2, y2
8 Projectile Motion Setting the World Boundaries x +y 0 (X world,Y world ) (x1, y1) (x2, y2) x1=0 y1 = maximum_height x2 = maximum_range y2 = 0 ground
9 System of Coordinates EFFECTS OF CHANGING THE BOUNDARIES What happens if we double all the maximum values for the WorldBound Coordinates? What happens if we decrease the maximum values for the WorldBound Coordinates?
10 World Boundaries SETTING THE BOUNDARIES where =85 degrees where =45 degrees Time of flight : from take-off to landing
11 World-to-Device Coordinates TRANSFORMATION EQUATIONS Computed using the Physics equation for x
12 Projectile Motion PHYSICS EQUATIONS where g = 9.8 m/sec. 2 pull of gravity Vo in m/sec. initial velocity t in sec. Time in radians Launching Angle Increment t in the equations and x and y will be automatically adjusted.
13 Projectile Motion Unit Conversion for Theta (degrees-to-radians) = Theta_in_degrees * M_PI/180 Defined in math.h e.g. cos( ), sin( )
14 Projectile Motion PUTTING THE PIECES TOGETHER // InitGraphics here // InitBoundaries here t=0.0; while(t < tf) { cleardevice(); setcolor(RED); circle (Xdev( x(t, Vo, Theta) ), Ydev( y(t, Vo, Theta)), 12); t=t+tinc; } Physics Equation for x World-to-Device Transformation Function circle(x, y, radius)
15Bullet classBullet { public: private }; Physics Equations Transformation Equations Drawing the Bullet Moving the Bullet Checking for Collision – can be handled outside the Bullet class, but this will depend on your class design. The codes that make the Tank move will have to be assembled together inside a function, rather than being dispersed inside a while loop (as seen earlier). Converting the code into its object-oriented representation: P
16Bullet classBullet { public: private }; Physics Equations Transformation Equations Drawing the Bullet Moving the Bullet Checking for Collision How to synchronize the movement of the tank and the bullet? How to make the bullet follow the path of a trajectory moving to the right/left? What controls the speed of the bullet animation? Converting the code into its object-oriented representation: Q Q Q
17 Bullet & Tank The bullet should be released where the Tank’s nozzle points to, and follow the appropriate trajectory (whether to move to the left, or right). How can this be done?
18 Releasing the bullet CASES TO CONSIDER Note: the tank’s nozzle can be directed by the user to any angle (e.g. [0, 170])
19 Setting the boundaries for the Bullet Next, set the bullet’s world components (x, y) to point to the location of the tank’s nozzle. x = tankNozzleX; y = tankNozzleY; The time of flight will have to be calculated using the formula that considers a Target lower than the launch point. A
20 Target lower than the launch point
21 Class Tank Launching Angle of Bullet = Theta from Tank Vo = Vo from Tank You should monitor the current Vo and Theta set by the user for the Tank, and pass these parameters to the Bullet class prior to releasing the bullet.
22 Class Tank Initially, NozzleAngle = Angle as defined by user – 90; //45-90 Theta = Angle as defined by user; //45 Next, every time the Tank’s NozzleAngle is changed (through user keyboard interaction), we perform the following updates: To move the NozzleAngle in a clock-wise direction: NozzleAngle = NozzleAngle +2 Theta = Theta -2 To move the NozzleAngle in a counter clock-wise direction: NozzleAngle = NozzleAngle -2 Theta = Theta +2 Tank’s Nozzle Angle vs. Bullet’s Launching Angle
23 Class Tank NozzleAngle = Angle NozzleLength = 25 //some constant value Angle from user (e.g. 45 degrees) (NozzleX, NozzleY) (Tx,Ty) NozzleX = Tx + NozzleLength*cos((NozzleAngle) * M_PI / 180.0) NozzleY = Ty + NozzleLength*sin((NozzleAngle) * M_PI / 180.0) Take note: The Nozzle position is calculated relative to the Tank’s body. Therefore, when the tank’s body moves at some angle, the nozzle moves as well.
24 Class Tank Tank during take-off When in-flight Making the Tank to appear to be inclined would require some trigonometric formulas. Nozzle moves with the Tank’s body!
25 Class Tank (TankX,TankY) In so doing, only TankX and TankY needs to be updated every time the user instructs the tank to move via keyboard control. TankXTankY For easier handling, you may want to compute all the coordinates of the tank figure based on just a single point in the Tank (e.g. TankX, TankY). Top-right bottom-right Top-left bottom-left The rest of the other coordinates can be calculated relative to this point.
26 Facing to the Right Top-right X = TankX+W*cos((Angle)*M_PI/180)); (TankX,TankY) Top-right bottom-right Top-left bottom-left Top-right Y = TankY+H+W*sin((Angle)*M_PI/180)); e.g. Angle=5 //top-right tankBody[2]=Xdev(WBound,DBound,(tankX+W*cos((angle) * M_PI / 180.0))); tankBody[3]=Ydev(WBound,DBound,(tankY+H+W*sin((angle) * M_PI / 180.0)));
27 Facing to the Right Bottom-right X = TankX+W*cos((Angle)*M_PI/180)) + (H*sin(Angle*M_PI/180)); (TankX,TankY) Top-right bottom-right Top-left bottom-left Bottom-right Y = TankY+W*sin((Angle)*M_PI/180)); e.g. Angle=5 //bottom-right tankBody[4]=Xdev(WBound,DBound,(tankX+W*cos((angle) * M_PI / 180.0)) + (H*sin(angle*M_PI/180.0))); tankBody[5]=Ydev(WBound,DBound,(tankY+W*sin((angle) * M_PI / 180.0)));
28 Facing to the Right Top-left X = TankX; (TankX,TankY) Top-right bottom-right Top-left bottom-left Top-left Y = TankY+H; tankBody[0]=Xdev(WBound,DBound,tankX); tankBody[1]=Ydev(WBound,DBound,tankY+H);
29 Facing to the Right Bottom-left X = TankX + (H*sin(Angle*M_PI/180)); (TankX,TankY) Top-right bottom-right Top-left bottom-left Bottom-left Y = TankY; e.g. Angle=5 //bottom-left tankBody[6]=Xdev(WBound,DBound,tankX+(H*sin(angle * M_PI/180.0))); tankBody[7]=Ydev(WBound,DBound,tankY);
30 Facing to the Left (TankX,TankY) Top-right bottom-right Top-left bottom-left
31 Facing to the Left Top-right bottom-right Top-left bottom-left (TankX,TankY)
32 Facing to the Left Top-right bottom-right Top-left bottom-left (TankX,TankY)
33 Facing to the Left Top-right bottom-right Top-left bottom-left (TankX,TankY)
34 Fillpoly void fillpoly (int numpoints, int *polypoints); numpoints polypoints x-y- coordinates fillpoly draws the outline of a polygon with numpoints points in the current line style and color (just as drawpoly does), then fills the polygon using the current fill pattern and fill color. polypoints points to a sequence of (numpoints * 2) integers. Each pair of integers gives the x- and y- coordinates of a point on the polygon.
35 Fillpoly Example void fillpoly (int numpoints, int *polypoints); float W=35, H=15; int tankBody[8]; //top-left tankBody[0]=Xdev(tankX); tankBody[1]=Ydev(tankY+H); //top-right tankBody[2]=Xdev((tankX+W*cos((angle) * M_PI / 180.0))); tankBody[3]=Ydev((tankY+H+W*sin((angle) * M_PI / 180.0))); //bottom-right tankBody[4]=Xdev((tankX+W*cos((angle) * M_PI / 180.0)) + (H*sin(angle*M_PI/180.0))); tankBody[5]=Ydev((tankY+W*sin((angle) * M_PI / 180.0))); //bottom-left tankBody[6]=Xdev(tankX+(H*sin(angle * M_PI/180.0))); tankBody[7]=Ydev(tankY); setcolor(YELLOW); setfillstyle(SOLID_FILL,RED); fillpoly(4,tankBody);
36 Pieslice void pieslice (int x, int y, int stangle, int endangle, int radius); Use with setcolor() and setfillstyle() functions
37 void drawWheel(float x, float y, float r, int a, directionType xDir) { a = a % 360; setcolor(BLACK); circle(Xdev(x),Ydev(y),Xdev(r)); setfillstyle(SOLID_FILL,RED); setlinestyle(SOLID_LINE,0,1); pieslice(Xdev(x),Ydev(y),a,a+30,Xdev(r)); setfillstyle(SOLID_FILL,GREEN); pieslice(Xdev(x),Ydev(y),a+180,a ,Xdev(r)); }
38 Keyboard Handling Monitoring the Control and Shift keys: VK_CONTROL if(GetAsyncKeyState(VK_CONTROL)<0) { ControlFlag = ! ControlFlag; } bool ControlFlag, ShiftFlag; VK_SHIFT if(GetAsyncKeyState(VK_SHIFT)<0) { ShiftFlag = ! ShiftFlag; } Control + Shift + Right Arrow For the Tank to Jump to the Right: Control + Shift + Right Arrow key Control + Shift + Left Arrow For the Tank to Jump to the Left: Control + Shift + Left Arrow key
39 Keyboard Handling Possible approach in monitoring key combinations : VK_RIGHT if(GetAsyncKeyState(VK_RIGHT)<0) { XDir=RIGHT; ShiftFlag if(ShiftFlag) { outtext("SHIFT + RIGHT"); ShiftFlag=!ShiftFlag; } ControlFlag if(ControlFlag) { outtext("CTRL + RIGHT"); if (TankX < getmaxx()-W) TankX += 2; Angle=Angle-5; RaiseWheelFlag=TRUE; ControlFlag=!ControlFlag; } …
40 Element of Surprise rand() generates a pseudorandom number - returns int int RandomVal(int min, int max) { return (min + (rand() % ((max-min)+1) )); } srand(time(NULL));
41 Element of Surprise srand() Seed for random-number generation Seed the random-number generator with current time so that the numbers will be different every time we run. srand( (unsigned)time( NULL ) ); /* Display 10 numbers. */ for( i = 0; i < 10;i++ ) printf( " %6d\n", rand() );
42 Element of Surprise rand() generates a pseudorandom number - returns int int RandomVal(int min, int max) { return (min + (rand() % ((max-min)+1) )); } rand() rand() returns a pseudo-random integral number in the range (0 to RAND_MAX-1)
43 Element of Surprise rand() float RandomVal(float min, float max) { float r; r = (float)rand()/RAND_MAX; r = min + (r*(max-min)); return r; } rand() rand() returns a pseudo-random integral number in the range (0 to RAND_MAX)-1
44 Time elapsed, wait… clock() void wait ( int seconds ) { clock_t clock_t endwait; endwait = clock () + seconds * CLOCKS_PER_SEC ; while (clock() < endwait) {} } clock_t clock_t startTime, elapsedTime; startTime = clock(); …... elapsedTime = (clock() - startTime)/CLOCKS_PER_SEC;
Assign #2 45
46 From nozzle to ground The changes would guarantee that the complete trajectory would be plotted on screen (from t=0 to TimeOfFlight). The only thing left would be to account for the motion of the projectile until it hits the ground (EARTH_Y). from t=0 to TimeOfFlight While BulletY is less than EARTH_Y, let the bullet fall (t=t+tinc) EARTH_Y T=TimeOfFlight t=0
47 From nozzle to ground The TimeOfFlight is computed only from the point of release to the same y location. However, we need to consider the case when the tank is on top of the ledge. from t=0 to TimeOfFlight (x1, y1) T=TimeOfFlight t=0
48 Jumping over the Ledge (cx,cy) – tank’s center of gravity Safe landing zone Check to see if the tank’s center position falls within the safe landing zone of the ledge.
49 Jumping over the Ledge (cx,cy) – tank’s center of gravity Safe landing zone But when can the tank possibly land on the ledge? When it is falling! It could have jumped off another ledge, or jumped from the ground. Q Flag variables will have to be created to monitor these cases.
50 Jumping over the Ledge (cx,cy) – tank’s center of gravity Safe landing zone How do we know that the tank is falling? If its PrevY position is less than its CurrentY position, or If it’s (cx, cy) position goes out of the safe landing zone area (fell-off the ledge by accident!). +Y Q Flag variables will have to be created to monitor these cases.
51 Jumping over the Ledge (cx,cy) – tank’s center of gravity Safe landing zone When do we allow a Tank to jump? on top of a ledgeresting on the ground If it’s currently on top of a ledge, or if it’s resting on the ground. +Y Flag variables will have to be created to monitor these cases. Q If it’s not on the air yet!
52 Jumping over the Ledge * You will have to check if the Tank can actually jump first, before allowing it to jump on screen. So even if the user presses the key combination that instructs the Tank to jump e.g. (Ctrl+Shift+Left) to jump left, or (Ctrl+Shift+Right) to jump right The tank should be coming from the ground or off the ledge, in order to jump.
53 Class Ledge classLedge { public: friend void CheckPlatform(Tank &tank, Ledge &ledge) private }; Drawing the Ledge Moving the Ledge CheckPlatform – friend function The ledge class can be made to be a friend of class Tank. In turn, the ledge can carry (automatically adjust the Tank’s x & y positions, as well as compute the Tank’s new center of gravity) the Tank, once it has landed safely on top of it. The Ledge is a flying ledge that can carry the Tank. *
54 Friend Functions (for Tank & Ledge) CheckPlatform – friend function This is just one possible approach for solving the ‘Tank jump’ problem: * //tells the tank if it fell-off the ledge or if it’s still inside the safe zone //tells the Tank if it can jump or not //tells the Tank if it fell-off the ledge accidentally
55 Class Ledge classTank { public: friend class Ledge; friend void CheckPlatform(Tank &tank, Ledge &ledge); private: }; Drawing the Tank Moving the Tank CheckPlatform – friend function The Tank does not know whether or not it has landed on top of the Ledge.
56 Common Pitfalls Cannot click simulation window
57 Common Pitfalls Isolation of keyboard handling from Class Tank Multiple key press signals are sent with one key press //if(GetAsyncKeyState(VK_SPACE)<0) //continuous detection of the same key press //if(GetAsyncKeyState(VK_SPACE) & 0x8000 == 0x8000) if(GetAsyncKeyState(VK_SPACE) & 0x0001 == 0x0001)
58 Common Pitfalls Isolation of Transform functions
59 Common Pitfalls Cannot find the function move()
60 Common Pitfalls Which one to use? Rock rock; Rock* rock; Rock* rock[8]; Rock** rock;
61 Common Pitfalls Some extra “blank space” is read when reading information from a file. Program crashes when reading a text file, but no syntax error is reported by the compiler.