Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Simulation Essentials Overview World System of Coordinates Device System of Coordinates Transformation Equations Projectile Motion Equations Randomization,

Similar presentations


Presentation on theme: "1 Simulation Essentials Overview World System of Coordinates Device System of Coordinates Transformation Equations Projectile Motion Equations Randomization,"— Presentation transcript:

1 1 Simulation Essentials Overview World System of Coordinates Device System of Coordinates Transformation Equations Projectile Motion Equations Randomization, Timer Keyboard Handling

2 Demo C:\Core\Massey Papers\159234\Animation-2008-OOP-v.5.0 2

3 3 PHYSICS PROJECTILE MOTION 159.234 1 ter … Time, Initial Velocity, Mass, pull of gravity Air resistance, property of material, etc.

4 4 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 +y 0 0 (X world,Y world ) (X Device,Y Device )

5 5 World Boundaries SETTING THE BOUNDARIES 159.234 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

6 6 Transformation 159.234 Given a world coordinate, what’s the equivalent device coordinate? +x +y +x +y 0 0 (X world,Y world ) (X Device,Y Device )

7 7 Projectile Motion Setting the World Boundaries 159.234 +x +y 0 (X world,Y world ) (x1, y1) (x2, y2) x1=0 y1 = maximum_height x2 = maximum_range y2 = 0 ground

8 8 World Boundaries SETTING THE BOUNDARIES 159.234 where  =85 degrees where  =45 degrees Time of flight : from take-off to landing

9 9 World-to-Device Coordinates TRANSFORMATION EQUATIONS 159.234 Computed using the Physics equation for x

10 10 Projectile Motion PHYSICS EQUATIONS 159.234 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. Use the metric system

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

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

13 Coordinate Transformations With the transformation equations, we can now always think in terms of world coordinates Let the Physics equations work it out where the bullet will be at any time t. Before displaying on screen, convert the world coordinate into its equivalent device coordinate. 13

14 Exercise Why do we need the transformation equations? Implement xDev and yDev using functions. What are the appropriate data types for the world and device boundaries? Incorporate error checking in your function implementations See transform.h, transform.cpp from C:\Core\Massey Papers\159234\Bomb-v.10.0 14

15 Demo C:\Core\Massey Papers\159234\SimpleProjectile 15

16 16 System of Coordinates EFFECTS OF CHANGING THE BOUNDARIES 159.234 What happens if we switch the values for DeviceBound x2 and DeviceBound x1? What happens if we double all the maximum values for the WorldBound Coordinates?

17 17 Element 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));

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

19 19 Element of Surprise rand() 159.234 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

20 20 Element 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

21 21 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;

22 22 Keyboard Handling GetAsyncKeyState 159.234 The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState. To find other pre-defined constants: google Using google, type the following keywords: msdn vk_shift Virtual-key code e.g. vk_shift vk_control SHORT GetAsyncKeyState( int vKey ); // vKey - virtual-key code http://msdn.microsoft.com/en-us/library/ms645540(VS.85).aspx

23 23 Keyboard Handling GetAsyncKeyState 159.234 void MoveSprite() { if(GetAsyncKeyState(VK_UP) < 0) { SpriteY = SpriteY - 2; //up outtext("UP"); } if(GetAsyncKeyState(VK_DOWN) < 0) { SpriteY = SpriteY + 2; //down outtext("DOWN"); …. To find other pre-defined constants: Using google, type the following keywords: msdn virtual key codes http://msdn.microsoft.com/en-us/library/ms645540(VS.85).aspx

24 24 Keyboard Handling Monitoring the Control and Shift keys: if(GetAsyncKeyState(VK_CONTROL)<0) { ControlFlag =! ControlFlag; } bool ControlFlag, ShiftFlag; if(GetAsyncKeyState(VK_SHIFT)<0) { 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

25 25 Keyboard Handling Possible approach in monitoring key combinations : if(GetAsyncKeyState(VK_RIGHT)<0) { XDir=RIGHT; if(ShiftFlag) { outtext("SHIFT + RIGHT"); ShiftFlag=!ShiftFlag; } if(ControlFlag) { outtext("CTRL + RIGHT"); if (TankX < getmaxx()-W) TankX += 2; Angle=Angle-5; RaiseWheelFlag=TRUE; ControlFlag=!ControlFlag; } …


Download ppt "1 Simulation Essentials Overview World System of Coordinates Device System of Coordinates Transformation Equations Projectile Motion Equations Randomization,"

Similar presentations


Ads by Google