Big:
You don't need to rotate a vector for this either. :-)
You don't need to rotate a vector for this either. :-)
' Calculate normal which points from center of ball to center of hole. Nx# = Ball_X#-Hole_X# Ny# = Ball_Y#-Hole_Y# D# = Sqr(Nx#*Nx# + Ny#*Ny#) Nx# = Nx# / D# Ny# = Ny# / D# ' Now this is where it gets tricky. I'm not sure how much force this vector should have, but I'm gonna take a wild guess from what I remember about my ball bouncing code, and say it's 2x the velocity of the ball. ' Why 2x? Consider what would happen in a completely elastic collision if a ball struck the opposite side of the hole after passing over it quickly. It would change direction 180 degrees. If you only added 1x the force in the opposite direction the ball would simply stop moving. We need it to dstart moving in the opposite direction, so we add 2x the force. ' But even though that's true of bounces, I'm just guessing that this is the right amount of force to apply to this case. I think it's right though. A roll around an edge is like millions of tiny bounces. ' Calculate force vector pointing toward center of hole. Ball_Velocity# = Sqr(Ball_Vx#*Ball_Vx# + Ball_Vy#*BallVy#) HoleForce_Vx# = Nx# * Ball_Velocity#*2.0 HoleForce_Vy# = Ny# * Ball_Velocity#*2.0 ' Scale force vector by Tween# we calculated earlier, so ball is pushed towards the center of the hole less when it's only over the edge slightly. HoleForce_Vx# = HoleForce_Vx# * Tween# HoleForce_Vy# = HoleForce_Vy# * Tween# ' Add force pointing toward center of hole to balls velocity vector. Add less force if less time has passed this frame. Ball_Vx# = Ball_Vx# + HoleForce_Vx#*Seconds# Ball_Vy# = Ball_Vy# + HoleForce_Vy#*Seconds# ' Adjust ball's position according to it's final velocity and the amount of time passed this frame. Ball_X = Ball_X + Ball_Vx*Seconds# Ball_Y = Ball_Y + Ball_Vy*Seconds#