1 159.234lecture 10 159.234 lecture 10 developing the tank game assignment #1 game structure drawing...

Post on 02-Jan-2016

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

159.234159.234 LECTURE 10LECTURE 10

Developing the Tank GameDeveloping the Tank Game

• Assignment #1 • Game Structure• Drawing the Tank•Firing the bullet• Keyboard, mouse• Zoom-in, zoom-out

2

Objects in the GameObjects in the Game

TankAlienCloudsBulletLedge

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 structuredapproach. OOP is the most practical approach in writing this game.

Why develop this game?

3

System of CoordinatesSystem of Coordinates

Tank, Alien & Clouds movementfollow this system of coordinates.

World System of Coordinates

+x

+y

0

To move upwards: + DispYTo move downwards: - DispYTo move to the right: + DispXTo move to the left: - DispX

All Physics equations work with this system of coordinates.

Disp – stands for DisplacementDisp – stands for Displacement

4

System of CoordinatesSystem of Coordinates

EARTH_Y:

For example: 100

To respond to Left/Right movementcommands from the user, simple statements such as the following wouldsuffice:

Left: if(Boundary checking here…) TankX –= 2;

Right: if(Boundary checking here…) TankX += 2;

+x

+y

0

5

System of CoordinatesSystem of Coordinates

The Bullet object is plotted using Physics equations and transformationEquations.

TransformationX(PhysicsX(t,..))

t – dictates the next position of the bullet.

+x

+y

0

6

Transformation Equations

WORLD-to-DEVICE COORDINATES

159.234

1280 x 1024 pixels

100,000,000 miles x 500,000 miles

World System of Coordinates Device System of Coordinates

+x

+y+x

+y0

0

(Xworld,Yworld)(XDevice,YDevice)

7

World BoundariesSETTING THE BOUNDARIES

159.234

Use the upper-left and bottom-right coordinates to set the boundaries

+x

+y+x

+y0

0

(Xworld,Yworld)(XDevice,YDevice)

Top-left: x1, y1Bottom-right: x2, y2

8

Projectile Motion

Setting the World Boundaries

159.234

+x

+y

0

(Xworld,Yworld)

(x1, y1)

(x2, y2)

x1=0y1 = maximum_heightx2 = maximum_rangey2 = 0

ground

9

System of Coordinates

EFFECTS OF CHANGING THE BOUNDARIES

159.234

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

159.234

gVoHeight

2

sin*2

max

where =85 degrees

gVo

2sinDistance Horizontal 2

max where =45 degrees

g

Vot flight

sin**2 Time of flight : from

take-off to landing

11

World-to-Device Coordinates

TRANSFORMATION EQUATIONS

159.234

12

12

XX

XX

WorldBoundWorldBound

dDeviceBoundDeviceBounXslope

11*tx_intercep XX WorldBoundXslopedDeviceBoun

tx_intercepWorld* XX XslopeDevice

Computed using the Physics equation for x

12

Projectile Motion

PHYSICS EQUATIONS

159.234

where g = 9.8 m/sec.2 pull of gravity

Vo in m/sec. initial velocityt in sec. Time in radians Launching Angle

tVox *cos*

tVogty *sin*2

1 2

Increment t in the equations and x and y will be automatically adjusted.

13

Projectile Motion

Unit Conversion for Theta (degrees-to-radians)

159.234

= Theta_in_degrees * M_PI/180

Defined in math.h

e.g. cos(), sin()

14

Projectile Motion

PUTTING THE PIECES TOGETHER

159.234

// 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)

15

BulletBullet

class Bullet {public:

private };

Physics EquationsTransformation EquationsDrawing the BulletMoving the BulletChecking 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:

PP

16

BulletBullet

class Bullet {public:

private };

Physics EquationsTransformation EquationsDrawing the BulletMoving the BulletChecking 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:

QQ

QQ

QQ

17

Bullet & TankBullet & 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 bulletReleasing the bulletCASES 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 BulletSetting 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.

AA

20

Target lower than the launch point

21

Class TankClass 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 TankClass 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 userkeyboard 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 -2Theta = Theta +2

Tank’s Nozzle Angle vs. Bullet’s Launching AngleTank’s Nozzle Angle vs. Bullet’s Launching Angle

23

Class TankClass 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 TankClass Tank

Tank during take-offTank during take-off

When in-flightWhen in-flight

Making the Tank to appear to be inclined would Making the Tank to appear to be inclined would require some trigonometric formulas.require some trigonometric formulas.

Nozzle moves with the Tank’s body!

25

Class TankClass Tank

(TankX,TankY)(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.

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. TankXTankX, TankYTankY).

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 RightFacing to the Right

Top-right X = TankX+W*cos((Angle)*M_PI/180));

(TankX,TankY)(TankX,TankY)

Top-right

bottom-right

Top-left

bottom-left

Top-right Y = TankY+H+W*sin((Angle)*M_PI/180));e.g. Angle=5e.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 RightFacing to the Right

Bottom-right X = TankX+W*cos((Angle)*M_PI/180)) + (H*sin(Angle*M_PI/180));

(TankX,TankY)(TankX,TankY)

Top-right

bottom-right

Top-left

bottom-left

Bottom-right Y = TankY+W*sin((Angle)*M_PI/180));

e.g. Angle=5e.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 RightFacing to the Right

Top-left X = TankX;

(TankX,TankY)(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 RightFacing to the Right

Bottom-left X = TankX + (H*sin(Angle*M_PI/180));

(TankX,TankY)(TankX,TankY)

Top-right

bottom-right

Top-left

bottom-left

Bottom-left Y = TankY;e.g. Angle=5e.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 LeftFacing to the Left

(TankX,TankY)(TankX,TankY)

Top-right

bottom-right

Top-left

bottom-left

31

Facing to the LeftFacing to the Left

Top-right

bottom-right

Top-left

bottom-left

(TankX,TankY)(TankX,TankY)

32

Facing to the LeftFacing to the Left

Top-right

bottom-right

Top-left

bottom-left

(TankX,TankY)(TankX,TankY)

33

Facing to the LeftFacing to the Left

Top-right

bottom-right

Top-left

bottom-left

(TankX,TankY)(TankX,TankY)

34

Fillpolyvoid fillpoly (int numpoints, int *polypoints);

159.234

fillpoly draws the outline of a polygon with numpointsnumpoints points in the current line style and color (just as drawpoly does), then fills the polygon using the current fill pattern and fill color. polypointspolypoints points to a sequence of (numpoints * 2) integers. Each pair of integers gives the x-x- and y-y-coordinatescoordinates of a point on the polygon.

35

Fillpoly Examplevoid fillpoly (int numpoints, int *polypoints);

159.234

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

Pieslicevoid pieslice (int x, int y, int stangle, int endangle, int radius);

159.234

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+30+180,Xdev(r));}

38

Keyboard HandlingKeyboard HandlingMonitoring the Control and Shift keys:

if(GetAsyncKeyState(VK_CONTROLVK_CONTROL)<0) { ControlFlag = ! ControlFlag; }

bool ControlFlag, ShiftFlag;

if(GetAsyncKeyState(VK_SHIFTVK_SHIFT)<0) { ShiftFlag = ! ShiftFlag; }

For the Tank to Jump to the Right: Control + Shift + Right Arrow Control + Shift + Right Arrow key

For the Tank to Jump to the Left: Control + Shift + Left Arrow Control + Shift + Left Arrow key

39

Keyboard HandlingKeyboard HandlingPossible approach in monitoring key combinations :

if(GetAsyncKeyState(VK_RIGHTVK_RIGHT)<0) {

XDir=RIGHT; if(ShiftFlagShiftFlag) {

outtext("SHIFT + RIGHT"); ShiftFlag=!ShiftFlag;ShiftFlag=!ShiftFlag; } if(ControlFlagControlFlag)

{ outtext("CTRL + RIGHT"); if (TankX < getmaxx()-W) TankX += 2; Angle=Angle-5; RaiseWheelFlag=TRUE; ControlFlag=!ControlFlag;ControlFlag=!ControlFlag; }

40

Element of SurpriseElement of Surprise

rand()

159.234

generates a pseudorandom number - returns int

int RandomVal(int min, int max){

return (min + (rand() % ((max-min)+1) ));}

srand(time(NULL));

41

Element of SurpriseElement of Surprise

srand()

159.234

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() );

43

Element of SurpriseElement of Surprise

rand()

159.234

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()

159.234

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 groundFrom 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 groundFrom 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)

(x1, y1)

T=TimeOfFlight

t=0

48

Jumping over the LedgeJumping 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 landingzone of the ledge.

49

Jumping over the LedgeJumping 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.

QQ

Flag variables will have to be created to monitor these cases.

50

Jumping over the LedgeJumping over the Ledge

(cx,cy) – tank’s center of gravity

Safe landing zone

How do we know that the tank is falling?How do we know that the tank is falling?

If its PrevY position is less than its CurrentY position, orIf it’s (cx, cy) position goes out of the safe landing zone area (fell-off the ledge by accident!).

+Y

QQ

Flag variables will have to be created to monitor these cases.

51

Jumping over the LedgeJumping over the Ledge

(cx,cy) – tank’s center of gravity

Safe landing zone

When do we allow a Tank to jump?When do we allow a Tank to jump?

If it’s currently on top of a ledgeon top of a ledge, or if it’s resting on the groundresting on the ground.

+Y+Y

Flag variables will have to be created to monitor these cases.

QQIf it’s not on the air yet!

52

Jumping over the LedgeJumping over the Ledge

** You will have to check if the Tank can actually jump first, beforeallowing 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, inorder to jump.

53

Class LedgeClass Ledge

class Ledge {public:friend void CheckPlatform(Tank &tank, Ledge &ledge)private };

Drawing the LedgeMoving the LedgeCheckPlatform – 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’sx & 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)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 LedgeClass Ledge

class Tank {public:friend class Ledge;friend void CheckPlatform(Tank &tank, Ledge &ledge);private: };

Drawing the TankMoving the TankCheckPlatform – friend function

The Tank does not know whether or not it has landed on top of the Ledge.

56

Common PitfallsCommon Pitfalls

Cannot click simulation window

57

Common PitfallsCommon 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 PitfallsCommon Pitfalls

Isolation of Transform functions

59

Common PitfallsCommon Pitfalls

Cannot find the function move()

60

Common PitfallsCommon Pitfalls

Which one to use?

Rock rock;

Rock* rock;

Rock* rock[8];

Rock** rock;

61

Common PitfallsCommon 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.

top related