This topic is now archived and is closed to further replies.


physics, collision, and gravity

Recommended Posts

I implemented physics, collision detection and gravity in engine. My question is in what order should I do them ? I have... if(detectCollision()) { doPhysics(); } doGravity(); translate(); In what order should these be called ? I tried various orders, some tend to make the ball jump higher every time but let a ball restingon an inclined plane slide down. While the other one I tried doesnt make the ball jump higher (jumps to same height each time) but the ball won slide down on an incline plane. Any ideas ? Nitzan ------------------------- ------------------------- [edited by - nitzan on November 7, 2002 9:09:31 PM]

Share this post

Link to post
Share on other sites
Well doGravity(); allways affects the ball so it should be done first then physics.


1. add the gravity vector to the balls motion vector

2. detect colition

3. if colition then reflect the balls motion vector relative to the normal of the plane that the ball colides with

4. if colition then multiply the balls motion vetor with something like 0.95

Hope this can help you solve the problem.

Share this post

Link to post
Share on other sites
So you have a ball start out at a ceratain point in space with and an initial velocity vector. NOTICE: Gravity only affects the vertical component of the vector, horizontal component is independent and (if no friction in program)stays constant, unless you want the ball to gain velocity from collisions.

1. Lets say it starts out falling. So you reduce the balls height (according to the formula which i assume u know dx=1/2at^2) and increase the balls vertical component of velocity.

2. If you detect collision reflect the vector accordingly. And increase or reduce the magnitude of the vector (depending on friction, and or elastic and inelastic collisions). Rember the ball bounces off walls at equal and opposite angle.

Could stop myself from doing ascii diagram:

..\...| <----Wall
...( )|
( )...|

... And so on.
If you dont want your ball to stop, increase the magnitude of the velocity vecter a little with each collision

[edited by - snisarenko on November 9, 2002 1:10:26 AM]

Share this post

Link to post
Share on other sites
usually gravity is considered part of physics. if you''ve set your physics up using kinetecs of motion equations everything is done for you and you don''t need a special doGravity() method

s(t) = 1/2a*t^2 + v*t + s

s(t) = position at time t
a = acceleration
v = last calculated velocity
s = last calculated position
t = time since you last calculated this equation

gravity is some amount of acceleration downwards. i.e. set the y value of your acceleration vector to some negative value. if you want gravity to "look" right, then you''ll be wanting to use that equation. you can certainly make it look right through trial and error tweaking, but why tweak when the equations defining motion have been worked out for you.

but, if you don''t want to change what you''ve got, then just doGravity right after doPhysics.....


Share this post

Link to post
Share on other sites