Jump to content
  • Advertisement
Sign in to follow this  
xytor

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!