Staredit Network > Forums > Null > Topic: Finding where a ray intercects a sphere: How?
Finding where a ray intercects a sphere: How?
Apr 7 2010, 2:21 pm
By: Biophysicist  

Apr 7 2010, 2:21 pm Biophysicist Post #1



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.

Apr 7 2010, 2:56 pm NudeRaider Post #2

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

Does this help?
http://en.wikipedia.org/wiki/Ray_tracing_(graphics)#Example




Apr 7 2010, 3:25 pm Biophysicist Post #3



Sadly, no. I am unfamiliar with some of the notation used. I'm not as advanced in math as the writer, apparently.



None.

Apr 7 2010, 3:33 pm NudeRaider Post #4

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. :P




Apr 7 2010, 3:39 pm poiuy_qwert Post #5

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).




Apr 7 2010, 3:49 pm NudeRaider Post #6

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.




Apr 7 2010, 3:51 pm Biophysicist Post #7



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.

Apr 7 2010, 3:54 pm NudeRaider Post #8

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)




Apr 7 2010, 3:58 pm Biophysicist Post #9



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.

Apr 7 2010, 4:03 pm NudeRaider Post #10

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).




Apr 7 2010, 4:08 pm Biophysicist Post #11



Quote from NudeRaider
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.

Apr 7 2010, 4:11 pm NudeRaider Post #12

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
Quote from NudeRaider
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.




Apr 7 2010, 4:13 pm dumbducky Post #13



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

Apr 7 2010, 4:16 pm Biophysicist Post #14



Quote
What rotations?
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.

Apr 7 2010, 4:21 pm NudeRaider Post #15

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. :P

EDIT:
But since I'm a nice guy: http://www.teacherschoice.com.au/Maths_Library/Coordinates/polar_-_rectangular_conversion.htm ;)




Apr 7 2010, 4:34 pm Biophysicist Post #16



Quote
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.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[01:05 am]
Vrael -- I won't stand for people going around saying things like im not a total madman
[01:05 am]
Vrael -- that's better
[12:39 am]
NudeRaider -- can confirm, Vrael is a total madman
[10:18 pm]
Vrael -- who says I'm not a total madman?
[02:26 pm]
UndeadStar -- Vrael, since the ad messages get removed, you look like a total madman for someone that come late
[2024-5-02. : 1:19 pm]
Vrael -- IM GONNA MANUFACTURE SOME SPORTBALL EQUIPMENT WHERE THE SUN DONT SHINE BOY
[2024-5-02. : 1:35 am]
Ultraviolet -- Vrael
Vrael shouted: NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
Gonna put deez sportballs in your mouth
[2024-5-01. : 1:24 pm]
Vrael -- NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
[2024-4-30. : 5:08 pm]
Oh_Man -- https://youtu.be/lGxUOgfmUCQ
[2024-4-30. : 7:43 am]
NudeRaider -- Vrael
Vrael shouted: if you're gonna link that shit at least link some quality shit: https://www.youtube.com/watch?v=uUV3KvnvT-w
Yeah I'm not a big fan of Westernhagen either, Fanta vier much better! But they didn't drop the lyrics that fit the situation. Farty: Ich bin wieder hier; nobody: in meinem Revier; Me: war nie wirklich weg
Please log in to shout.


Members Online: Oh_Man, Roy