Exemplo n.º 1
0
        private Vector3 raycastGroundPlaneCursor()
        {
            Ray     ray = game.GetWereldViewRay(new Vector2(game.Mouse.CursorPosition.X, game.Mouse.CursorPosition.Y));
            Vector3 pos = World.Raycast(ray);

            return(pos);
        }
        private TileFace RaycastTileFace(IXNAGame _game, WorldObject target)
        {
            var bb    = target.ObjectType.TileData.GetBoundingBox();
            var range = (bb.Max - bb.Min) * 0.5f;

            Ray ray;
            var targetMatrix = Matrix.Invert(target.WorldMatrix);

            ray           = _game.GetWereldViewRay(_game.Mouse.CursorPositionVector);
            ray.Position  = Vector3.Transform(ray.Position, targetMatrix);
            ray.Direction = Vector3.TransformNormal(ray.Direction, targetMatrix);

            raycasterFace.Reset();
            Vector3 normal;
            Vector3 up;
            Vector3 right;


            for (int i = 0; i < 6; i++)
            {
                var face = (TileFace)(i + 1);
                normal = TileSnapInformationBuilder.getFaceNormal(face);
                up     = TileSnapInformationBuilder.getFaceUp(face);
                right  = Vector3.Cross(up, normal);
                var scaledNormal = normal * Math.Abs(Vector3.Dot(normal, range));
                var scaledUp     = up * Math.Abs(Vector3.Dot(up, range));
                var scaledRight  = right * Math.Abs(Vector3.Dot(right, range));
                var v1           = scaledNormal + scaledUp +
                                   scaledRight;
                var v2 = scaledNormal + scaledUp -
                         scaledRight;
                var v3 = scaledNormal - scaledUp +
                         scaledRight;
                var v4 = scaledNormal - scaledUp -
                         scaledRight;

                float?result;
                Functions.RayIntersectsSquare(ref ray, ref v1, ref v2, ref v3, ref v4, out result);
                raycasterFace.AddResult(result, face);
            }

            return(raycasterFace.ClosestObject);
        }
        private WorldObject RaycastWorldTile(IXNAGame _game)
        {
            raycaster.Reset();

            Ray ray;

            for (int i = 0; i < World.WorldObjectList.Count(); i++)
            {
                var obj          = (WorldObject)World.WorldObjectList[i];
                var objectMatrix = Matrix.Invert(obj.WorldMatrix);
                var objBB        = obj.ObjectType.TileData.GetBoundingBox();
                ray           = _game.GetWereldViewRay(_game.Mouse.CursorPositionVector);
                ray.Position  = Vector3.Transform(ray.Position, objectMatrix);
                ray.Direction = Vector3.TransformNormal(ray.Direction, objectMatrix);

                raycaster.AddResult(ray.Intersects(objBB), (WorldObject)World.WorldObjectList[i]);
            }

            return(raycaster.ClosestObject);
        }