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

#1 2012-10-10 10:58:21

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

Rigid body simulation in JS — work in progress

I'm working on a very simple rigid body simulation library in JS which should ideally work like Box2d, except it should:

- Be simpler, and consist only of primitive shapes (point, connection between two points)
- Have an easy-to-read well-documented and explained code, with the physics explained

As a proof-of-concept I did get a really cool "ball rolling on a hill" simulation to work perfectly, with bouncing and friction implemented. It went straight downhill from there, though (pun NOT intended. honest.), and I'm still struggling with how to model joints between points. I'm trying to deal with a joint as a very stiff spring, since the physics of that is easy to understand and model.


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

Offline

 

#2 2012-10-10 11:14:53

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

Cool!


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#3 2012-10-10 18:58:22

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

Would this have flexible joints and shapes and things like the Rigs of Rods engine?


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#4 2012-10-10 23:08:02

technoguyx
Scratcher
Registered: 2008-10-18
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

Sounds cool.  big_smile  I really gotta get around to actually try Box2D. From what I've seen, you basically create various kinds of objects, simulate physics in real-time, and then render them however you want, am I right?


http://getgnulinux.org/links/en/linuxliberated_4_78x116.png

Offline

 

#5 2012-10-11 01:35:32

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

Re: Rigid body simulation in JS — work in progress

Yeah.


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

Offline

 

#6 2012-10-11 01:38:16

zubblewu
Scratcher
Registered: 2011-02-17
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

Sounds cool  yikes


........................................................................................................................................................................................................................................

Offline

 

#7 2012-10-14 04:50:02

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

Re: Rigid body simulation in JS — work in progress

Bump. I got particles and springs working on Saturday, so I can now model a chain hanging from a point or something, it's really, really cool.  big_smile


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

Offline

 

#8 2012-10-14 05:10:38

slinger
Scratcher
Registered: 2011-06-21
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

That sounds pretty sweet!


http://s0.bcbits.com/img/buttons/bandcamp_130x27_blue.png

Offline

 

#9 2012-10-14 05:23:30

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

Springs? Pushing or pulling?


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#10 2012-10-14 05:40:46

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

Re: Rigid body simulation in JS — work in progress

Springs do both...  hmm


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

Offline

 

#11 2012-10-14 05:46:31

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

I meant your programmed one. It can do both?


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#12 2012-10-14 05:54:19

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

Re: Rigid body simulation in JS — work in progress

Yeah, it can. It uses Hooke's law to simulate the forces.  wink


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

Offline

 

#13 2012-10-15 07:35:24

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

Re: Rigid body simulation in JS — work in progress

Darn! I broke my beautiful chain sim... I really need to start using version control. I think I'm gonna ask for this to be moved to the ATs, this seems to be in the wrong place.  hmm


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

Offline

 

#14 2012-10-15 07:38:13

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

I wanted to know because then,you can simulate suspension in cars.


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#15 2012-10-15 08:03:22

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

Re: Rigid body simulation in JS — work in progress

Yeah, I can actually do that already! And I can also simulate World of Goo style towers, though if I mess up the balance by 0.001 or something the whole thing goes crazy.

Not very user-friendly...  tongue

P.S. Got the chain working again  big_smile

Code:

    var w = new KMWorld();
    w.gravity = new KMVector(0,-0.05);
    var b = new KMBody(0,600,1);
    b.fixed = true;
    var prev = b;
    for (var i=1; i<100; i++) {
        var t = new KMBody(i*(960/100), 600, 1);
        t.ghost = true;
        w.addBody(t);
        var s = new KMSpring(t, prev, 0.5, 10, 0.15);
        prev = t;
        w.addBody(s);
    }
    prev.fixed=true;

    animate(function() {
            w.step();
            can.clearRect(0,0,960,720);
            w.drawOn(can);
    },1);

My springs were too short  tongue

Last edited by Hardmath123 (2012-10-15 08:03:48)


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

Offline

 

#16 2012-10-15 13:39:51

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

Re: Rigid body simulation in JS — work in progress

Very nice!  smile  I saw the topic title, and I knew this would be you  tongue  This would be very useful for something I'm working on, too...

Springs are cool — though I found I couldn't use them to model collisions between a box and the floor, for example. The collision was too, uh, springy.

Have you implemented collision normals? I found some code for reflecting a vector in another somewhere, which is really useful. I imagine you've already done that, though...  smile

Edit: typo

Last edited by blob8108 (2012-10-15 13:40:17)


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

Offline

 

#17 2012-10-15 14:00:30

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: Rigid body simulation in JS — work in progress

Hardmath123 wrote:

Darn! I broke my beautiful chain sim... I really need to start using version control. I think I'm gonna ask for this to be moved to the ATs, this seems to be in the wrong place.  hmm

lol welcome to club  smile

Offline

 

#18 2012-10-15 15:25:40

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

Re: Rigid body simulation in JS — work in progress

Could you post the code? I might use it in a little something for tomorrow morning...


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

Offline

 

#19 2012-10-16 05:26:46

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

Re: Rigid body simulation in JS — work in progress

blob8108 wrote:

Very nice!  smile  I saw the topic title, and I knew this would be you  tongue  This would be very useful for something I'm working on, too...

Springs are cool — though I found I couldn't use them to model collisions between a box and the floor, for example. The collision was too, uh, springy.

Have you implemented collision normals? I found some code for reflecting a vector in another somewhere, which is really useful. I imagine you've already done that, though...  smile

Edit: typo

Thanks!

Yeah, I got vector normals and projection working. Nothing a piece of paper and pencil can't help you figure out if you give it enough time.  wink

What do you need the code for? It's not nearly done yet, I just have particles, springs, gravity, and particle collisions implemented. No walls yet. Also, the code's really ugly and uncommented...  hmm


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

Offline

 

#20 2012-10-16 05:59:01

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

Re: Rigid body simulation in JS — work in progress

Hardmath123 wrote:

blob8108 wrote:

Very nice!  smile  I saw the topic title, and I knew this would be you  tongue  This would be very useful for something I'm working on, too...

Springs are cool — though I found I couldn't use them to model collisions between a box and the floor, for example. The collision was too, uh, springy.

Have you implemented collision normals? I found some code for reflecting a vector in another somewhere, which is really useful. I imagine you've already done that, though...  smile

Edit: typo

Thanks!

Yeah, I got vector normals and projection working. Nothing a piece of paper and pencil can't help you figure out if you give it enough time.  wink

Awesome! Sounds like you're doing better than me...  tongue

What do you need the code for? It's not nearly done yet, I just have particles, springs, gravity, and particle collisions implemented. No walls yet. Also, the code's really ugly and uncommented...  hmm

Nevermind, I was wondering if I could use it in a stupid thing for someone... a little simulation of a ballooon on a string, or something, which sounds easy enough since you have springs. I've done the thing now, anyway, so it's fine now  smile  Although I would still like to see your code, just out of interest.


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

Offline

 

#21 2012-10-16 06:20:09

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

Re: Rigid body simulation in JS — work in progress

Maybe I'll release the code in a couple of days, I want to clean it up. I got particle-to-particle collisions working nice right now, but for some reason it adds energy to the system... gotta mess with that equation a bit more.  tongue

I need a good name for it. Currently the name is "Kinimate" so that I can prefix my objects with "KM". Suggestions?


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

Offline

 

#22 2012-10-17 20:39:04

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Rigid body simulation in JS — work in progress

Sounds awesome.  I'm just commenting here so that I can keep track of the thread.  Perhaps I'll post some code if I can get rigid body physics working this weekend.

Offline

 

#23 2012-10-18 05:18:54

s_federici
Scratcher
Registered: 2007-12-18
Posts: 500+

Re: Rigid body simulation in JS — work in progress

Very interesting. I'm bumping this topic.

Offline

 

#24 2012-10-18 05:42:56

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

Re: Rigid body simulation in JS — work in progress

Thanks, amcerbu and s_frederici. I have the following progress:
• Particles:
- Position, acceleration, gravity
- Collisions glitchy
• Springs:
- Stiffness
- Damping
- Need better drawing, currently renders a thin black line.
• Walls:
- Collisions unimplemented
- Everything else unimplemented, too, actually  tongue

Amcerbu, I kind of disagree with your collision explanation on your gdocs. You say the relevant vectors will be exchanged, but that's not true. Imagine a bowling ball colliding with a peanut: the bowling ball will continue to move in the sam direction with some deceleration, it will not inherit the peanut's movement vector.


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

Offline

 

#25 2012-10-18 09:20:13

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Rigid body simulation in JS — work in progress

Yeah, I'm not exactly sure how to model interactions between objects of different masses.  I assume a peanut/bowling ball collision is nonelastic, so it wouldn't be an exact exchange to begin with, but you're right that the peanut also wouldn't receive a significant fraction the bowling ball's momentum.

Offline

 

Board footer