//################################################################################################################### /// <summary> /// Ray hit test in space from 2D coordinate of a camera field of view. /// </summary> /// <param name="camera"> the camera where the hit test start, UrhoSharp CullingCamera recommanded </param> /// <param name="x"> ray position on x axis from left up corner of camera view, normalised value </param> /// <param name="y"> ray position on y axis from left up corner of camera view, normalised value </param> /// <returns> ray hit test information. If no collision detected is detected, return null </returns> private RayQueryResult?RayHit(Camera camera, float x, float y) { Ray cameraRay = CullingCamera.GetScreenRay(x, y); // ray coresponding to center of field of view // hit test using the previous ray var result = Scene.GetComponent <Octree>().RaycastSingle(cameraRay, RayQueryLevel.Triangle, 100, DrawableFlags.Geometry, 0x70000000); return(result); }
//################################################################################################################### protected override void OnUpdate(float timeStep) { Ray cameraRay = CullingCamera.GetScreenRay(0.5f, 0.5f); // ray coresponding to center of field of view // hit test using the previous ray var result = Scene.GetComponent <Octree>().RaycastSingle(cameraRay, RayQueryLevel.Triangle, 100, DrawableFlags.Geometry, 0x70000000); if (result != null) // if the ray hit something { // display green sphere at ray hit position sphere.Node.Position = result.Value.Position; sphere.Color = Color.Green; } else { // display red sphere at max range of ray hit test sphere.Node.Position = CullingCamera.Node.Position + CullingCamera.Node.Rotation * new Vector3(0, 0, 5); sphere.Color = Color.Red; } }