Quick question: How can I find where a ray (expressed as an endpoint and a direction) intersects with a sphere (expressed as an origin and a radius)? I need to do it algebraically (because it's for a game project and having the engine do it graphically would be absurd). If it matters, the endpoint of the ray will always be the same point as the origin of the sphere, both of which are at (0,0,0).
None.
We can't explain the universe, just describe it; and we don't know whether our theories are true, we just know they're not wrong. >Harald Lesch
Sadly, no. I am unfamiliar with some of the notation used. I'm not as advanced in math as the writer, apparently.
None.
We can't explain the universe, just describe it; and we don't know whether our theories are true, we just know they're not wrong. >Harald Lesch
What part do you not get? You need to ask questions if you want something explained.
PyMS and ProTRG developer
Just treat the ray as a vector with the length being the radius of the sphere. If the origin of the sphere is (0,0,0) then the intersecting point's coordinates will be that vectors components, if not just add the components to the origin (apply the vector to the origin).
We can't explain the universe, just describe it; and we don't know whether our theories are true, we just know they're not wrong. >Harald Lesch
Look at the resulting formula in the wiki:
-(v*d) +/- sqrt( (v*d)² - (v²-r²) )
solving the simplification
we get
-(s-c)*d +/- sqrt( (s-c)²*d² - ((s-c)²-r²) )
with
s = c (in your case) = starting point of the sphere and the ray
d = direction of the ray
r = radius of the sphere
-( (0,0,0) - (0,0,0) )*d +/- sqrt( 0 - (0²-r²) )
+/- sqrt (r²) = +/- r which is actually self-evident as you let the ray start at the same point where the sphere is.
So the point where they intersect is r*d. Remember d is the direction of the vector.
Basically the result is what poiuy said.
That makes somewhat more sense, but I don't see how to multiply the radius by a direction. What unit is the direction supposed to be in, anyway? Degrees and radians are only for two-dimensional space as far as I know...
And I'm really embarrassed to be asking such a stupid question and wasting your time. I'm sorry.
None.
We can't explain the universe, just describe it; and we don't know whether our theories are true, we just know they're not wrong. >Harald Lesch
You use a vector: (x, y, z)
Each of the 3 parts representing the direction in one axis.
Usually it's normalized so that at least 1 of the components is 1 and the rest is between -1 and 1.
Example: (0.5, 1, -0.75)
You know what? I give up. This isn't happening. I'm just too retarded.
And my idea is, as well.
Sorry for wasting your time.
EDIT: I've calmed down a bit. Allow me to explain my reasoning: It seems that I'd have to know in advance where they intersect so I can figure out that vector. If I just pick some arbitrary point and multiply it by the radius, that won't produce anything useful. Clearly, I'm missing something important, probably in that formula that I'm too retarded to understand.
None.
We can't explain the universe, just describe it; and we don't know whether our theories are true, we just know they're not wrong. >Harald Lesch
How is your ray defined? You have to know in which direction it goes if you want to intersect it with something...
Well actually either that or you need the ray's origin (given that the end point is known).
How is your ray defined? You have to know in which direction it goes if you want to intersect it with something...
It's represented by two rotations: One horizontal, the other vertical.
EDIT: And I think I misspoke a while ago. When I said "endpoint" of the ray, I might have misused the term. I'm referring to the point that the ray comes out of. What is the correct term for that?
And ignore my emo ramblings. I've calmed down and am willing to try again.
None.
We can't explain the universe, just describe it; and we don't know whether our theories are true, we just know they're not wrong. >Harald Lesch
Quote from name:TassadarZeratul
How is your ray defined? You have to know in which direction it goes if you want to intersect it with something...
It's represented by two rotations: One horizontal, the other vertical.
What rotations? I've never heard that term. Maybe a picture showing your ray in a coordinate grid would help.
You just need to do some studying of vectors. In a coordinate system where both the ray and the sphere share the same endpoint and origin (0, 0, 0), this is easy. The point of intersection will simply be the direction times the radius of the sphere, or rather the
unit vector of the vector times the radius of the sphere. The unit vector itself is a coordinate. So if the radius is 5cm, and you want to see where it intersects with a ray on the positive x-axis, then they will intersect at (5cm) *(1,0,0)=(5,0,0)
tits
I think I'll back up a bit to make this easier to explain. I'm trying to create a flight simulator (though an atypical one in that it has no graphics but instead relays the environment textually). The game world is represented by a horizontal plane, where, if you were to look straight down at the origin, the x-coordinate of something represents the left-right position, the y-coordinate represents the forwards-backwards position, and the z-coordinate represents how high above or far below the plane something is. The ray represents the direction of the player's ship. If the ship was at (0,0,0), the ray would represent how far vertically and horizontally the ship has rotated from facing straight ahead.
None.
We can't explain the universe, just describe it; and we don't know whether our theories are true, we just know they're not wrong. >Harald Lesch
So there's no rotation around the z axis? Then the last coordinate is always 0, which makes it a bit easier.
For the other 2 you just have to convert the angles into coordinates using sin/cos conversion. And before you ask: This can be googled easily.
EDIT:
But since I'm a nice guy:
http://www.teacherschoice.com.au/Maths_Library/Coordinates/polar_-_rectangular_conversion.htm
So there's no rotation around the z axis? Then the last coordinate is always 0, which makes it a bit easier.
Uhm, there is? Z = vertical (at least to me, because that's how it's used in the FreeSpace mission editor). Ships can rotate along both the x and z axes (but not the y axis, because that would serve no practical purpose).
@the rest of your post: Yes, it's Googlable if you possess a functioning brain. You seem to forget that I don't. But that link looks promising. Assuming I can find a way to apply it to my rather odd coordinate system (odd because I measured the headings from the y axis, not the x, and the directions are reversed - once again because that's how it works in the FS editor), which I'm pretty sure I can, I'll be able to get this working. Thanks.
None.