1 159.234LECTURE 10 159.234 LECTURE 10 Developing the Tank Game Assignment #1 Game Structure Drawing the Tank Firing the bullet Keyboard, mouse Zoom-in,

Slides:



Advertisements
Similar presentations
IS660Z Programming Games Using Visual Basic Overview of Cannonball.
Advertisements

Visual Basic: ballistics
Projectile Motion - Angled Launches Fill in the component and resultant velocities of the thrown projectile as it ascends and descends. Assume that g =
1 Monkey and hunter experiment Body projected horizontally under gravity Body projected horizontally under gravity Body projected at an angle under gravity.
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
Parametric Equations Here are some examples of trigonometric functions used in parametric equations.
1 Flash Actionscript Animation. 2 Introduction to Sprites We will now look at implementing Sprites in Flash. We should know enough after this to create.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Games and Simulations O-O Programming in Java The Walker School
Graphical Tree-Based Scientific Calculator: CalcuWiz Will Ryan Christian Braunlich.
What is Projectile Motion?. Instructional Objectives: Students will be able to: –Define Projectile Motion –Distinguish between the different types of.
1 Getting Started: C-Revisions and Introduction to Graphics Next: Simulation Essentials, Memory Handling.
Scratch the Cat. Object Oriented Programing Writing computer programs Based on Objects Instead of Actions Based on Data Instead of Logic.
Chapter Introduction to Computers and Programming 1.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
MrsBillinghurst. net A2 Computing A2 Computing Projects Game Animation in Pascal.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Lecture 4: More kinematics. Displacement and change in displacement Position vector points from the origin to a location. The displacement vector points.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
GRAPHICS AND MOUSE PROGRAMMING IN C. Turbo C has a good collection of graphics libraries. If you know the basics of C, you can easily learn graphics programming.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Projectile Motion Horizontally Launched Projectiles Projectiles Launched at an Angle A.S – Due Friday, 11/14 Text Reference: chapter 3.
AP Physics.  Think of a curve being traced out over time, sometimes doubling back on itself or crossing itself. Such a curve cannot be described by a.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
Copyright Sautter Motion in Two Dimension - Projectiles Projectile motion involves object that move up or down and right or left simultaneously.
1 Simulation Essentials Overview World System of Coordinates Device System of Coordinates Transformation Equations Projectile Motion Equations Randomization,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Physics Type 2 Projectile Motion Type 3 Projectile Motion
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Computer Graphics Basic Maths for Graphics in C++ CO2409 Computer Graphics Week 4.
Motion in Two Dimensions. (Ignore any effects from air resistance) A pickup is moving with a constant velocity and a hunter is sitting in the back pointing.
Chapter 3: Curvilinear Motion
Two Dimensional Motion Two components: Horizontal (x-axis) & Vertical (y-axis)
Controlling Program Flow with Decision Structures.
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
Advanced loop controls. Loop Controls Recall from last week loop controls Event-controlled loops using sentinels repeats until a control variable takes.
Chapter Projectile Motion 6.1.
Introduction to: Python and OpenSesame FOR PROS. OpenSesame In OpenSesame you can add Python in-line codes which enables complex experiment. We will go.
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009 Intro to Action Script 11 "The games of a people reveal.
Final Project Virtual Slot Machine. By: Bander Alrogi Khaled Al Nuaimi Mohammed Almajhad Metaib Al-Enzai Course Title: ECE 2552 Instructor: Dr Kepuska.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
School of Computer Science Space School 2015 Programming a Lunar Lander Game.
Projectile Motion Chapter 3 Section 3. What is Projectile Motion? Projectile Motion – Motion that is launched into the air that is subject to gravity.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Part 1 Projectiles launched horizontally
Chapter Projectile Motion 6.1.
Computer Graphics Basic Maths for Graphics in C++
MOM! Phineas and Ferb are … Aims:
Scratch for Interactivity
Chapter Projectile Motion 6.1.
Introduction to Events
Projectile Motion.
Chapter 5 - Functions Outline 5.1 Introduction
Motion in Two Directions
Projectile Motion AP Physics C.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Projectile Motion AP Physics C.
Projectile Motion AP Physics C.
Projectile Motion AP Physics C.
Projectile Motion AP Physics C.
CSC 221: Introduction to Programming Fall 2018
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

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.