View Single Post
Old 04-16-2004, 10:27 AM
Iceplug's Avatar
Iceplug Iceplug is offline

Retired Moderator
* Guru *
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Smile About the Projectiles:

I figured I will also explain how to make objects that fall due to gravity:

If you will remember from a physics book:
D = VT + 1/2 AT^2
V = AT
These two equations are the main equations that describe how things move when their acceleration (gravity) is constant in each dimension (velocity is how much distance changes, acceleration is how much velocity changes) (which is how much you'll add onto the current distance and velocity of the object):

I have rewritten them below with fully descriptive names:

Distancechange = CurrentVelocity * Timechange + 0.5 * CurrentAcceleration * Timechange * Timechange
Velocitychange = CurrentAcceleration * TimeChange

In many games (like my example above), you will have 2 dimensions... one represents the flat part of the earth, the other up and down (perpendicular to the earth surface). So, if you have two dimensions (X and Y), then you'll have two equations like the above

DistancechangeX = CurrentVelocityX * Timechange + 0.5 * CurrentAccelerationX * Timechange * Timechange
VelocitychangeX = CurrentAccelerationX * TimeChange

DistancechangeY = CurrentVelocityY * Timechange + 0.5 * CurrentAccelerationY * Timechange * Timechange
VelocitychangeY = CurrentAccelerationY * TimeChange

So, now you have a distance, velocity, and acceleration in the X direction and in the Y direction.
Usually, you will have the Y axis to represent the up and down direction.
Large values of Y are at the bottom of the form, closer to the land.

So, gravity will only act along the Y axis... therefore the CurrentAccelerationY can be replaced by a constant that you want for gravity... you do not have to put 9.81 or 32.1 here... those are accelerations for meters or feet per second^2... put any number there you want and play around with it until it looks good for you (generally, larger numbers will increase the hangtime of your particles...). Also, since larger numbers for Y lead to the bottom of the form, make gravity positive (negative values will make you 'fall' upwards )

Since all the gravity is in the Y direction, there is no gravity in the X direction (yay!) so therefore you can just do this:
DistancechangeX = CurrentVelocityX * Timechange
VelocitychangeX = 0
Since there's no velocity change in X direction, we don't need to do anything to it.

For the value of time, you can use whatever number you want... since you are probably using a timer or a game loop. If you wanted a time-based game loop, you have to use the TimeChange variables, but otherwise, you don't need them! (yay again) Just set them to one (which is just like removing them)

DistancechangeX = CurrentVelocityX
DistancechangeY = CurrentVelocityY + 0.5 * CurrentAccelerationY VelocitychangeY = CurrentAccelerationY
(Remember VelocitychangeX = 0)

Now, we have three very short equations!
Just modify these and put them in a timer or game loop, and they will make the object fall for you:
When I say modify: change currentvelocity to the current velocity of your object... the currentacceleration can be substituted by whatever you want for gravity...
for the changes, you will do something like this:
Obj.X = Obj.X + DistanceChangeX
Obj.Y = Obj.Y + DistanceChangeY
Obj.VelocY = Obj.VelocY + VelocityChangeY
(Note: no term for Obj.VelocX here, since VelocityChangeX = 0)

Obj.Y = Obj.Y + Obj.VelocY + 0.5 * Obj.Gravity
Obj.VelocY = Obj.VelocY + Obj.Gravity
Obj.X = Obj.X + Obj.VelocX
That's it.

Remember that if you want to throw an object up, you are throwing it towards the lower numbers for Y on the form... so make VelocY negative to make the object go up.
Also, you can divide by 2 instead of multiplying by 0.5:
Obj.Y = Obj.Y + Obj.VelocY + Obj.Gravity / 2
Obj.VelocY = Obj.VelocY + Obj.Gravity
Obj.X = Obj.X + Obj.VelocX

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote