0

我想使用 Up-Vector 获得网格的碰撞向量。

我有一个位置和一个向上向量..用这两个我计算远向量

public Vector3 SetPlayerToGround(Matrix Object, Matrix Player, Mesh GroundObject)
        {

            Vector3 IntersectVector = new Vector3(0, 0, 0);

            if (GroundObject != null)
            {
                Vector3 Player_UP = new Vector3(Player.M12, Player.M22, Player.M32);
                Vector3 PlayerPos = new Vector3(Player.M14, Player.M24, Player.M34);
                Vector3 PlayerPos_Far = PlayerPos - Player_UP ;

                gameengine.m_Device.Transform.World = Object;


                IntersectInformation closestIntersection;


             
                if (GroundObject.Intersect(PlayerPos, PlayerPos_Far, out closestIntersection))
                {
                    IntersectVector = Player_UP + (PlayerPos_Far * closestIntersection.Dist);

                }
            }

            return IntersectVector;
        }

好吧,如果我这样做

Vector3 PlayerPos_Far = PlayerPos + Player_UP;

它总是与任何东西相交......但我想要相交的对象总是在“位置 - UpVector”下

所以我认为

Vector3 PlayerPos_Far = PlayerPos - Player_UP ;

是正确的

为什么我不能相交?

这是一个更好的描述:

我需要相交的场景图像

这是玩家,他在一艘船上。玩家总是在 0,0,0,因为我在玩家周围移动世界。如果我将玩家向前移动,我会选择玩家位置向量,它只会影响所有其他对象的位置。但我认为玩家与 Intersect 无关……而是船本身。我使用位置 0,0,0 和上向量作为方向来从船中获取地面的相交向量。船的矩阵是(矩阵对象):

Vector3 com = ship.position - gamemanager.player.position;
                    Matrix world;
                    world = Matrix.Identity * Matrix.Scaling(0.001f, 0.001f, 0.001f) * Matrix.Translation(com);
                    m_Device.Transform.World = world;

我尝试了一些东西,我认为他不会使用我翻译的飞船矩阵......

4

1 回答 1

0

您必须提供由位置和方向定义的射线。您不必指定两个点。所以通过-PLAYER_UP而不是PlayerPos_Far.

此外,该Intersect方法将不关心转换。您必须预先转换光线。用逆世界矩阵变换光线位置和方向(从世界空间变换到模型空间),你应该没问题。

Vector3 rayStart = PlayerPos;
Vector3 rayDir = -PLAYER_UP;
Matrix world; //the ship's world matrix
world = Matrix.Invert(matrix);
rayStart = Vector3.TransformCoordinate(rayStart, world); //you may need to add an out parameter
rayDir = Vector3.TransformNormal(rayDir, world);
GroundObject.Intersect(rayStart, rayDir, ...
于 2013-05-05T20:55:44.453 回答