Jump to content
  • Advertisement
Sign in to follow this  
m0nkfish

Confusing implementing gravity and Newton's Third Law

This topic is 3508 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing my first game engine in a modular fashion, so objects in the game world have a position, velocity and acceleration. These are all stored as (2D) vectors. So there I am, all happy with my model - but I've hit a snag. What do I do when something is sitting on something else? Obviously its velocity should be 0, but acceleration due to gravity dictates that the velocity is increasing in a downwards direction by ~10m/s.
  Object A
[sitting on]
  Object B
So Object B is exerting the force of m*a in an upwards direction on Object A. Is it worth implementing forces in such a true-to-life fashion? If I assume that all my objects have a mass of 1.0, my upward force is just -gravity. I don't know how far to go with this - when a player jumps off something, is he essentially exerting a large downward force that is 'reflected' back up at him to temporarily negate gravity? I've no idea how to implement such things :(

Share this post


Link to post
Share on other sites
Advertisement
It's called the normal force. Is object B static or a free body? If it is static, it applies a force on object A equal to object A's weight force, a normal force. This way they cancel. If object B is a free body, you would just add the weight force from object A to the net force on object B. There are a lot of things that you have to account for, but essentially when a character jumps onto an object you have a collision. Usually this is treated as an inelastic collision, meaning that the character doesn't "bounce" and his/her velocity is set to 0. So, as I said above, what you do depends on the type of object that he lands on.

Say the character lands on a floor. In a game level, this would be static, because usually floors don't move. In this case, you would just have a normal force exerted on the character that is equal to his weight force so he has no acceleration.

Say he lands on a crate that is sitting on the floor. Now you can think of the crate and the player as a single weight force acting on the floor. So the floor has a single normal force on the crate that cancels all of the weight.

There's a lot to think about, and writing an accurate physics engine is a difficult task. I hope this helps at least some.

Share this post


Link to post
Share on other sites
unless you are simulating atoms and atomic interaction, your simulation has two parts:

1.) equations of motion
2.) collision detection

if your were simulating magnetic and electric forces between atoms, then collision will be a part of equation of motion as electromagnetic repulsion between atoms and molecules.

in any other case you need to handle collision separately. you simply need to have SIZE and POSITION of each object, then each step you check if the distance between every pair of objects is less than their "shape size".


ALGORITHM:

--- motion
for i=0 to numberOfObjects

obj.acc= f/m
obj.vel+= obj.acc * timeStep
obj.pos+= obj.vel * timeStep

--- collision
//check if each object is "close" to every other
for i=0 to numberOfObjects
for j=0 to numberOfObjects

if(obj.pos - obj[j].pos < SIZE) collision! --> modify obj[i,j].vel/.acc

DRAW()
--->REPEAT



this is vector math as you know, so "size", acc, vel and pos have their (x,y,z) components in 3D. usually objects can be approximated by spheres(circles in 2D), boxes(squares,rectangles in 2D) and other simple geometry to simplify their "size shape" and speed up calculation. often objects are separated in two main categories:

1.) STATIC - walls, floors and other "background" objects
2.) DYNAMIC - everything else

one of the tricks here is that you do not need to check STATIC vs STATIC objects collision, they do not move so they can not collide. still, you need to check DYNAMIC vs STATIC and some DYNAMIC vs DYNAMIC... but not all, for example we usually do not need to check if two bullets will collide in mid air, only if they hit a wall or someone's butt, right?




now, lets see how this applies to your case and the case of bouncing ball...

so, everything is going fine, until there is some collision, or some key press like jump, run, gas-pedal, brakes-pedal, left, right... your only concern is how to modify acceleration or velocity vectors.

1.) when you press "jump key" you simply give your object upward velocity like this: "obj.vel.y= 32.1". say, "brake-pedal" is pressed, you could slow car down by applying negative acceleration: "obj.acc= -12.3", or by modifying its velocity directly: "obj.vel-= 32.1"


2.) bouncing ball. when it collides with ground our algorithm says we need to modify motion of both. since floor is STATIC we can define its acceleration as constant zero, so we only need to do something about ball's momentum. say we dropped ball from some distance, then by the time it makes its first collision it would have gained some velocity. based on its mass, velocity and "elasticity" or "damping factor" you would then give it some upward velocity and it will bounce up.

when some friction/damping is applied, as energy is lost on impact or air friction, it will eventually make this bouncing velocity zero and the ball will settle on the ground in constant collision but zero velocity.


from this we can see another trick, to optimize our collision further we can treat all the objects with zero velocity as SEMI-STATIC and skip collision checks vs STATIC and other SEMI-STATIC objects, since objects with zero velocity can not move anywhere on their own.

[Edited by - PlayStationX on January 13, 2009 3:30:41 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!