Jump to content
  • Advertisement
Sign in to follow this  
too_many_stars

2D, Ball and Line distance problem

This topic is 2014 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!