public bool Raycast(FRay ray, out float closestDistance) { // Convert the ray to local space of this node since all of our raycasts are local. FRay localRay = WMath.TransformRay(ray, Transform.Position, Transform.LocalScale, Transform.Rotation.Inverted()); bool bHit = false; if (m_actorMesh != null) { bHit = m_actorMesh.Raycast(localRay, out closestDistance, true); } else { bHit = WMath.RayIntersectsAABB(localRay, m_objRender.GetAABB().Min, m_objRender.GetAABB().Max, out closestDistance); } if (bHit) { // Convert the hit point back to world space... Vector3 localHitPoint = localRay.Origin + (localRay.Direction * closestDistance); localHitPoint = Vector3.Transform(localHitPoint + Transform.Position, Transform.Rotation); // Now get the distance from the original ray origin and the new worldspace hit point. closestDistance = (localHitPoint - ray.Origin).Length; } return(bHit); }
public bool Raycast(FRay ray, out float closestDistance) { // Convert the ray to local space of this node since all of our raycasts are local. FRay localRay; if (DisableRotationAndScaleForRaycasting) { localRay = WMath.TransformRay(ray, Transform.Position, Vector3.One, Quaternion.Identity); } else { localRay = WMath.TransformRay(ray, Transform.Position, VisualScale, Transform.Rotation.Inverted().ToSinglePrecision()); } closestDistance = float.MaxValue; bool bHit = false; if (m_actorMeshes.Count > 0) { foreach (var actor_mesh in m_actorMeshes) { bHit = actor_mesh.Raycast(localRay, out closestDistance, true); if (bHit) { break; } } } else if (m_objRender != null) { if (m_objRender.FaceCullingEnabled && m_objRender.GetAABB().Contains(localRay.Origin)) { // If the camera is inside an OBJ render that has backface culling on, the actor won't actually be visible, so don't select it. return(false); } bHit = WMath.RayIntersectsAABB(localRay, m_objRender.GetAABB().Min, m_objRender.GetAABB().Max, out closestDistance); if (bHit) { // Convert the hit point back to world space... Vector3 localHitPoint = localRay.Origin + (localRay.Direction * closestDistance); Vector3 globalHitPoint = Transform.Position + Vector3.Transform(localHitPoint, Transform.Rotation.ToSinglePrecision()); // Now get the distance from the original ray origin and the new worldspace hit point. closestDistance = (globalHitPoint - ray.Origin).Length; } } return(bHit); }