home *** CD-ROM | disk | FTP | other *** search
/ The Party 1994: Try This At Home / disk_image.bin / source / gravity / gravity.txt
Text File  |  1995-04-18  |  16KB  |  462 lines

  1.  
  2. Gravity Faq
  3.  
  4. The information contained in document will allow you to realistically
  5. simulate jumping and interstellar maneuvers.
  6.  
  7. The purpose of this document is to understand the physics of the
  8. world we live in, and apply realistic physics to your game.  It
  9. will not give you optimized routines for your program, that is your
  10. job!  But if you understand how things work, it will be a heck of
  11. a lot easier.
  12.  
  13. =====================================================================
  14. = Basics
  15. =====================================================================
  16.  
  17. There are some key terms I will define to make sure we are talking
  18. about the same things:
  19.  
  20. Distance    : Total path of travel.
  21. Displacement: Difference between original position, and final position
  22.  
  23. Speed       : Change in displacement divided by the change in time
  24. Velocity    : Direction of travel, and speed of travel.
  25.  
  26. Acceleration: Change in velocity divided by the change in time.
  27.  
  28. Force       :
  29.  
  30. Note: That displacement, velocity, and acceleration are actually
  31. vector quantities. (That is, they have a direction and magnitude).
  32.  
  33. Abbreviations:
  34.  
  35. a   : acceleration
  36. M   : mass
  37. s   : displacement
  38. s0  : initial displacement
  39. t   : time
  40. v   : velocity
  41. v0  : initial velocity
  42.  
  43. units
  44. m     : meter                    (unit of length)
  45. m/s   : meter per second         (unit of speed)
  46. m/s^2 : meter per second squared (unit of acceleration)
  47. N     : Newtons                  (unit of force)
  48. s     : second                   (unit of time)
  49.  
  50. =====================================================================
  51. = The concepts
  52. =====================================================================
  53.  
  54. Imagine a spaceship flying through space at 1000 m/s.  It is deep in
  55. interstellar space with nothing around it.  What forces are acting
  56. on it?  The answer: none!
  57. It will continue to fly through space at 1000 m/s forever if no
  58. force acts on it.
  59.  
  60. : The ONLY reason why objects will change velocity (direction and/or
  61. : speed) is if a force is applied to it.
  62.  
  63. What is a force you ask?  A force is an external push or pull.  A
  64. force is also a vector.  It has a magnitude and a direction
  65. associated with it.    Right now gravity is exerting a force on you:
  66. it is pulling you toward the center of the earth.
  67.  
  68. Also, the only reason an object will change displacement is if
  69. it has some velocity.
  70.  
  71. Suppose now that the hand of god reaches down and starts pushing the
  72. spaceship forward.  There is now a force applied to the ship.  With
  73. any force applied, an acceleration is induced.  With our new
  74. acceleration, the velocity starts to increase.  If god doesn't stop
  75. pushing the ship, then soon the ship will go very fast!
  76.  
  77.  
  78. =====================================================================
  79. = Look, I just want to make my guy jump.
  80. =====================================================================
  81.  
  82. Ok, I'm getting to this.  We have our jumping dude Joe.  Joe's
  83. initial position is on the floor or platform that is not moving.
  84. The user presses a key and Joe should leap into the air.  You need
  85. to keep track of three things about Joe:  His position, velocity,
  86. and acceleration.
  87.  
  88. Initially, his position will be at the floor.  Once the key is hit,
  89. we simply give Joe a very fast velocity in the upward direction.
  90. Then every frame/sec/update/whatever decrease his upward velocity
  91. by a constant amount.  That's it!
  92.  
  93. The constant decrease is actually an acceleration in the downward
  94. direction.  The horizontal motion of Joe is unaffected.  So while
  95. Joe is flying through the air, you can change his forward/backward
  96. movement as normal.
  97.  
  98. Side note:  In real life, it is impossible to move forward/backward
  99. when jumping straight up.  But I find games that allow forward/
  100. backward movement while jumping more playable and fun than just jumping
  101. straight up.  And who said games are realistic anyway?
  102.  
  103. What should be the acceleration that we are applying?  On earth, the
  104. acceleration of Joe will be 9.81 m/s^2 in the downward direction.
  105. Unfortunately, meters per second squared means nothing to the computer.
  106. The acceleration must be determined based on your game's update rate.
  107. Usually guessing and tweaking is the best way to achieve this.
  108.  
  109. So our pseudo-code is as such:
  110.  
  111. Joe.pos.y = 0; (Or wherever floor is)
  112. Joe.vel.y = 0;
  113. Joe.accel = 10;
  114. Joe.jumping = NO;
  115.  
  116. Loop
  117.   If (jump key is pressed)
  118.   {
  119.      Joe.vel.y = 100;
  120.      Joe.jumping = YES;
  121.   }
  122.  
  123.   if (Joe.jumping == YES)             /* Move Joe if we are jumping */
  124.   {
  125.      Joe.vel.y = Joe.vel.y - Joe.accel
  126.      Joe.pos.y = Joe.pos.y + Joe.vel.y
  127.   }
  128.  
  129.   if (Joe.pos.y <= 0)          /* Check to see if Joe has hit floor */
  130.   {
  131.      Joe.vel.y = 0;
  132.      Joe.jumping = NO;
  133.   }
  134. End Loop
  135.  
  136. For the first time the jump key is pressed, the position of Joe will
  137. suddenly jump off the floor.
  138.  
  139. As he goes higher, Joe begins to slow down because the velocity
  140. is constantly decremented.  Joe will eventually stop moving up, and
  141. begin to move down.  He will move faster and faster until
  142. he reaches the floor again.
  143.  
  144. Joe's velocity and position in the y direction at the end of each loop
  145. will appear as such:
  146.  
  147. Loop0 : Joe.vel.y =   0     Joe.pos.y =   0
  148. Loop1 : Joe.vel.y = 100     Joe.pos.y = 100
  149. Loop2 : Joe.vel.y =  90     Joe.pos.y = 190
  150. Loop3 : Joe.vel.y =  80     Joe.pos.y = 270
  151. Loop4 : Joe.vel.y =  70     Joe.pos.y = 340
  152. Loop5 : Joe.vel.y =  60     Joe.pos.y = 400
  153. Loop6 : Joe.vel.y =  50     Joe.pos.y = 450
  154. Loop7 : Joe.vel.y =  40     Joe.pos.y = 490
  155. Loop8 : Joe.vel.y =  30     Joe.pos.y = 520
  156. Loop9 : Joe.vel.y =  20     Joe.pos.y = 540
  157.  
  158. Loop10: Joe.vel.y =  10     Joe.pos.y = 550
  159. Loop11: Joe.vel.y =   0     Joe.pos.y = 550
  160. Loop12: Joe.vel.y = -10     Joe.pos.y = 540
  161. Loop13: Joe.vel.y = -20     Joe.pos.y = 520
  162. Loop14: Joe.vel.y = -30     Joe.pos.y = 490
  163. Loop15: Joe.vel.y = -40     Joe.pos.y = 450
  164. Loop16: Joe.vel.y = -50     Joe.pos.y = 400
  165. Loop17: Joe.vel.y = -60     Joe.pos.y = 340
  166. Loop18: Joe.vel.y = -70     Joe.pos.y = 270
  167. Loop19: Joe.vel.y = -80     Joe.pos.y = 190
  168. Loop20: Joe.vel.y = -90     Joe.pos.y = 100
  169. Loop21: Joe.vel.y =-100     Joe.pos.y =   0
  170.  
  171. Notice that Joe is going very fast when he hits the floor.
  172. If Joe does not hit the floor, or the floor disappears (i.e. he jumps
  173. off a platform) then eventually Joe will be going very fast.
  174.  
  175. We need to give him a limiting velocity.  This is the fastest velocity
  176. Joe can go by falling.  In real life this is caused by air resistance.
  177. This is easy enough to do.
  178.  
  179. The modified pseudo-code is such:
  180.  
  181. Joe.pos.y = 0; (Or wherever floor is)
  182. Joe.vel.y = 0;
  183. Joe.accel = 10;
  184. Joe.jumping = NO;
  185.  
  186. Loop
  187.   if (jump key is pressed)
  188.   {
  189.      Joe.vel.y = 100;
  190.      Joe.jumping = YES;
  191.   }
  192.  
  193.   if (Joe.jumping == YES)             /* Move Joe if we are jumping */
  194.   {
  195.      if (Joe.vel.y > -100)          /* Limit Joe's velocity to -100 */
  196.      {
  197.              Joe.vel.y = Joe.vel.y - Joe.accel
  198.              Joe.pos.y = Joe.pos.y + Joe.vel.y
  199.      }
  200.   }
  201.  
  202.   if (Joe.pos.y <= 0)          /* Check to see if Joe has hit floor */
  203.   {
  204.      Joe.vel.y = 0;
  205.      Joe.jumping = NO;
  206.   }
  207. End Loop
  208.  
  209. The added loop will not decrease Joe's velocity if he is already moving
  210. downward at a speed of -100.  Thus we have added his limiting velocity.
  211.  
  212. =====================================================================
  213. = Other dimensions
  214. =====================================================================
  215.  
  216. Ok, the above example is all well and good, but some game sprites
  217. do not just go up and down.  Some go side to side while they are
  218. jumping.
  219.  
  220. Easy!  It just so happens that the x and y values of velocity and
  221. acceleration are independent!  So while you resolve the y position
  222. and velocity of Joe, you can move Joe in the x direction normally.
  223.  
  224. It will appear that Joe follows a parabolic path, which is what he
  225. would follow in the real world.
  226.  
  227. The same goes for 3 dimensions.  x,y,z velocities and accelerations are
  228. all independent of one another.
  229.  
  230. =====================================================================
  231. = Spaceman Spiff
  232. =====================================================================
  233.  
  234. Another example where kinematics applies is an asteroids type game.
  235. You fly around in a zero-g environment, but can thrust forward in
  236. any direction.
  237.  
  238. In this case gravity is not a factor.  What do we use instead?  We
  239. use the thrust of the spaceship as our acceleration.  We simply break
  240. up our acceleration vector into x and y directions, and apply the same
  241. techniques as above.
  242.  
  243. To implement this we get the pseudo-code:
  244.  
  245. pos.x = 0;  pos.y = 0;
  246. vel.x = 0;  vel.y = 0;
  247. acc.x = 0;  acc.y = 0;
  248.  
  249. Loop
  250.   if (thrust key pressed)
  251.   {
  252.      resolve_direction_vector;
  253.      acc.x = scale_x * 10;
  254.      acc.y = scale_y * 10;
  255.   }
  256.  
  257.   vel.x = vel.x + acc.x;
  258.   vel.y = vel.y + acc.y;
  259.  
  260.   pos.x = pos.x + vel.x;
  261.   pos.y = pos.y + vel.y;
  262. End Loop
  263.  
  264. Now we have a step we have not seen before: resolve_direction_vector.
  265. We need this because the ship can move in any direction.
  266. What this step does is get the appropriate directions vector and
  267. break it down into an x vector and y vector.  Once broken down, the
  268. resolve_direction_vector step will set scale_x and scale_y.
  269.  
  270.                          /^
  271.          Original       / |
  272.          acceleration  /  |
  273.          vector       /   |
  274.                      /    |
  275.                     /     |  Y vector
  276.                    /      |
  277.   Start           / *     |
  278.   position ->    X------->|
  279.  
  280.                   X vector
  281.  
  282. * = Angle of ship measured from x axis
  283.  
  284. Given an angle for our direction from 0 to 365, we can determine
  285. the scale_x and scale_y variables.  There are various ways to
  286. do this, but an easy way (not optimized) is to use trig:
  287.  
  288. scale_x = cos(angle);
  289. scale_y = sin(angle);
  290.  
  291. The trig functions will take care of negative values.  That is, if
  292. the ship is facing West (angle = 180 degrees), the values for
  293. scale_x and scale_y:    scale_x = -1;    scale_y = 0;
  294. And our ship will accelerate in the negative x direction.
  295.  
  296. How about if the ship is pointed in a Northeast direction (angle = 45)?
  297. We get:
  298. scale_x = .707
  299. scale_y = .707
  300.  
  301. Which means the ship is accelerating by 7.07 in the y direction, and
  302. by 7.07 in the x direction.  The illusion you see the ship
  303. accelerate by 10 in a 45 degree direction.
  304.  
  305. You will want to limit the maximum velocity of the ship, or else
  306. it will soon be going faster than the user can see.
  307.  
  308. If you want, some people like a damping field.  That means eventually,
  309. the ship slows down by itself.  To do this, simply decrement the
  310. velocity towards zero every loop.  (e.g. if velocity is positive,
  311. subtract; if velocity is negative, add)
  312.  
  313. =====================================================================
  314. = The REAL equations
  315. =====================================================================
  316.  
  317. This section explains the real equations.  If you wish to do some
  318. more complex kinematics, this section will give you everything you
  319. need to resolve the equations.  If not, then the above text is
  320. plenty for a lot of applications.  For more explanations, consult
  321. a basic physics book.
  322.  
  323. We define three key variables with respect to an object:
  324. displacement, velocity, and acceleration.
  325.  
  326. Force is related to acceleration in the following way:
  327. (Incidentally, Newton's second law of motion)
  328.  
  329. ;Eqn 1
  330. ;
  331. ; F = M * a
  332. ;
  333. In English: Force equals mass times acceleration.
  334.  
  335. So in our spaceship example, the hand of god can cause a small
  336. interceptor with little mass to accelerate faster than a huge
  337. dreadnought with much mass.
  338.  
  339. The relationship between displacement, velocity, and acceleration
  340. are:
  341.  
  342. ;Eqn 2
  343. ;
  344. ;  s = s0 + v0*t + 0.5*a*t^2
  345. ;
  346. The current displacement is equal to the initial displacement, plus
  347. the velocity times time, plus one-half times acceleration times
  348. time squared.
  349.  
  350. ;Eqn 3
  351. ;
  352. ; v = v0 + a*t
  353. ;
  354. The current velocity equals the initial velocity plus acceleration
  355. times time.
  356.  
  357. ;Eqn 4
  358. ;
  359. ; v^2 - v0^2 = 2*a*s
  360. ;
  361. The current velocity squared minus the initial velocity equals two
  362. times acceleration time displacement.
  363.  
  364. With these 4 magic equations, almost all physics motion problems
  365. can be resolved.
  366. These equations are all based on constant acceleration (which fits
  367. in fine with most computer applications)
  368.  
  369. =====================================================================
  370. = How do these equations work?
  371. =====================================================================
  372.  
  373. Let us take a ball thrown directly up into the air.  This is a
  374. one-dimensional situation, because the ball is only going up or
  375. down.  Let's take s0 to be the position where we release the ball
  376. up into the air.  Since we have an initial position, I'm going to
  377. be referring to the current displacement of the ball as position.
  378. The term position will be reference from s0.
  379.  
  380. Ok, so we throw the ball up into the air. The instant we release
  381. the ball (t = 0) we know:
  382. s0 = 0;
  383. v0 = 10 m/s in the up direction (about 22 miles per hour)
  384. a = ?
  385.  
  386. Hmm. What is the acceleration?  For any object near the earth's
  387. surface, the force due to gravity can be found with:
  388. ;Eqn 5
  389. ;
  390. ; Fg = M*g
  391. ;
  392. Where M is the mass and g is the gravitational constant.
  393. For earth, g = 9.81 m/s^2.
  394.  
  395. A bit of math now.  Combine Eqns 1 & 5
  396. ;
  397. ; Fg = M*a  ->  M*g = M*a  ->  g = a
  398. ;
  399. Whoa! The masses seem to have canceled!  That's interesting.  For
  400. any falling object, the acceleration is not affected by the mass.
  401. Does this mean that a hippo will fall as fast as a Ping-Pong ball?
  402. Yes they will: in a vacuum.  But I digress...
  403.  
  404. So the acceleration of the ball equals g, or 9.81 m/s^2.
  405. But the acceleration is in the DOWNWARD direction, so we say a = -g
  406.  
  407. Using Eqn 3, we get:
  408.   v  =  v0 + a*t  =  10 m/s +  (-9.81 m/s^2) * 0 s  =  10 m/s.
  409. If we use Eqn 2, we also get s = 0. Big deal, huh?
  410.  
  411. Let's take a look at 0.5 sec later:
  412.   v  =  10 + (-9.81) * 0.5 s  = 5.1 m/s
  413.  
  414. The ball has slowed down, which is what we expect.  Eventually it
  415. will stop.  To find this, we simply set v = 0 and solve for t.
  416. v  =  0  =  10 + (-9.81) * t
  417. Solving for t, we find t = 1.019 seconds.  Thus 1.019 seconds after
  418. we throw the ball up, it stops in mid-air.
  419.  
  420. Ok, we know it stops, but where is it?  To find this, we use Eqn 2:
  421.   s  =  0  +  10 * 1.019 + 0.5 * (-9.81) * 1.019^2  =  5.1 meters
  422. That is 5.1 meters above the spot where we released it.
  423.  
  424. What goes up must come down, but how fast?  How fast is our ball
  425. going when we catch it?  When we catch the ball, we know that
  426.   s = 0.  So use Eqn 4.  After some calculations, we find
  427.   v = sqrt( v0^2 ).  That's easy, right? v = v0.  Well...sort of.
  428.  
  429. There is actually two solutions to this equation.  +v0 and -v0.
  430. What does this mean?  It means whenever the ball is at position
  431.   s = 0, then it is either going up at v0 or down at v0.  Since
  432. we know it was going up when we threw it, it must be coming down
  433. at the same velocity.
  434.  
  435. The last thing we find is what time it comes down:
  436. Use Eqn 3:
  437.   v  =  -v0  =  v0 + (-9.81) * t
  438. Result: t = 2.038 seconds.  If we throw a ball up at 10 m/s, then
  439. 2.038 seconds later, we will find the same ball in our hand.
  440.  
  441. This section was just to show how the equations work.  In an
  442. actual program, you wouldn't simply stick these equations in because
  443. you would have to keep track of t for every single object.  Every time
  444. something jumped, you'd reset that object's personal t.
  445.  
  446.  
  447. =====================================================================
  448. = Hasta La Vista, dudes
  449. =====================================================================
  450.  
  451. Well, that's it.  Hope this gives you enough information to start out.
  452.  
  453. If you have questions, suggestions, clarifications, and any other
  454. modifications, please drop me a line.
  455.  
  456. Edgar Roman
  457.  
  458. eroman@nmsu.edu
  459. or (aroman@nmsu.edu)
  460.  
  461. First Release: 12/29/94
  462.