• Create Account

## 2D, Ball and Line distance problem

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1too_many_stars  Members

320
Like
0Likes
Like

Posted 11 April 2014 - 12:46 PM

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

### #2Aardvajk  Members

12252
Like
1Likes
Like

Posted 11 April 2014 - 01:13 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.