/// <summary> /// Create a JMousePicker instance. JMousePicker will project a 2D screen coordinate into a 3D unit vector within the world. /// It can determine intersection with terrain as well as check for intersection with a JBoundingSphere. /// </summary> /// <param name="camera">The JCamera entity.</param> /// <param name="projectionMatrix">The JMasterRenderers Projection Matrix based off of the JCameras properties.</param> /// <param name="terrain">The JTerrain the Mouse Ray will intersect with.</param> /// <param name="gameWindow">The active JGameWindow. We need this to get it's height and width.</param> public JMousePicker(JCamera camera, Matrix4 projectionMatrix, JPerlinTerrain terrain, JGameWindow gameWindow) { Camera = camera; ProjectionMatrix = projectionMatrix; ViewMatrix = JMathUtils.createViewMatrix(camera); Terrain = terrain; GameWindow = gameWindow; }
/// <summary> /// Update CurrentRay each frame from the current 2D mouse coordinate. Also updates CurrentTerrainPoint reflecting CurrentRays intersection with /// Terrain. /// </summary> public void Update() { ViewMatrix = JMathUtils.createViewMatrix(Camera); CurrentRay = CalculateMouseRay(); if (IntersectionInRange(0, JConfig.MOUSE_PICKER_RAY_RANGE, CurrentRay)) { CurrentTerrainPoint = BinarySearch(0, 0, JConfig.MOUSE_PICKER_RAY_RANGE, CurrentRay); } else { CurrentTerrainPoint = new Vector3(0, 0, 0); } }