1 / 13

Game Physics and Projectiles

Game Physics and Projectiles Soon Tee Teoh CS 134 Interactive Systems and Events In a game, the player interacts with the game through input devices: mouse, keyboard, joystick, steering wheel etc. The OS manages user input A user input generates an Interrupt at the hardware level

lotus
Download Presentation

Game Physics and Projectiles

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Game Physics and Projectiles Soon Tee Teoh CS 134

  2. Interactive Systems and Events • In a game, the player interacts with the game through input devices: mouse, keyboard, joystick, steering wheel etc. • The OS manages user input • A user input generates an Interrupt at the hardware level • Interrupt gets converted into event, and put in queue at the OS level • The application controls how to handle this event stream

  3. Game Loop and Events • In a game, we need to run the game loop continuously. • At the same time, we need to receive input from the user, for example, the user has pressed a button, or moved the joystick. • How to handle this? • Several options: • Polling: Periodically query input devices to find out if event has occurred. • Non-blocking: If nothing has happened, then query function exits • Waiting: Wait until event happens • Blocking: Event query function will wait until event happens • The Callback abstraction • This uses the polling method • The program registers some functions to be called whenever an event occurs. • When event occurs, the registered callback function is called to handle the event. • Otherwise, if no event occurs, then the idle function is called to run the game loop. • Alternatively, game loop incorporates event handling while (1) { ProcessEvents(); UpdateAnimation(); Render(); } Game Loop

  4. Game Loop and Physics • The Game Loop is a function that is called continuously. • For example, it can be the following code (same as previous page): • Alternatively, put game loop in idle function, which is called whenever there is nothing else going on (callbacks handle events, so game loop does not): • Thus, a game loop may look like: • How to update the position of an object? • If we know its position and velocity/acceleration etc. and we know the time elapsed since the last game loop, we can update the object’s position. while (1) GameLoop(); void idleFunc() { GameLoop(); } Update position, velocity, acceleration etc. according to physics model. Also, perform collision detection. Perform collision response if necessary. t may be different each time void GameLoop() { float t = TimeSinceLastCall(); for (i=0;i<nCharacters;i++) Characters[i].AI(); for (i=0;i<nObjects;i++) Objects[i].UpdateStatus(t); } need t as argument

  5. Example: Object at Constant Velocity • Suppose in the previous game loop, an object is at position P. • Suppose that the object is traveling at constant velocity V. • Suppose that between the previous game loop and the current game loop, t amount of time has elapsed. • Then, the new position of the object is: P + Vt

  6. Vector and Its Components • A vector has a magnitude and direction. • Example: Force and velocity are vectors. • Often, the vector is in 2D or 3D space, so we express the vector as (x,y) or (x,y,z). • We also express a vector pictorially as an arrow. • Vector addition: • Just add the components • Pictorially, sum the arrows • Splitting vector to components: • For convenience, we often consider a vector in terms of orthogonal components. • For example, we split vector V into Va and Vb. So, V = Va + Vb V = (2,1) V1 + V2 V2 V1 Vb V Va

  7. Projectiles • At the starting point, an object is fired with a certain speed in a certain direction. • After that, there is no force acting on the object except gravity. • Given the initial position and velocity of the object, we wish to calculate its path. We also wish to calculate some other useful numbers.

  8. Projectile Displacement Over Time v0 y q x Suppose that the projectile starts at (0,0). Suppose that the initial velocity of the projectile is v0. X is the horizontal direction, and Y is the vertical direction. Then, the distance in the x and y direction over time t is: x(t) = (v0 cos q) t y(t) = (v0 sin q) t – (gt2)/2 Here, g is the gravitational acceleration.

  9. Projectile Velocity Over Time v0 y q x We can also calculate the velocity over time: Velocity in the x direction Vx(t) = v0 cos q Velocity in the y direction Vy(t) = v0 sin q – gt Overall velocity V(t) = sqrt(v02 – 2gtv0sin q + g2t2)

  10. Projectile Maximum Height v0 y h q x We can also calculate the maximum height gained by the projectile: Maximum height h = (v02 sin2q )/ (2g)

  11. Projectile Time to Impact • To calculate time to impact, we consider three cases: • Case 1: Target is of the same height as projectile launch. • Case 2: Target is higher than projectile launch. • Case 3: Target is lower than projectile launch.

  12. Projectile Time to Impact Case 1: T = (2v0 sin q)/g Case 2: T = (v0 sin q)/g + sqrt(2(h-b)/g) h b Case 3: T = (v0 sin q)/g + sqrt(2(h+b)/g) h b

  13. Projectile Total Horizontal Displacement Total horizontal displacement R = v0 T cos q R h b R h b R

More Related