159'234 Lecture 10 - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

159'234 Lecture 10

Description:

The codes that make the Tank move will have to be assembled ... Bullet & Tank ... However, we need to consider the case when the tank is on top of the ledge. ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 46
Provided by: ecal
Category:
Tags: lecture | tank

less

Transcript and Presenter's Notes

Title: 159'234 Lecture 10


1
159.234 LECTURE 16
Developing the Tank Game
2
Objects in the Game
Why develop this game?
The game is too complex to be written using just
the traditional structured approach. OOP is the
most practical approach in writing this game.
Tank Alien Clouds Bullet Ledge (Assign2)
We would want to develop the game by writing
classes, and creating instances of them to
enable us to keep track of each objects property
isolated from the others.
3
System of Coordinates
Device System of Coordinates
Tank, Alien Clouds movement follow this system
of coordinates.
To move upwards - DispY To move downwards
DispY To move to the right DispX To move
to the left - DispX
Disp stands for Displacement
Use getmaxx(), getmaxy() to find the boundaries
of the current resolution setting
4
System of Coordinates
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
EARTH_Y getmaxy()-(getmaxy()/5)
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.
6
Transformation Equations
159.234
WORLD-to-DEVICE COORDINATES
100,000,000 miles x 500,000 miles
1280 x 1024 pixels
y
0
x
(Xworld,Yworld)
(XDevice,YDevice)
x
y
0
World System of Coordinates
Device System of Coordinates
7
World Boundaries
159.234
SETTING THE BOUNDARIES
Use the upper-left and bottom-right coordinates
to set the boundaries
y
0
x
(Xworld,Yworld)
(XDevice,YDevice)
x
y
0
Top-left x1, y1 Bottom-right x2, y2
8
Projectile Motion
159.234
Setting the World Boundaries
x10 y1 maximum_height x2 maximum_range y2 0
(x1, y1)
y
(Xworld,Yworld)
ground
x
0
(x2, y2)
9
World Boundaries
159.234
SETTING THE BOUNDARIES
where ?85 degrees
where ?45 degrees
Time of flight from take-off to landing
10
World-to-Device Coordinates
159.234
TRANSFORMATION EQUATIONS
Computed using the Physics equation for x
11
Projectile Motion
159.234
PHYSICS EQUATIONS
Increment t in the equations and x and y will be
automatically adjusted.
where g 9.8 m/sec.2 pull of gravity Vo
in m/sec. initial velocity t in sec.
Time ? in radians Launching Angle
12
Projectile Motion
159.234
Unit Conversion for Theta (degrees-to-radians)
? Theta_in_degrees M_PI/180
Defined in math.h
e.g. cos(?), sin(?)
13
Projectile Motion
159.234
PUTTING THE PIECES TOGETHER
circle(x, y, radius)
// InitGraphics here // InitBoundaries
here t0.0 while(t lt tf)
cleardevice() setcolor(RED) circle
(Xdev( x(t, Vo, Theta) ), Ydev( y(t, Vo, Theta)),
12) tttinc
World-to-Device Transformation Function
Physics Equation for x
14
Bullet
Converting the code into its object-oriented
representation
class Bullet public private
Physics Equations Transformation
Equations Drawing the Bullet Moving the
Bullet Checking for Collision can be handled
outside the Bullet class
P
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).
15
Bullet
Converting the code into its object-oriented
representation
class Bullet public private
Physics Equations Transformation
Equations Drawing the Bullet Moving the
Bullet Checking for Collision
Q
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?
Q
Q
What controls the speed of the bullet animation?
16
Bullet Tank
The bullet should be released where the Tanks
nozzle points to, and follow the appropriate
trajectory (whether to move to the left, or
right).
How can this be done?
17
Releasing the bullet
CASES TO CONSIDER
Note the tanks nozzle can be directed by the
user to any angle (e.g. 0, 170)
18
Setting the boundaries for the Bullet
Q
How to account for all the cases we have seen?
A
The tricks are
  • Set the world boundaries to be fixed, covering
    the whole region
  • where the bullet is allowed to move.

WorldBound.X1 0 WorldBound.Y1
maximum_height WorldBound.X2
maximum_range WorldBound.Y2 0
19
Setting the boundaries for the Bullet
A
  • Next, set the bullets device components (x1,y2)
    to point to the
  • location of the tanks nozzle.

DeviceBound.X1 TankNozzleX DeviceBound.Y2
TankNozzleY DeviceBound.Y1 0 DeviceBound.X2
getmaxx()
These changes will not give us a perfect
projectile motion, but for the tanks firing
mechanism, effects would suffice.
20
From nozzle to ground
t0
from t0 to TimeOfFlight
TTimeOfFlight
While BulletY is less than EARTH_Y, let the
bullet fall (tttinc)
EARTH_Y
The changes would guarantee that the complete
trajectory would be plotted on screen (from t0
to TimeOfFlight). The only thing left would be
to account for the motion of the projectile until
it hits the ground (EARTH_Y).
21
From nozzle to ground
(x1, y1)
t0
from t0 to TimeOfFlight
TTimeOfFlight
(x1, y1)
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.
22
Jumping over the Ledge
Safe landing zone
(cx,cy) tanks center of gravity
Check to see if the tanks center position falls
within the safe landing zone of the ledge.
23
Jumping over the Ledge
Safe landing zone
(cx,cy) tanks center of gravity
Q
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.
Flag variables will have to be created to monitor
these cases.
24
Jumping over the Ledge
Safe landing zone
Y
(cx,cy) tanks center of gravity
Q
How do we know that the tank is falling?
If its PrevY position is less than its CurrentY
position, or If its (cx, cy) position goes out
of the safe landing zone area (fell-off the
ledge by accident!).
Flag variables will have to be created to monitor
these cases.
25
Jumping over the Ledge
Safe landing zone
Y
(cx,cy) tanks center of gravity
Q
When do we allow a Tank to jump?
If its not on the air yet!
If its currently on top of a ledge, or if its
resting on the ground.
Flag variables will have to be created to monitor
these cases.
26
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.
(CtrlShiftLeft) to jump left, or
(CtrlShiftRight) to jump right The tank
should be coming from the ground or off the
ledge, in order to jump.
27
Class Ledge
The Ledge is a flying ledge that can carry the
Tank.
class Ledge 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 Tanks x y positions,
as well as compute the Tanks new center of
gravity) the Tank, once it has landed safely on
top of it.

28
Friend Functions (for Tank Ledge)

This is just one possible approach for solving
the Tank jump problem
CheckPlatform friend function
//tells the tank if its inside the safe
zone //tells the Tank if it can jump or
not //tells the Tank if it fell-off the ledge
accidentally
29
Class Ledge
The Tank does not know whether or not it has
landed on top of the Ledge.
class Tank public friend class Ledge friend
void CheckPlatform(Tank tank, Ledge
ledge) private
Drawing the Tank Moving the Tank CheckPlatform
friend function
30
Class 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.
Launching Angle of Bullet Theta from Tank
Vo Vo from
Tank
31
Class Tank
Tanks Nozzle Angle vs. Bullets Launching Angle
  • Initially,
  • NozzleAngle Angle as defined by user 90
    //45-90
  • Theta Angle as defined by user //45
  • Next, every time the Tanks 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

32
Class Tank
NozzleAngle Angle from user 90 //e.g.
45-90-45 NozzleLength 25 //some constant value
(NozzleX, NozzleY)
(Tx,Ty)
Angle from user (e.g. 45 degrees)
NozzleX Tx NozzleLengthcos((NozzleAngle)
M_PI / 180.0)
NozzleY Ty NozzleLengthsin((NozzleAngle)
M_PI / 180.0)
Take note The Nozzle position is calculated
relative to the Tanks body. Therefore, when the
tanks body moves at some angle, the nozzle moves
as well.
33
Class Tank
Tank during take-off
When in-flight
Nozzle moves with the Tanks body!
Making the Tank to appear to be inclined would
require some trigonometric formulas.
34
Class Tank
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
Top-left
bottom-right
bottom-left
(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.
The rest of the other coordinates can be
calculated relative to this point.
35
Facing to the Right
Top-right
Top-left
bottom-right
bottom-left
(TankX,TankY)
Top-right X TankXWcos((Angle)M_PI/180))
e.g. Angle-5
Top-right Y TankY-HWsin((Angle)M_PI/180))
36
Facing to the Right
Top-right
Top-left
bottom-right
bottom-left
(TankX,TankY)
Bottom-right X TankXWcos((Angle)M_PI/180))

(Hsin(-AngleM_PI/180))
e.g. Angle-5
Bottom-right Y TankYWsin((Angle)M_PI/180))
37
Facing to the Right
Top-right
Top-left
bottom-right
bottom-left
(TankX,TankY)
Top-left X TankX
Top-left Y TankY-H
38
Facing to the Right
Top-right
Top-left
bottom-right
bottom-left
(TankX,TankY)
Bottom-left X TankX (Hsin(-AngleM_PI/180))
e.g. Angle-5
Bottom-left Y TankY
39
Facing to the Left
Top-left
Top-right
bottom-left
bottom-right
(TankX,TankY)
Bottom-left X TankX-(Hsin(-AngleM_PI/180))
or
e.g. Angle-5
Bottom-left X TankX(Hsin(AngleM_PI/180))
Bottom-left Y TankY(Wsin(AngleM_PI/180))
40
Facing to the Left
Top-left
Top-right
bottom-left
bottom-right
(TankX,TankY)
Bottom-right X (TankXW)-(Hsin(-AngleM_PI/180)
)
e.g. Angle-5
Bottom-right Y TankY
41
Facing to the Left
Top-left
Top-right
bottom-left
bottom-right
(TankX,TankY)
Top-left X TankX(Hsin(-AngleM_PI/180))
e.g. Angle-5
Top-left Y TankY-HWsin(AngleM_PI/180)
42
Facing to the Left
Top-left
Top-right
bottom-left
bottom-right
(TankX,TankY)
Top-right X TankXW
e.g. Angle-5
Top-right Y TankY-H
43
Keyboard Handling
Monitoring the Control and Shift keys
bool ControlFlag, ShiftFlag
if(GetAsyncKeyState(VK_CONTROL)lt0)
ControlFlag ! ControlFlag
if(GetAsyncKeyState(VK_SHIFT)lt0)
ShiftFlag ! ShiftFlag
For the Tank to Jump to the Right Control
Shift Right Arrow key
For the Tank to Jump to the Left Control
Shift Left Arrow key
44
Keyboard Handling
Possible approach in monitoring key combinations
if(GetAsyncKeyState(VK_RIGHT)lt0)
XDirRIGHT if(ShiftFlag)
outtext("SHIFT RIGHT")
ShiftFlag!ShiftFlag
if(ControlFlag)
outtext("CTRL RIGHT") if (TankX lt
getmaxx()-W) TankX 2
AngleAngle-5 RaiseWheelFlagTRUE
ControlFlag!ControlFlag

45
Creating Instances of our Objects
Lets see how the main function would look like
if we design our classes in the way described in
the slides What benefits can we actually get?
Write a Comment
User Comments (0)
About PowerShow.com