realbasic-games
[Top] [All Lists]

Re: Gravity, Physics etc.

To: REALbasic Games <realbasic-games at lists dot realsoftware dot com>
Subject: Re: Gravity, Physics etc.
From: William Squires <wsquires at satx dot rr dot com>
Date: Mon, 13 Dec 2004 21:15:08 -0600
Cc:
Delivered-to: realbasic-games at lists dot realsoftware dot com
References: <1D4C1578-4D56-11D9-9E4C-000D9334D728 at mac dot com>
Just remember these simple ideas...

1) Position comes first. This is expressed in some unit of length (distance from some central point - the origin of your grid system) such as; meters, inches, feet, kilometers, light-years, etc...

2) Velocity is rate of change of position (how fast your position is changing over time). This is expressed as a length per some unit of time. Ex: miles per hour, meters per second, etc... So if some object (which can be represented as a "point" (usually center of mass or gravity for most simulations) on the above grid) moves with a velocity of 1 meter per second (in some direction), then in 1 second, it'll be 1 meter further from it's location at time t=0 in the direction it was heading. Note that velocity is a vector, but speed is a scalar. Speed is the magnitude of a velocity vector.

3) Acceleration (which is due to a force acting on a mass, either with or against the direction of motion) is rate of change of velocity. It, too, is a vector, and has the units of length per (unit of time squared), such as 9.81 m/s^2 (9.81 meters per second squared, or 1 g, the acceleration downward due to Earth's gravity). Which means that the velocity increases by 9.81 meters/second every second.

Example. Take a rubber ball (basketball for example), and hold it with your hands above a hard concrete floor. Let's say you're 5' 3" tall from feet to shoulders, and your hands are held out horizontally to hold the ball.

  O
 -+-----o  <- Basketball ---
| |                       ^
  |                   63 inches
  |                       |
 /_\______________________v_____

Now imagine a vertical line running up through the ground and passing through the basketball (no, it doesn't pop!). This is your Y axis. For this example, the ball will fall straight down, along the Y axis, so we can dispense with the X (horizontal) axis. Assume that there's a point in the geometric center of the basketball. Call this y=0 At time t=0, the ball has acceleration g=9.81m/s^2, a velocity of 0 m/s, and a position of y=0. At that time, you let go of the basketball. We need to convert inches (5' 3" = 5x12 + 3 = 60 inches from the ground to the center of mass of the ball) to meters, so the units will match those of 'g' above. According to Calculator.App, this converts as:

63" = 1.6002 meters (which is about right - roughly the height of R2D2 :) )

so:

y(t) = y(t-1) + v(t) * t (Y position at some time 't' is the sum of its position at the previous time 't-1' plus the velocity as a function of time (because of the acceleration) times the time at index 't'

This equation allows you to find (in this case) the Y coordinate of the center of mass of the basketball at some time 't' (time since t=0, or what a stopwatch would show if you start at the instant you release the ball.) This works because all the units work, too. y(t) is in meters, and the 't' term, when multiplied by the velocity, cancels out the unit of time, leaving units of length. Everything's in terms of length so we're okay.

y(.1) = y(0) + v(.1) * (.1)

This is asking our system what the Y coordinate of the ball is 0.1 seconds after releasing it. We know y(0) = 0 (because the ball was originally stationary), but we don't know what v(.1) is yet. The equation for velocity is similar to the above, but v(t) replaces y(t), and a(t) (the acceleration as a function of time) replaces v(t), so:

v(t) = v(t-1) + a(t) * t

-or-

v(.1) = v(0) + a(.1) * (.1)

  But our a(t) = -9.81 (i.e. constant acceleration), so:

v(.1) = v(0) + -9.81 * .1

We know v(0) is also 0 just as the position was 0 because the ball was stationary (motionless) the instant before we release it, thus:

v(.1) = -0.981 m/s

Now that we know v(.1), we can plug it back into the above equation, and get:

y(.1) = 0 + (-0.981) * (.1)

-or-

y at .1 seconds from release is 0.0981 meters below its original position ('-' = below as most cartesian coordinate systems in 2 dimensions define 'down the page' (or 'closer to you') as -y, and 'up the page' (further away from you) as +y. Of course, our 'page' is this message as reproduced on your screen :)

If you want to calculate where the ball is 0.2 seconds from release, then you can either:

1) Calculate v(2) from v(1) using the acceleration equation, then get y(2) (this is the easiest case to handle from a computer animation standpoint), or

2) Calculate as v(1), but where 1 'time unit' is now 200 milliseconds (0.2 sec)!!! This is an important point - t=1 doesn't mean '1 second' in my example above, it means '1 time unit' which was 0.1 seconds (or 100 milliseconds). (as relates to the original 'generic' equation(s); when you plug-and-chug, you replace 't' with the actual time in seconds as I did in the example)

This is a really (IMHO) simplified explanation of the physics of movement as it relates to position, velocity, and acceleration. By expanding these equations to the horizontal axis, you can now compute ballistic trajectories (the path of a thrown or shot object through a gravitational field.)


On Dec 13, 2004, at 4:27 PM, Josh Farquhar wrote:

Hi again,
For my major project in software, I am programming a collection of mini-games. However, I have already hit a snag. I am not too good with advanced maths or physics or anything, so I am not sure how to program in gravity. The mini-game is one where you click on a ball to bounce it back up, before it hits the ground. http://www.neopets.com/games/zurroball.phtml is an example. I have managed some simple gravity code, where I add a certain number to a variable called VertVelocity, which I then add to the sprites Y position. This looks OK, but I know there must be something that will look a lot better. I also need the ball to bounce off walls etc. too. Any help would be appreciated! If I have missed out on telling you anything, just ask

That's exactly correct. that 'certain number' is the acceleration, which you're adding to the velocity to make it greater. The increasing velocity changes the position by ever increasing amounts, which is exactly what a falling object looks like if you took a video of it with a high-speed camera, and examined each frame.

-Josh

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>


William H Squires Jr
wsquires at satx dot rr dot com dot nospam <- remove the .nospam
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>