#### Archived

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

# Implementing physics in a 3d engine...

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

## Recommended Posts

Hi, A few days ago I came across a link to this. I thought it was really quite neat and I haven''t seen anything quite like it before. That got me wondering on how I could make my own good physics in my own engine. I came up with the following: The first step to doing a good physical simulation is good collision detection. A type of bounding volume should be used to check for possible collisions. For this I would like to use oriented bounding boxes (OBB). They can be slightly more accurate than axis aligned bounding boxes (AABB). Once possible collisions are detected, it must be done on a per polygon level to make things nice and accurate. Each object must have several attributes - mass, current velocity, previous velocity (from last frame), and acceleration. The forces in my program (explosions) are calculated based on the distance from the center of force and the strenght of the force. Objects closer to the center of the force will have greater forces acting on them and objects further away from the center of the force will have less force acting on them (this could have a linear falloff to make things interesting). The force and mass of the object is known, so this can be used to calculated the acceleration. Acceleration is basically the change in velocity over time. The change in time is the change in time between frames when the physics is being updated. The initial velocity is known (the velocity from the frame before), the change in time is known, so this means that the new velocity can be calculated. The only thing that I don''t know how to fit in is collisions between objects using the conservation of momentum. I probably don''t remember all this from the physics class that I took last semester. Momentum is mass times velocity. The part I don''t understand is how do I know how much momentum has been transferred between objects during a collision? I also don''t know how to get an object to roll when a force is applied. This is part of the reason why I think OBBs would be well suited to the job. The force can be applied to the corners of the box. Is there a specific formula for force causing rotation? I wish I hadn''t disposed of my physics notes/work from last semester... Moe''s site

##### Share on other sites
To get rotation with a force:

CrossProduct(Force,ForcePos-MassCenter)

this compute the Rotation Vector with the length of the Torque.

understand ??

##### Share on other sites
so I''ll take momentum:

Wasn''t it like there are various types of momentum transitions?

One extreme beeing the bouncing balls on strings. One hits the other, stops completely; the other moves of with all the momentum of the first.

The other extreme would be the ball hitting a marshmallow. Ball and marshmallow join their masses and moments and travel of together.

If my memories are correct, the behaviour depends on how solid the objects are.

---------------------------
I may be getting older, but I refuse to grow up

##### Share on other sites
Like for Rigid Body Physics.

I can''t rememeber where on gamasutra but there is a couple of demos and good articles by Mr. Andres about this.

It''s pretty cool stuff.

##### Share on other sites
So the amount of momentum each object gets/loses depends on the objects 'stiffness'?

As for the cross product - the cross product finds a vector that is perpendicular to the two vectors that are put in, right?
Moe's site

[edited by - Moe on June 27, 2002 11:02:59 PM]

##### Share on other sites
Eyup. Cross is perpendicular to the 2 input vectors.
BTW...

u cross v is NOT v cross u.

Same length, opposite directions.
I think you use that for normals, not sure.

Good luck !

##### Share on other sites
Hi, I''m doing 6th form physics now. Conservation of momentum is defined as:

"In any collision or explosion the amount of momentum before equals the final momentum"

Remember momentum is mass times velocity. This means if an object travelling at 10ms-1 with mass of 100kg hits a 150kg object at rest, the first object will have a momentum of 1000Kgms-1 and so will the second object because of the law of conservation of mom.
However, the second objects'' mass can''t change, nor it''s momentum so it''s speed must change. Imagine it were a frictionless world. The second object''s speed would be (P = momentum):

P = mv
V = P/m

= 1000/150
= 7ms-1 (1sf)

I hope this helps. it just means you have to slow the second object down or make it go faster depending on it''s mass, by using the first object''s momentum.
Email me on ryan@pcsoftware.co.nz if you need any more physics help.

btw, on your question for "is there a specific formula for force causing rotation?" the answer is torque which causes a twisting force. This is simply:
Torque = Force (perpendicular to pivot extension) * Distance (from pivot to edge of object)

- Ryan

##### Share on other sites
The thing I don''t understand is how do I know how much momentum is passed on to the other object?

Lets suppose we have 2 balls rolling toward eachother (not directly toward eachother, on some sort of angle - like a 90 degree angle between the two directions). The first ball is much larger and is moving faster than the second. When the balls collide, the larger ball should move a bit slower and the smaller ball should probably speed up a little. How do I know how much momentum is passed between the objects?

Moe''s site

##### Share on other sites
If you are asking: Given a 90 degree slope, we have a larger ball at the top rolling down and a smaller ball in the middle rolling down, how much momentum would be released into the smaller ball?

Well, since they are moving, you would probably have

P[final] = P[largerball] - P[smallerball].

I would think you would simply find the difference between how fast the two balls are going and what their total force(?) would be in their current direction.

~Dwarf

##### Share on other sites
quote:
Original post by Moe
The thing I don''t understand is how do I know how much momentum is passed on to the other object?

This is dependent on the elasticity of the collision.

Take a look at this (linear momentum and collisions).

##### Share on other sites
quote:
Original post by Oluseyi

Take a look at this (linear momentum and collisions).

Moe''s site

##### Share on other sites
Simple 2 Object Collision in 1 Dimensions.

P=mv where m=mass, v=velocity
|
Momentum kg/m/s

Eg. A ball with a mass of 2 kg collides with a 2.5 kg ball @ rest (40 m/s), and ball 2 has a final velocity of 20 m/s. What was the final velocity of ball 1?

[1D COLLISION]:
Pbefore = P1 + P2
PB = (2kg)(40m/s) + (2.5kg)(0m/s)
PB = 80 kg/m/s
Pbefore = Pafter
PB = P1f + P2f
PB = (2kg)(X) + (2.5kg)(20m/s)
X= (PB - P2f) / M1f
The final velocity (X) of ball 1 is: 15m/s

That should get you started!

[edited by - Carandiru on July 4, 2002 3:20:59 AM]

##### Share on other sites
in order to implement a good physics engine, i strongly recommend you do LOTS of research. there are some excellent papers written by professionals on Rigid Body Simulations on the net which you should read. some even supply the basic code. it is a great plus to have good knowledge about ordinary differential equations and have experience in methods such as euler and runga kutta. this subject is HUGE, so you probably wont find adequate stuff in forums. you should start searching up on rigid body simulations. they will explain the methods of collision detection and will provide you the equations necessary for proper collision response, and also to apply constraints on the bodies.

Salman

##### Share on other sites
Just a quick note: Look for Chris Hecker's tutorials. I can't remember the URL, but should be an easy find. Also has tutorials on perspective correct texture mapping, etc..

http://www.d6.com/users/checker/dynamics.htm

[edited by - Melraidin on July 4, 2002 1:51:56 PM]

##### Share on other sites
Formulas for 3D non-plastic collision response

Change in linear velocity
Dv = ( J n + (m J )t ) / m

Change in angular velocity
Dw = {r x (J n + (m J )t )} / I cg

Negate the J n term for the second object (do not negate the (m J )t term) for both velocity equations.

Collision Impulse
J = -v r (e + 1) /{1 /m1 + 1/m2 + n • (r 1 x n )/I 1 + n • (r 2 x n )/I 2}

Unit tangent vector
t = (n x v r) x n
t = t / |t |

m: mass of body
m: coefficient of friction (between the bodies at point of impact)
e : coefficient of restitution
r : displacement vector from center of mass to point of impact
v r: relative velocity of colliding objects (prior to collision)
I cg: Inertia tensor (referenced to the center of gravity)
n : unit normal vector (at point of collision)

Optionally, neglect friction by replacing m with 0 (t is no longer needed in this case) and simplify the equations.

[edited by - Magmai Kai Holmlor on July 5, 2002 5:22:11 PM]