Skip to main content
GameDev.net gamedev.net
🔒 Locked

segment point distance......

Started by xissburg Jan 30, 2006 at 7:05 PM 8 replies 550+ views
Original Post
xissburg
xissburg
Having a straight segment S which goes form the point p0 to p1, a point p and the distance d, how can get the point on S which dists d from p?!?
.
Zipster
Zipster
Say you have a sphere with the center at p and a radius of d. The point(s) you're looking for are the intersections of this sphere and the segment S. Keep in mind that there can be either 0, 1, or 2 such points. You can get more information by searching for ray-sphere intersection material.
xissburg
xissburg
no it isn't that :P

I want to know the point on S which dists d from p, not the distance between S and p......
.
Zakwayda
Zakwayda
Quote:
Original post by xissburg
no it isn't that :P

I want to know the point on S which dists d from p, not the distance between S and p......
I think that's what Zipster was saying: the point(s) on a segment that are a given distance from a point are the same as the points of intersection between the segment and a circle/sphere with the point as its center. Is that not what you're looking for?
KidWithTheSquid
KidWithTheSquid
Just to expand a bit on what the other two folks have said - let's see it in 2-D.

Your equation for a line is trusty ol:

y = mx + b (where m is ( ( p1y - p0y ) / ( p1x - p0x ))

Your equation for the circle is:

(x - px)^2 + (y - py)^2 + (z - pz)^2 = d^2.

You've got the line in the form of y=, now you want to do the same with the circle. That's where they meet in the y-plane. Now do the same for the x-axis and you're all set!

In three dimensions, the equation for a line gets a little bit more complicated.

Is that what you're searching for? If not, maybe you can give us some further input. :)
Zakwayda
Zakwayda
Quote:
Original post by KidWithTheSquid
Just to expand a bit on what the other two folks have said - let's see it in 2-D.

Your equation for a line is trusty ol:

y = mx + b (where m is ( ( p1y - p0y ) / ( p1x - p0x ))
No, no, no! Don't use that method! It's practically useless in a game context due to the degeneracies and lack of generality. You do have the right idea about plugging the line equation into the circle/sphere equation; just use the parametric form for the line instead.
Quote:
In three dimensions, the equation for a line gets a little bit more complicated.
That's another reason to use the parametric form: it's exactly the same in 2d and 3d, so you can use (almost) exactly the same code for line-circle and line-sphere intersections.
xissburg
xissburg
hey! 1 billion sorry Zipster!!! I misunderstood what you said :P....sorry...it's that I'm trying out.

KidWithTheSquid, I know the method you showed up but it is not usefull for me cuz I want a segment-sphere intersection test(I'm working in a 3D environment).

and Jyk,
Quote:
Original post by jyk
You do have the right idea about plugging the line equation into the circle/sphere equation; just use the parametric form for the line instead.


Are you talking about catch the parametric equation for the line ( p(s) = p0 + s*(p1-p0) ) and plug with the sphere equation ( (x - px)^2 + (y - py)^2 + (z - pz)^2 = d^2) ?!?
.
Zakwayda
Zakwayda
Quote:
Original post by xissburg
Are you talking about catch the parametric equation for the line ( p(s) = p0 + s*(p1-p0) ) and plug with the sphere equation ( (x - px)^2 + (y - py)^2 + (z - pz)^2 = d^2) ?!?
Yup :)
daftasbrush
daftasbrush
Quote:
Original post by xissburg
Are you talking about catch the parametric equation for the line ( p(s) = p0 + s*(p1-p0) ) and plug with the sphere equation ( (x - px)^2 + (y - py)^2 + (z - pz)^2 = d^2) ?!?
Break the parameteric equation into its "components" (x,y,z)
For your original case.... (I've changed p(s) to c(s) to avoid confusion with your point p)
Quote:
c(s).x = A.x + s*(B.x-A.x)
c(s).y = A.y + s*(B.y-A.y)
c(s).z = A.z + s*(B.z-A.z)
Substitute x,y & z in the sphere equation for these.
You then end up with a Quadratic Equation in s
Which you can solve for s (using the standard Quadratic Solution)
That will get you the value(s) of s where the point on the line is a distance d from point p
You can get
No Real Roots.. which means the line misses the sphere
1 root.. the line is a tangent to the sphere (just touches it)
2 roots.. the "entry" and "exit" points.

Note: Roots s<0 or s>1 aren't on your segment, so you need to decide whether you want to consider them or not... it depends on what you're actually doing.

Drop the roots back into the parametric equation to get the actual coordinates.
KidWithTheSquid
KidWithTheSquid
Quote:
Original post by jyk
Quote:
Original post by KidWithTheSquid
Just to expand a bit on what the other two folks have said - let's see it in 2-D.

Your equation for a line is trusty ol:

y = mx + b (where m is ( ( p1y - p0y ) / ( p1x - p0x ))
No, no, no! Don't use that method! It's practically useless in a game context due to the degeneracies and lack of generality. You do have the right idea about plugging the line equation into the circle/sphere equation; just use the parametric form for the line instead.


Yeah, was just checking the maths out in 2-D to illustrate your point. 2D is just a specific case of 3D confined to a plane! So, OP, in regards to applicability to your 3-D segment-sphere problem, you need to generalize it. jyk & daftasbrush performed the generalization for you here. ...Took all the fun out of it! :( (JK)

P.S. I see part of the confusion is probably because I included the equation for a sphere rather than a circle. Sorry about that, guys!

Also, Xissburg - are you the one doing that billiards simulator? If so, is this for that? What sorta thing you have in mind for it, or is it hush-hush until you're done? ;)

[Edited by - KidWithTheSquid on January 31, 2006 6:13:15 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.