Hi all,
I’ve been having some trouble getting my 3d model to translate along its local axis which has been rotated. The problem is that the model will be rotated, and the model will move only along the world axis. Basically, what I am trying to achieve is to have the object face in a certain direction and then move in that direction.
I have read:
- Matrix Basics. How to step away from storing an orientation as ‘3 angles’
- c# - how to move 3d object in XNA? - Stack Overflow (stackoverflow.com)
- Simple 3D Animation - RB Whitaker’s Wiki (wikidot.com)
- c++ - how to rotate all objects by their own centers and then translate them to the real position (it isn’t working) - Stack Overflow (stackoverflow.com)
- c++ - Translating a 3D object along a local axis - Stack Overflow (stackoverflow.com)
- 3d - Moving a rotated model in XNA - Game Development Stack Exchange (stackexchange.com)
- 3d - Translate along local axis - Game Development Stack Exchange (stackexchange.com)
As it stands, this is my current code to move the model:
public void Move(Vector3 position, Vector3 rotation)
{
Matrix rotationX = Matrix.CreateFromAxisAngle(World.Right, rotation.X);
Matrix rotationY = Matrix.CreateFromAxisAngle(World.Up, rotation.Y);
Matrix rotationZ = Matrix.CreateFromAxisAngle(World.Backward, rotation.Z);
Matrix translation = Matrix.CreateTranslation(Vector3.Transform(World.Translation + position, rotationX * rotationY * rotationZ));
Vector3 trans = World.Translation;
World = rotationX * rotationY * rotationZ;
// If it didn't rotate, have no effect on the translation
if (position != Vector3.Zero) World *= translation;
else World.Translation = trans;
}
Apologies for how primitive this code must seem, but I am very new to 3D programming, so this is probably something extremely simple that I am doing wrong.
Thanks in advance!