我有这个敌人的阶级,我想要它
1)在某个地方产生Vector3 pos
2)旋转以面对我的球员位置
3)前进
现在这个代码,它将出现在它指定的位置:pos,并尝试旋转以面对它的目标:targetShip。由于这种偏移,它无法正确面对它的目标。
如果我删除了为翻译赋值的代码,或者我将pos标准化,则模型将出现在原点并旋转以正确面对目标。
如果我尝试使用任何有关翻译的注释代码来移动它,它似乎从其他地方开始,我永远找不到它。
但是,如果我删除与轮换相关的代码,并取消与翻译相关的代码,那么我可以让它继续前进。
诀窍是一起完成。
class Enemy : BasicModel
{
Matrix rotation = Matrix.Identity;
Matrix translation = Matrix.Identity;
public Vector3 pos, up, right, targetShip,dir;
public Enemy(Model m, Vector3 pos)
: base(m)
{
up = Vector3.Up;
//sets the position to the Vector3 as it's spawn point.
translation = Matrix.CreateTranslation(pos);
}
public override void Update()
{
//Glo is a global class, where I store the player world.
targetShip = Glo.world.Translation;
targetShip.Normalize();
pos = transform.Translation;
rotation = RotateToFace(targetShip, pos, Vector3.Up);
//Attempt at moving the model forward. Causes it to go out of view
//translation *= Matrix.CreateTranslation(this.GetWorld().Backward);
//translation *= Matrix.CreateTranslation(pos);
}
public override Matrix GetWorld()
{
return rotation * world * translation ;
}
/*Params: O = Our target
* P = Our position
* U = up.
*
* Code from some site I googled up.
*/
Matrix RotateToFace(Vector3 O, Vector3 P, Vector3 U)
{
//The direction we're facing.
Vector3 D = (O - P);
//Our relative Right.
Vector3 Right = Vector3.Cross(U, D);
Vector3.Normalize(ref Right, out Right);
//Our back
Vector3 Backwards = Vector3.Cross(Right, U);
Vector3.Normalize(ref Backwards, out Backwards);
//Our relative up
Vector3 Up = Vector3.Cross(Backwards, Right);
//Make a matrix out of all of these.
Matrix rot = new Matrix(Right.X, Right.Y, Right.Z, 0, Up.X, Up.Y, Up.Z, 0, Backwards.X, Backwards.Y, Backwards.Z, 0, 0, 0, 0, 1);
return rot;
}
}
}
我试过
public override void Update()
{
targetShip = Glo.world.Translation;
targetShip.Normalize();
//Translate it to the origin
translation = Matrix.Identity;
//Orient it
rot = RotateToFace(targetShip, translation.Translation, Vector3.Up);
//Move it back to where it was originally;
translation = Matrix.CreateTranslation(offset);
//Calculate the front of the object
Vector3 front = Vector3.Forward * translation.Translation;
//normalize it
Vector3.Normalize(ref front, out front);
//multiply it
front *= 2;
//translate the object
translation *= Matrix.CreateTranslation(front);
}
public override Matrix GetWorld()
{
return world * rot * translation;
}
第二次修订:仍然无效
public override void Update()
{
targetShip = Glo.world.Translation;
targetShip.Normalize();
translation = Matrix.CreateLookAt(offset, targetShip, Vector3.Up);
var amountToMove = 0.2f;
var finalPos = Vector3.Lerp(targetShip, offset, amountToMove);
rot = translation * Matrix.CreateTranslation(finalPos);
}
public override Matrix GetWorld()
{
return world * rot * translation ;
}
答案 0 :(得分:0)
我修好了,别担心。无论如何,所有这些代码都已过时。