# 2d ballpen physics

This topic is 2517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites
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?

##### Share on other sites
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.

##### Share on other sites
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);

##### Share on other sites
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.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633769
• Total Posts
3013759
×