This seems like a simple problem with an equally simple solution that is invisible to me.
I have a model that spawns at the origin and looks at my player model wherever it goes.
I would like it to move forward where it's going. Whenever I try to translate it, it disappears. What am I doing wrong and how do I fix it?
public override void Update()
{
//Glo is a global class, where I store the player's world.
targetShip = Glo.world.Translation;
targetShip.Normalize();
//Rotates the model to face the player's position
rotation = RotateToFace(targetShip, pos, Vector3.Up);
pos += Vector3.Normalize(targetShip - pos) * 20f;
}
public override Matrix GetWorld()
{
// return rotation * translation * world ;
return Matrix.CreateFromQuaternion(rotation) * Matrix.CreateTranslation(pos);
}
UPDATE: When I replace:
translation *= Matrix.CreateTranslation(direction);
with
translation *= Matrix.CreateTranslation(Vector3.Forward);
It now moves forward.
It does not correctly face me, however. It still thinks it's at the origin, and is rotating accordingly.
Here is the edited RotateToFace function The original just returns a Matrix.
Quaternion 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);
Quaternion rot2 = Quaternion.CreateFromRotationMatrix(rot);
return rot2;
}
Draw code:
public void Draw(Camera camera)
{
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect be in mesh.Effects)
{
be.EnableDefaultLighting();
be.Projection = camera.projection;
be.View = camera.view;
be.World = GetWorld() * mesh.ParentBone.Transform;
}
mesh.Draw();
}
}
world
is read by other classes for hit detection and seems to be the object's real position \$\endgroup\$