# Euler Integration & Collision Response

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

## Recommended Posts

Greetings,

Partly for fun and partly to expand my knowledge, I have decided to write a very simple rigid body physics engine. While I have used third-party ones for many years, I have very little knowledge as to how the internals actually work, especially in regards to integration. Collision detection is fine, but I just want to make sure I am calculating my initial velocity properly, along with any reflected velocities (i.e. bouncing after a collision). Note that I am only looking at dynamic rigid-body points colliding with static immobile shapes.

The body's velocity is calculated as:

acceleration = body.forces / body.mass;
acceleration += gravity;
body.velocity = acceleration * time_step

The initial direction/amount to move the body by for this time step (i.e. before collisions) is:

displacement_vector = body.velocity * time_step

When working out the reflected velocity:

reflection_vector = Reflect (body.normalized_velocity, plane.normal) (I already have the maths for this)
reflection_vector.Normalize () (in case it isn't already)
reflection_velocity = reflection_vector * body.velocity.Magnitude ();

The reflection velocity is then set as being the body's velocity, and the cycle repeats itself until one time step's worth of movement has been completed.

The main bit I was concerned about was calculating the reflected velocity: am I supposed to add in the fixed velocity modifiers (i.e. gravity) after each bounce, or are they only applied once at the start of the time step? Finally, I scale the reflected vector by the magnitude of the original velocity (to simulate perfect bounce) - is this correct? Everything works in my sample simulation, but I want to make sure that I'm not building errors upon errors.

P.S. I know Euler integration is terrible - I'm just using it as a learning experience. Once I understand this fully, I will move onto RK4 which to be quite honest terrifies me (especially when it comes to the above scenarios).

##### Share on other sites
I usually deal with velocities and impulses, so my set up looks like this:

// apply forces
body.velocity += (gravity and other forces)*timestep

// collision detection + response
body.velocity -= 2*plane.normal*Dot(plane.normal, body.velocity)

// intergrate
body.position += body.velocity*timestep

That's pretty much it. Personally, I've never bothered with RK4 and we've shipped a least two AAA titles, one with heavy physics just using Euler

Cheers, Paul.

##### Share on other sites
Thank for for the reply - good to know I'm on the right track!

For things like gravity and any other fixed-velocity modifiers, do you have to add them in again after each reflection or is it just once per time step?

##### Share on other sites
Just once per timestep. You are integrating a force over a time interval, so once that change in velocity is "in there", you don't have to integrate again until the next timestep.

BTW, by updating your velocity before your positions, you are using a variant of euler integration called symplectic euler, or sometimes semi-implicit euler, that is significantly more stable than explicit euler. Point being, don't worry too much about RK4 unless a) you are interested (if so, kudos), or b) you need the additional accuracy. (b) seems unlikely.

##### Share on other sites
RK4 is actually pretty piss-poor if you're dealing with multiple simultaneously constrained bodies.

Best bang for the buck: velocity verlet or "SUVAT" SUVAT"]http://en.wikipedia....of_motion. Semi-implicit Euler is competitive, though. Which is best depends a lot on whether you have any drag components (forces based on velocity) and a variety of other factors.

##### Share on other sites

I usually deal with velocities and impulses, so my set up looks like this:

// apply forces
body.velocity += (gravity and other forces)*timestep

// collision detection + response
body.velocity -= 2*plane.normal*Dot(plane.normal, body.velocity)

// intergrate
body.position += body.velocity*timestep

That's pretty much it. Personally, I've never bothered with RK4 and we've shipped a least two AAA titles, one with heavy physics just using Euler

Cheers, Paul.

Hi wildbunny, I liked reading your articles, and I hope you don't mind me asking a question through this thread. After many, many experiments I must agree to your above statement - it simply doesn't pay off to use anything else than impulses (or alternatively forces + symplectic Euler integration). One thing though, I'm hoping you might be able to answer. Through math and experiment I've arrived at the same solutions for particle - particle collision and constraints as you're using, but I'm still looking for a good way to represent the same equations in angular form. For instance, the particle - particle constraint will reach equilibrium and stand still in just one loop if no other forces / impulses influence it. Does a similar solved-in-one-loop equation exist for rigid bodies that take rotation into account? So far, I've rewritten the equations for collision with friction so they work as a constraint, and this yields decent result, but I can't help wonder if something better is out there.

Cheers,
Mike

##### Share on other sites
this is not a math forum but

euler equation is unexplained in math text book...

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 21
• 11
• 25