20.06.2013, 06:17
Let's say your beam has a x, y, and z lenght (also called vector), and since vectors are freely movable, you need to define your current position, by creating a linear function made of vectors.
(my x, my y, my z) + s * (lenght x, lenght y, lenght z)
Now this function tells you that you have a three dimensional finite line, fixed at some position in a coordinates system, and it is stretchable. Making s infinite makes the whole line infinite long, anyway.
Try to handle some object as a sphere, and since a sphere has a center point and on every side the same radius, you can determine the closest approach of your "beam" to the single point (center of the sphere).
Now we have to allocate, where the vector from the center of the sphere points meets our beam function and what direction it has to go to meet actually our beam function.
We can create a line from our position to the center of the sphere (object) and define it as a vector and use the first vector we know from our beam function, we can create a normalized vector using 2 vectors in a three dimensional space (vector cross product). Now we create a normalized vector using our generated normalized vector and the vector known from our beam function.
Great, this vector now can be transformed into a line function, giving it the start position at the center of the sphere and creating an equation to detect how long your line from the center of the sphere to our beam function has to be, to cross it. The now known variable "s" can be used to calculate the vector, using only the line function from the center of the sphere to the cross point of our beam function, throwing away the starting position values and have a proper vector, which describes how long it is and what direction it goes. We can simply calculate its real lenght using Pythagora's equation sqrt(aІ + bІ + cІ) = d and you will get the actual closest distance from the center of the sphere to the closest approach. If your result equals or is smaller than your sphere radius size, your beam crosses your sphere (hit) else it does not (no hit).
Edit: There is a much faster way to compare sizes without using square root at all,
ResultІ should equal or be smaller than radiusІ to detect it as a hit.
resultІ <= radiusІ
instead
sqrt(resultІ) <= radius
(my x, my y, my z) + s * (lenght x, lenght y, lenght z)
Now this function tells you that you have a three dimensional finite line, fixed at some position in a coordinates system, and it is stretchable. Making s infinite makes the whole line infinite long, anyway.
Try to handle some object as a sphere, and since a sphere has a center point and on every side the same radius, you can determine the closest approach of your "beam" to the single point (center of the sphere).
Now we have to allocate, where the vector from the center of the sphere points meets our beam function and what direction it has to go to meet actually our beam function.
We can create a line from our position to the center of the sphere (object) and define it as a vector and use the first vector we know from our beam function, we can create a normalized vector using 2 vectors in a three dimensional space (vector cross product). Now we create a normalized vector using our generated normalized vector and the vector known from our beam function.
Great, this vector now can be transformed into a line function, giving it the start position at the center of the sphere and creating an equation to detect how long your line from the center of the sphere to our beam function has to be, to cross it. The now known variable "s" can be used to calculate the vector, using only the line function from the center of the sphere to the cross point of our beam function, throwing away the starting position values and have a proper vector, which describes how long it is and what direction it goes. We can simply calculate its real lenght using Pythagora's equation sqrt(aІ + bІ + cІ) = d and you will get the actual closest distance from the center of the sphere to the closest approach. If your result equals or is smaller than your sphere radius size, your beam crosses your sphere (hit) else it does not (no hit).
Edit: There is a much faster way to compare sizes without using square root at all,
ResultІ should equal or be smaller than radiusІ to detect it as a hit.
resultІ <= radiusІ
instead
sqrt(resultІ) <= radius