2d ballpen physics

Started by
23 comments, last by xytor 12 years, 2 months ago
You know those ball pens that some fast food playgrounds have? I want to program a 2d simulation of a bunch of balls inside of a convex hull with gravity. That's it.

I've been trying to hack it together, but those little guys just don't want to stand still! They bounce around, and whenever I try to make them stack up on the bottom, they either bounce more, jitter like crazy or just ghost through each other.

This wouldn't happen to be an easy problem that I am just missing, would it?
Advertisement
How are you handling the collisions between two balls and a ball and a wall? If you are treating the collisions as elastic, then it should be expected that the balls will continue moving.

How are you handling the collisions between two balls and a ball and a wall? If you are treating the collisions as elastic, then it should be expected that the balls will continue moving.


Well, if it intersects the wall, I reset the ball's position to just touch it barely, and I give it a force with the direction equal to the wall normal vector and a magnitude of 1.99 * the ball's velocity. If two balls collide it's pretty much the same thing, but the magnitude is simply the vector between the two ball's position.
try this: http://www.wildbunny.co.uk/blog/2011/03/25/speculative-contacts-an-continuous-collision-engine-approach-part-1/

try this: http://www.wildbunny...pproach-part-1/


That method is impressive! Thank you! My only concern is dealing with the conservation of velocity(momentum) and bouncing.

try this: http://www.wildbunny...pproach-part-1/


Huh, I was just about to post that! But yes, speculative contacts work really well with spheres/circles - the stability is remarkable. :)
Hmm... Trying to adapt this method to my simple simulation has been troublesome. I don't have impulses or masses... Just force, velocity and position. Don't suppose there's any stable way of making it work? Or do I have to have a full-blown physics engine?
It doesn't have to be full blown, but it should at least have mass. otherwise there is no translation between the force and velocity. The mass can be assumed to be 1 and all the calculations are the same. But i would suggest incorporating mass so that you can adjust the interaction between objects as your simulation becomes more advanced.
Gah, it's still unstable... considering using some other method other than naive frame-based euler integration (my time step is currently implicitly a frame -- always 1/30th of a second).
It seems all the time I'm putting into this to make it a "quick" "hacked together" sim could be better spent just making a real physics engine -.-

Edit: Ok, trying out verlet... things seem to be working relatively well, except my derived velocity vector is inaccurate. It's the correct direction but way bigger. What am I doing wrong here?

ball["vel"].x = ((ball["pos"].x - ball["prevPos"].x) / deltaT) + (0.5 * ball["force"].x * inverseBallMass * deltaT);
ball["vel"].y = ((ball["pos"].y - ball["prevPos"].y) / deltaT) + (0.5 * ball["force"].y * inverseBallMass * deltaT);
There's nothing wrong with using a fixed time step (1/30s for example). Euler is very unstable without a fixed timestep.

My guess is that you dont need the + term in those equations.

dX/dt would be everything just up until the +'s.
vel = (newpos - oldPos) / dt;

I've never used verlet.

Also, you should make a vector class, with operators. This way you wont have to keep doing component wise math everywhere, leading to mistakes.

This topic is closed to new replies.

Advertisement