# Simply Collisions With Bezier Curves

## Recommended Posts

NickGravelyn    855
I'm currently trying to handle a simple circle particle collision (in 2D) with a bezier curve. I've implmented a good system for finding the closest point and a neighboring point (for computing the normal), and everything works so far, but after the ball settles, it just falls through the line into nothingness. Here's the code for computing everything. I'm using XNA so the Vector2 and Vector3 are built in classes that, I believe, are nearly identical to the DirectX versions.
			Vector2 d = p.position - closestPoint;
{
Vector2 normal = closestPoint - n1;
normal = new Vector2(normal.Y, -normal.X);
normal.Normalize();
p.ApplyForce(normal * (p.mass * 9.8f));
Vector3 reflect = Vector3.Reflect(new Vector3(p.velocity, 0f), new Vector3(normal, 0f));
p.velocity = new Vector2(reflect.X, reflect.Y) * .9f;
}

Any ideas?

##### Share on other sites
oliii    2196
I think it's just a classic example of numerical innaccuracy. When the particle settles, the velocity is very low, so the reflect does nothing really. You seem to cancel out the gravity, I'm not sure that is necessary.

What I would do, is push the particle away from the closest point.

Vector2 normal = p.position - closestPoint;float l = normal.Length();if (l < p.radius){    normal /= l;    p.position += normal * (p.radius - l);    p.velocity -= 2.0f * (p.velocity.DotProduct(normal)) * normal;}

##### Share on other sites
NickGravelyn    855
Thanks for the try. Your code makes the ball more jerky when reacting (probably from pushing the position) and it still falls through the ground. My first thought was that I wasn't taking small enough steps, but I'm breaking up the elapsed time into 300 pieces and iterating over 300 small time steps for the physics and it's still falling through. I have no idea why. It probably is when velocity is low like you said, but I don't understand. It falls cleanly through the ground with no interference.

So here's the total physics code for me in case anyone else has an idea:
Vector2 closestPoint, normal;			for (int i = 0; i < 300; i++)			{				p.ApplyForce(new Vector2(0f, gravity * p.mass));				b.GetClosestPoint(p.position, out closestPoint, out normal);				Vector2 d = p.position - closestPoint;				if (d.Length() < p.radius)				{					p.position = closestPoint + (normal * p.radius);					p.ApplyForce(normal * (p.mass * -gravity));					Vector3 reflect = Vector3.Reflect(new Vector3(p.velocity, 0f), new Vector3(normal, 0f));					p.velocity = new Vector2(reflect.X, reflect.Y) * .9f;				}				p.Update((float)gameTime.ElapsedGameTime.TotalSeconds / 300f);			}