I thought you just do conservation of momentum? And use the coefficient of restitution for the material to work out the "bounciness" for elastic collisions? I only say this because we did it in mechanics just this morning...
Last edited by blob8108 (2012-10-18 11:33:56)
Offline
I temporarily only care about perfectly elastic bodies, because, well, they're the most fun and make the most impressive demo. I'm trying to figure out the final velocities of two bodies after impact. If you look up "elastic collision" on WolframAlpha, you get a weird formula I don't trust.
Offline
Does this work? It's only for scalar velocities, but hopefully the maths is equally valid for vectors.
First object: m_1 — mass u_1 — velocity before collision v_1 — velocity after collision (find) Similarly for second object... m_2 — mass u_2 — velocity before collision v_2 — velocity after collision (find) e — coefficient of restitution When e=0: inelastic collision, v_1 = v_2 When 0 < e < 1: elastic When e=1: "perfectly elastic" Momentum is conserved: m_1*u_1 + m_2*u_2 = m_1*v_1 + m_2*v_2 m_2*v_2 = m_1*u_1 + m_2*u_2 - m_1*v_1 v_2 = (m_1*u_1 + m_2*u_2 - m_1*v_1) / m_2 Coefficient of restitution: e = (v_2 - v_1) / (u_1 - u_2) v_2 - v_1 = e*(u_1 - u_2) v_2 = e*(u_1 - u_2) + v_1 Combining the equations to find v_1: e*(u_1 - u_2) + v_1 = (m_1*u_1 + m_2*u_2 - m_1*v_1) / m_2 e*(u_1 - u_2) + v_1 = (m_1*u_1 + m_2*u_2) / m_2 - m_1*v_1/m_2 v_1 = (m_1*u_1 + m_2*u_2) / m_2 - m_1*v_1/m_2 - e*(u_1 - u_2) v_1 + m_1*v_1/m_2 = (m_1*u_1 + m_2*u_2) / m_2 - e*(u_1 - u_2) v_1(1 + m_1/m_2) = (m_1*u_1 + m_2*u_2) / m_2 - e*(u_1 - u_2) v_1 = ((m_1*u_1 + m_2*u_2) / m_2 - e*(u_1 - u_2)) / (1 + m_1/m_2) ~ then, find v_2: v_2 - v_1 = e*(u_1 - u_2) so v_1 = v_2 - e*(u_1 - u_2)
Hope that works...
Note to self: do maths on *paper* next time.
Last edited by blob8108 (2012-10-18 14:51:58)
Offline
I just tried it again on paper, and got the same result as Wolfram Alpha when e=1 (ie. perfectly elastic.)[/i] Which may, of course, be precisely what you want. I happen to be sitting next to a scanner, so I can send you the workings if you're interested.
Conclusion: do not try to help Hardmath with maths.
Also, trust WolframAlpha...
Last edited by blob8108 (2012-10-19 14:41:08)
Offline
Thanks for the help; I wouldn't mind seeing what you got, if it's not too much trouble (I assume you don't sit next to scanners 24/7, so I may be asking for a lot).
I tried the Wolfram|Alpha equation, it didn't seem to work right. Here's the JS which does the force calculations, if you want to take a look. Note that I messed up big time: the exertForce function actually takes a velocity, not a force, as input, so basically you should divide by the mass of the body before passing forces to that function. I don't know why that happened, could have been due to the fact that I was coding at 10PM.
var effectiveForceLine = new KMVector(body.x-otherBody.x, body.y-otherBody.y); // Effective vector along which forces of collision act var effectiveForceBody = body.velocity.projectOn(effectiveForceLine).total(); // Effective force exerted by body var effectiveForceOther= otherBody.velocity.projectOn(effectiveForceLine).total(); // Effective force exerted by other body if (otherBody.fixed) {// Ignore this, this is basically for universally fixed objects (the ground) // ??? } else { // Wolfram formula: var final1 = ((body.mass*effectiveForceBody)-(otherBody.mass*effectiveForceBody)+otherBody.mass*(effectiveForceOther-effectiveForceBody))/(body.mass+otherBody.mass); body.exertForce(effectiveForceLine.resizeTo(effectiveForceBody)); // Stop the body. I don't particularly like doing this, it's ugly. I should be applying just one force, right? body.exertForce(effectiveForceLine.resizeTo(final1)); // Apply the relevant force to this body }
Offline
Hardmath123 wrote:
Thanks for the help; I wouldn't mind seeing what you got, if it's not too much trouble (I assume you don't sit next to scanners 24/7, so I may be asking for a lot).
Sadly not But here, I tried it again for you. Although I got a wrong sign this time...
I tried the Wolfram|Alpha equation, it didn't seem to work right.
Hmm. It should work, as far as I know... How didn't it work? Maybe the conversion to vectors breaks it.
...the exertForce function actually takes a velocity, not a force, as input, so basically you should divide by the mass of the body before passing forces to that function. I don't know why that happened, could have been due to the fact that I was coding at 10PM.
I thought force / mass = acceleration, not velocity?
Offline
blob8108 wrote:
Hardmath123 wrote:
Thanks for the help; I wouldn't mind seeing what you got, if it's not too much trouble (I assume you don't sit next to scanners 24/7, so I may be asking for a lot).
Sadly not But here, I tried it again for you. Although I got a wrong sign this time...
Cool, that's exactly what Wolfram says. So my coding is wrong.
Did you use Penultimate or something for that?
I tried the Wolfram|Alpha equation, it didn't seem to work right.
Hmm. It should work, as far as I know... How didn't it work? Maybe the conversion to vectors breaks it.
The end velocities didn't match up to what they should be in obvious cases (i.e. one static body vs. one moving one, etc.)
...the exertForce function actually takes a velocity, not a force, as input, so basically you should divide by the mass of the body before passing forces to that function. I don't know why that happened, could have been due to the fact that I was coding at 10PM.
I thought force / mass = acceleration, not velocity?
Right, acceleration changes the velocity of the body. So basically the exertForce funciton should be renamed accelerateByVector or something.
Offline
How about using the equation for conservation of energy?
K.E. of o1 = 0.5m1u1^2
K.E. of o2 = 0.5m2u2^2
So:
0.5m1u1^2 + 0.5m2u2^2 = 0.5m1v1^2 + 0.5m2v2^2
Then use the equation for conservation of momentum:
m1u1 + m2u2 = m1v1 + m2v2
Should work, right?
EDIT: Hmm.
Last edited by Hardmath123 (2012-10-19 04:13:43)
Offline
Hardmath123 wrote:
blob8108 wrote:
Hardmath123 wrote:
Thanks for the help; I wouldn't mind seeing what you got, if it's not too much trouble (I assume you don't sit next to scanners 24/7, so I may be asking for a lot).
Sadly not But here, I tried it again for you. Although I got a wrong sign this time...
Cool, that's exactly what Wolfram says. So my coding is wrong.
I'm not sure I understand your code...
Did you use Penultimate or something for that?
I used InkBook on OS X — I have one of those Wacom Bamboo Pen tablet thingies
Thought (sorry if this is obvious, don't mean to offend!): are you handling the collision in one go for both objects, or are you iterating through all the objects and handling collisions for each object? If the latter, have you already updated the velocity by the time you handle the collision for the second object? I remember thinking my physics library would have been much better if I'd thought about handling each collision in one go... I imagine you've already thought of this, however
Offline
What don't you get about my code? I commented it as well as I could.
I'm actually being lazy and doing collisions for each object. I'll do it your way in later refinements. But don't worry, the actual velocity and motion is all handled in a separate for loop at the end of my physics.
You're writing a physics library?
Offline
... very stupid mistake ... now it works.
Offline
Hardmath123 wrote:
What don't you get about my code? I commented it as well as I could.
You did, yeah, sorry I just couldn't quite figure out what "effective force" was, and that sort of thing. I didn't long at it very long, to be fair.
I'm actually being lazy and doing collisions for each object. I'll do it your way in later refinements. But don't worry, the actual velocity and motion is all handled in a separate for loop at the end of my physics.
Good — I was sure you'd have thought of that! I guess I'd run out of suggestions
You're writing a physics library?
I was writing a simple one for a game — yet another project I'll finish, some day...
Hardmath123 wrote:
... very stupid mistake ... now it works.
Glad you fixed it! Can I ask what it was?
Offline