Ray.Intersect
bool Intersect(Plane plane, Vec3& at)
Checks the intersection of this ray with a plane!
Plane plane | Any plane you want to intersect with. |
Vec3& at | An out parameter that will hold the intersection point. If there’s no intersection, this will be (0,0,0). |
RETURNS: bool | True if there’s an intersection, false if not. Refer to the ‘at’ parameter for intersection information! |
bool Intersect(Sphere sphere, Vec3& at)
Checks the intersection of this ray with a sphere!
Sphere sphere | Any Sphere you want to intersect with. |
Vec3& at | An out parameter that will hold the closest intersection point to the ray’s origin. If there’s no intersection, this will be (0,0,0). |
RETURNS: bool | True if intersection occurs, false if it doesn’t. Refer to the ‘at’ parameter for intersection information! |
bool Intersect(Bounds bounds, Vec3& at)
Checks the intersection of this ray with a bounding box!
Bounds bounds | Any Bounds you want to intersect with. |
Vec3& at | If the return is true, this point will be the closest intersection point to the origin of the Ray. If there’s no intersection, this will be (0,0,0). |
RETURNS: bool | True if intersection occurs, false if it doesn’t. Refer to the ‘at’ parameter for intersection information! |
bool Intersect(Mesh mesh, Ray& modelSpaceAt, Cull cullFaces)
Checks the intersection point of this ray and a Mesh with collision data stored on the CPU. A mesh without collision data will always return false. Ray must be in model space, intersection point will be in model space too. You can use the inverse of the mesh’s world transform matrix to bring the ray into model space, see the example in the docs!
Mesh mesh | A mesh containing collision data on the CPU. You can check this with Mesh.KeepData. |
Ray& modelSpaceAt | The intersection point and surface direction of the ray and the mesh, if an intersection occurs. This is in model space, and must be transformed back into world space later. Direction is not guaranteed to be normalized, especially if your own model->world transform contains scale/skew in it. |
RETURNS: bool | True if an intersection occurs, false otherwise! |
bool Intersect(Mesh mesh, Ray& modelSpaceAt, UInt32& outStartInds, Cull cullFaces)
Checks the intersection point of this ray and a Mesh with collision data stored on the CPU. A mesh without collision data will always return false. Ray must be in model space, intersection point will be in model space too. You can use the inverse of the mesh’s world transform matrix to bring the ray into model space, see the example in the docs!
Mesh mesh | A mesh containing collision data on the CPU. You can check this with Mesh.KeepData. |
Ray& modelSpaceAt | The intersection point and surface direction of the ray and the mesh, if an intersection occurs. This is in model space, and must be transformed back into world space later. Direction is not guaranteed to be normalized, especially if your own model->world transform contains scale/skew in it. |
UInt32& outStartInds | The index of the first index of the triangle that was hit |
Cull cullFaces | How should intersection work with respect to the direction the triangles are facing? Should we skip triangles that are facing away from the ray, or don’t skip anything? |
RETURNS: bool | True if an intersection occurs, false otherwise! |
bool Intersect(Mesh mesh, Vec3& modelSpaceAt)
Checks the intersection point of this ray and a Mesh with collision data stored on the CPU. A mesh without collision data will always return false. Ray must be in model space, intersection point will be in model space too. You can use the inverse of the mesh’s world transform matrix to bring the point into model space, see the example in the docs!
Mesh mesh | A mesh containing collision data on the CPU. You can check this with Mesh.KeepData. |
Vec3& modelSpaceAt | The intersection point of the ray and the mesh, if an intersection occurs. This is in model space, and must be transformed back into world space later. |
RETURNS: bool | True if an intersection occurs, false otherwise! |
bool Intersect(Model model, Ray& modelSpaceAt, Cull cullFaces)
Checks the intersection point of this ray and the Solid flagged Meshes in the Model’s visual nodes. Ray must be in model space, intersection point will be in model space too. You can use the inverse of the mesh’s world transform matrix to bring the ray into model space, see the example in the docs!
Model model | Any Model that may or may not contain Solid flagged nodes, and Meshes with collision data. |
Ray& modelSpaceAt | The intersection point and surface direction of the ray and the mesh, if an intersection occurs. This is in model space, and must be transformed back into world space later. Direction is not guaranteed to be normalized, especially if your own model->world transform contains scale/skew in it. |
Cull cullFaces | How should intersection work with respect to the direction the triangles are facing? Should we skip triangles that are facing away from the ray, or don’t skip anything? |
RETURNS: bool | True if an intersection occurs, false otherwise! |
Examples
Ray Mesh Intersection
Here’s an example of casting a Ray at a mesh someplace in world space, transforming it into model space, calculating the intersection point, and displaying it back in world space.
Mesh sphereMesh = Default.MeshSphere;
Mesh boxMesh = Mesh.GenerateRoundedCube(Vec3.One*0.2f, 0.05f);
Pose boxPose = (Demo.contentPose * Matrix.T(0, -0.1f, 0)).Pose;
Pose castPose = (Demo.contentPose * Matrix.T(0.25f, 0.11f, 0.2f)).Pose;
public void StepRayMesh()
{
// Draw our setup, and make the visuals grab/moveable!
UI.Handle("Box", ref boxPose, boxMesh.Bounds);
UI.Handle("Cast", ref castPose, sphereMesh.Bounds*0.03f);
boxMesh .Draw(Default.MaterialUI, boxPose .ToMatrix());
sphereMesh.Draw(Default.MaterialUI, castPose.ToMatrix(0.03f));
Lines.Add(castPose.position, boxPose.position, Color.White, 0.005f);
// Create a ray that's in the Mesh's model space
Matrix transform = boxPose.ToMatrix();
Ray ray = transform
.Inverse
.Transform(Ray.FromTo(castPose.position, boxPose.position));
// Draw a sphere at the intersection point, if the ray intersects
// with the mesh.
if (ray.Intersect(boxMesh, out Ray at, out uint index))
{
sphereMesh.Draw(Default.Material, Matrix.TS(transform.Transform(at.position), 0.01f));
if (boxMesh.GetTriangle(index, out Vertex a, out Vertex b, out Vertex c))
{
Vec3 aPt = transform.Transform(a.pos);
Vec3 bPt = transform.Transform(b.pos);
Vec3 cPt = transform.Transform(c.pos);
Lines.Add(aPt, bPt, new Color32(0,255,0,255), 0.005f);
Lines.Add(bPt, cPt, new Color32(0,255,0,255), 0.005f);
Lines.Add(cPt, aPt, new Color32(0,255,0,255), 0.005f);
}
}
}
Found an issue with these docs, or have some additional questions? Create an Issue on Github!