2D, Ball and Line distance problem

Started by
0 comments, last by Aardvajk 10 years ago

Hi guys,

Here's my problem...

I have a ball constructor Ball(x,y) and a line constructor Plane(x0,y0,x1,y1)

The norm of the line is defined as dx=x1-x0, and dy=y1-y0, where the normal vector is either norm(-dx,dy) or norm(dx,-dy)

In order the gauge the distance from the ball at some arbitrary position to the line/plane at some arbitrar position, I use the followin funcition:


float Plane::distToPlane(Ball &ball){
	float dot_prod=(ball.getCricle().position.dot(normal));
	return (dot_prod);
}

Everything works fine except the float that's returned I can't use. What I really need, is a as the ball is approaching the line, the distance get's closer and closer to zero, and right now it's not doing that.

I need to be able to do this check for any arbitrary line/ball position:

if(distance<0)//do this

I guess what I am really trying to say is that I need this....


float Plane::distToPlane(Ball &ball){
	float dot_prod=(ball.getCricle().position.dot(normal)+SOME_OFFSET);
	return (dot_prod);
}

Where the offset will always close the dot product to zero no matter where the ball and the line start.

I have been trying many different things but can't grasp the mathematical relationship.

If anyone has any idea on how to approach the problem, please let me know.

Thanks,

MIke

Advertisement
You need to find the closest point on the line to the ball, then you can take the length of the vector from that point to the ball.

Google 2D closest point on line to point or something. I don't have the code to hand. It's basically the projection of the ball pos onto the line.

This topic is closed to new replies.

Advertisement