Ball into hole physics

Official forum for the Chipmunk2D Physics Library.
Post Reply
drbellavance
Posts: 1
Joined: Mon Nov 16, 2009 11:07 am
Contact:

Ball into hole physics

Post by drbellavance »

I am a total noob at this so please bare with me. I have a ball that is rolling around on a surface that has holes in it. I've figured out all the calls and modifications to get the ball to roll as a function of the gravity vector which is close to perpendicular to the surface ( this is an iPhone app so the accelerometer is used to adjust the gravity vector per the tilt of the "surface". I am trying to figure out a way to make the ball behave very life like when the ball reaches the edge of the hole. The physics should be life like enough so that if the ball "lips" the hole, the ball will change its vector (dependent on gravity vector and ball velocity). If the ball gets close enough to the center of the hole I need to change it so that the ball image gets smaller as it "disappears" down the hole.

Any help, suggested reading materials, code examples, or tutorials would be greatly appreciated.

Thanks in advance.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Ball into hole physics

Post by slembcke »

The easiest way to do it would be to calculate a force to apply to the ball based on it's distance from the hole. You could calculate a physically correct force based on gravity and the curvature of the ball, but I think just a force that increases linearly based on the distance will look just fine.

Code: Select all

cpVect delta = cpvsub(ball_pos, hole_pos);
cpFloat dist = cpvlength(delta);
if(dist < hole_radius){
  if(dist < hole_radius - ball_radius){
    // ball has fallen into hole, run an animation or something
  } else {
    cpVect ballForce = cpvmult(delta, force_adjustment*(hole_radius - dist)/dist);
    ball->f = cpvadd(ball->f, ballForce);
  }
}
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests