1 Simulation Essentials Overview World System of Coordinates Device System of Coordinates Transformation Equations Projectile Motion Equations Randomization, Timer Keyboard Handling
Demo C:\Core\Massey Papers\159234\Animation-2008-OOP-v.5.0 2
3 PHYSICS PROJECTILE MOTION ter … Time, Initial Velocity, Mass, pull of gravity Air resistance, property of material, etc.
4 Transformation Equations WORLD-to-DEVICE COORDINATES 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 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
6 Transformation 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 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
8 World Boundaries SETTING THE BOUNDARIES where =85 degrees where =45 degrees Time of flight : from take-off to landing
9 World-to-Device Coordinates TRANSFORMATION EQUATIONS Computed using the Physics equation for x
10 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. Use the metric system
11 Projectile Motion Unit Conversion for Theta (degrees-to-radians) = Theta_in_degrees * M_PI/180 Defined in math.h e.g. cos( ), sin( )
12 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)
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
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
Demo C:\Core\Massey Papers\159234\SimpleProjectile 15
16 System of Coordinates EFFECTS OF CHANGING THE BOUNDARIES 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 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));
18 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() );
19 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
20 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
21 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;
22 Keyboard Handling GetAsyncKeyState 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
23 Keyboard Handling GetAsyncKeyState 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
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 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; } …