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.
Offline
Would this have flexible joints and shapes and things like the Rigs of Rods engine?
Offline
Sounds cool. 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?
Offline
Yeah.
Offline
Sounds cool
Offline
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.
Offline
Springs? Pushing or pulling?
Offline
Springs do both...
Offline
I meant your programmed one. It can do both?
Offline
Yeah, it can. It uses Hooke's law to simulate the forces.
Offline
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.
Offline
I wanted to know because then,you can simulate suspension in cars.
Offline
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...
P.S. Got the chain working again
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
Last edited by Hardmath123 (2012-10-15 08:03:48)
Offline
Very nice! I saw the topic title, and I knew this would be you 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...
Edit: typo
Last edited by blob8108 (2012-10-15 13:40:17)
Offline
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.
lol welcome to club
Offline
Could you post the code? I might use it in a little something for tomorrow morning...
Offline
blob8108 wrote:
Very nice! I saw the topic title, and I knew this would be you 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...
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.
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...
Offline
Hardmath123 wrote:
blob8108 wrote:
Very nice! I saw the topic title, and I knew this would be you 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...
Edit: typoThanks!
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.
Awesome! Sounds like you're doing better than me...
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...
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 Although I would still like to see your code, just out of interest.
Offline
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.
I need a good name for it. Currently the name is "Kinimate" so that I can prefix my objects with "KM". Suggestions?
Offline
Very interesting. I'm bumping this topic.
Offline
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
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.
Offline
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