Beispiel #1
0
        private bool GetScreenspaceCoords(MovableObject mo, Camera camera, out Vector2 result)
        {
            result = Vector2.ZERO;

            //if (!mo.IsInScene)
            //{
            //    return false;
            //}

            var optimizedAabb = mo.WorldAabbUpdated;
            var aabb          = new AxisAlignedBox(optimizedAabb.Minimum, optimizedAabb.Maximum);

            var point = this.offset
                        + (aabb.GetCorner(AxisAlignedBox.CornerEnum.FAR_LEFT_BOTTOM)
                           + aabb.GetCorner(AxisAlignedBox.CornerEnum.FAR_RIGHT_BOTTOM)
                           + aabb.GetCorner(AxisAlignedBox.CornerEnum.NEAR_LEFT_BOTTOM)
                           + aabb.GetCorner(AxisAlignedBox.CornerEnum.NEAR_RIGHT_BOTTOM)) / 4;

            // Is the camera facing that point? If not, return false
            var cameraPlane = new Plane(camera.DerivedOrientation.ZAxis, camera.DerivedPosition);

            if (cameraPlane.GetSide(point) != Plane.Side.NEGATIVE_SIDE)
            {
                return(false);
            }

            // Transform the 3D point into screen space
            point = camera.ProjectionMatrix * (camera.ViewMatrix * point);

            // Transform from coordinate space [-1, 1] to [0, 1] and update in-value
            result.x = (point.x / 2) + 0.5f;
            result.y = 1 - ((point.y / 2) + 0.5f);

            return(true);
        }