This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#26 2012-10-18 11:22:27

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

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...  tongue

http://imgur.com/aLSaR.jpg

Last edited by blob8108 (2012-10-18 11:33:56)


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#27 2012-10-18 13:16:46

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

I temporarily only care about perfectly elastic bodies, because, well, they're the most fun and make the most impressive demo.  tongue  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.  hmm


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#28 2012-10-18 14:50:54

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

Does this work? It's only for scalar velocities, but hopefully the maths is equally valid for vectors.

Code:

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.  tongue

Last edited by blob8108 (2012-10-18 14:51:58)


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#29 2012-10-18 15:06:17

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

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.  tongue

Also, trust WolframAlpha...

Last edited by blob8108 (2012-10-19 14:41:08)


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#30 2012-10-19 02:35:35

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

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.  tongue

Code:

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
}

Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#31 2012-10-19 03:42:33

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

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  tongue  But here, I tried it again for you. Although I got a wrong sign this time...  tongue


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.  hmm


...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.  tongue

I thought force / mass = acceleration, not velocity?


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#32 2012-10-19 03:47:51

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

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  tongue  But here, I tried it again for you. Although I got a wrong sign this time...  tongue

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.  hmm

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.  tongue

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.


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#33 2012-10-19 04:07:38

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

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)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#34 2012-10-19 06:07:53

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

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  tongue  But here, I tried it again for you. Although I got a wrong sign this time...  tongue

Cool, that's exactly what Wolfram says. So my coding is wrong.

I'm not sure I understand your code...  hmm

Did you use Penultimate or something for that?

I used InkBook on OS X — I have one of those Wacom Bamboo Pen tablet thingies  smile

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  smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#35 2012-10-19 06:32:04

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

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?


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#36 2012-10-19 06:46:34

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

... very stupid mistake ... now it works.  smile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#37 2012-10-19 09:18:21

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

Hardmath123 wrote:

What don't you get about my code? I commented it as well as I could.

You did, yeah, sorry  smile  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  tongue

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.  smile

Glad you fixed it! Can I ask what it was?  big_smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

Board footer