[SOLVED] Translate along local axis in 3d space

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:

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!

Have a look here.

If you are just trying to do momentary rotation around the local axis which it looks like you are at first glance here is a snippet from a different camera class.
In the below case the turn speeds are very small amounts that add moments of rotation to the existing world matrix axis’s.

You might need to take the translation the forward and up from the resulting matrix after this and use it in Matrix.CreateWorld to get a new orthanormalized matrix as if i remember right the multiplys can cause the matrix orientation vectors to denormalize over time if you only use this every frame.

            case UP_FREE_Y: 
                if (UseYawPitchRollToRotate)
                {
                    world *=
                        Matrix.CreateFromAxisAngle(world.Right, turnSpeedInDimension.Y) *
                        Matrix.CreateFromAxisAngle(world.Up, turnSpeedInDimension.X) *
                        Matrix.CreateFromAxisAngle(world.Forward, turnSpeedInDimension.Z)
                        ;
                    Forward = world.Forward;  //  <<  Calls create world ensuring the vectors are orthanormal.
                }
2 Likes

Thanks for the quick answer!

The first snippet which you sent seems to work.

Thanks again!

If you click the link
the individual rotations are in there as well if that is also what you were looking for.

1 Like