Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - Perfectly Elastic Collision

Author
Message
Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 25th Feb 2012 09:01 Edited at: 25th Feb 2012 10:16
A perfectly elastic collision between a moving object and a stationary object always results in a 90 degree angle! (except in the degenerate case of a dead-on collision)

I dunno why, but I felt like coding up a simulation of this.



Each frame is the result of a simulation, and it's actually coded so that you can do this with any number of particles. For example:


[edit]
and so just to make it clear: The first program is like the second program only it's visualized differently. The accuracy & method of calculation is the same.

[edit2]
also, it's really interesting to note that the collision process is handled through force only. There's no rigid body collision, no momentum conservation, only spring forces acting on circles (although conservation of momentum IS implied by the equal/opposite forces). It's a more general method. In fact, this simulation: http://www.youtube.com/watch?v=P-oc1eYZO2Y uses that method. They LOOK like rigid spheres, but they're just very rigid springs. the problem with this method is that you have to use a small timestep, otherwise it's not very accurate. (the simulation is open source, look at the code if you don't believe me. The collision code is very similar to what I have here, except for the key difference that his code has a dampened spring instead.)

Rapidrory
14
Years of Service
User Offline
Joined: 21st Aug 2009
Location:
Posted: 5th Apr 2012 21:25
Ah this is exactly what I've been trying to do for my gas simulation, just every time I've tried using this sort of physics, it looks wrong. May have to try again at some point

Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 7th Apr 2012 17:41
You can actually do it faster and more accurately than this program using laws of conservation of momentum & energy laws.

Conservation of momentum implies:

m1*v1+m2*v2=m1*v1'+m2*v2'

Conservation of energy implies:

m1*v1^2+m2*v2^2=m1*v1'^2+m2*v2'^2

So for the one dimensional case, you can just solve for v1' and v2' and you'll get a collision behaving exactly like in this program.

For the two dimensional case, consider that the only forces between colliding spheres act on the axis of collision. So if one sphere has position p1 and another p2, the direction from p1 to p2 will be denoted by x=(p2-p1)/|p2-p1|.

So now we only have to consider the 1D collision in the direction of x. If we let u1=v1.x, u2=v2.x, p1=v1'.x, p2=v2'.x (so each value is a scalar the result of a dot product of two vectors), then the equations are now:

m1*u1^2+m2*u2^2=m1*p1^2+m2*p2^2
m1*u1+m2*u2=m1*p1+m2*p2

It's not a vector equation, and so it's easily solvable. Once you have the solutions p1 and p2 you can calculate the vectors v1' and v2'. What we want is that v1' is equal to v1 on the axis perpendicular to x, but has magnitude p1 in the direction of x. So first we zero the velocity in the direction of x:

v1-(v1.x)*x

Then we add or subtract p1*x. So:

v1'=v1+(p1-v1.x)*x
v2'=v2+(p2-v2.x)*x

and voila, perfectly elastic collision!

Login to post a reply

Server time is: 2024-03-29 13:44:13
Your offset time is: 2024-03-29 13:44:13