I just need to rotate a model 90 degrees when it starts, without editing its local axis.
I have a missile flying 90 degrees sideways down a tunnel, and I need it to be facing down the tunnel while still retaining its movement directions. I’ve tried:
virtual public void setOrientationWorld(float x, float y, float z)
{
mOrientation *= Matrix.CreateRotationX(MathHelper.ToRadians(x));
mOrientation *= Matrix.CreateRotationY(MathHelper.ToRadians(y));
mOrientation *= Matrix.CreateRotationZ(MathHelper.ToRadians(z));
}
setOrientationWorld(0, -90, 0);
And I’ve tried:
virtual public void setOrientationLocal(float x, float y, float z)
{
mOrientation *= Matrix.CreateFromAxisAngle(mOrientation.Right, MathHelper.ToRadians(x));
mOrientation *= Matrix.CreateFromAxisAngle(mOrientation.Up, MathHelper.ToRadians(y));
mOrientation *= Matrix.CreateFromAxisAngle(mOrientation.Forward, MathHelper.ToRadians(z));
}
setOrientationLocal(0, -90, 0);
But they both ruin everything. When I do either, the missile rotates, and all the axis’s change. Even just going setPosition(0,0,-1) makes it move along the worlds X axis instead of it’s Z.
I have tried moving the missile both locally and globally, but to no avail, like this:
virtual public void moveLocal(float x, float y, float z)
{
mPosition += mOrientation.Right * x;
mPosition += mOrientation.Up * y;
mPosition += mOrientation.Forward * z;
}
And
virtual public void moveWorld(float x, float y, float z)
{
mPosition.X += x;
mPosition.Y += y;
mPosition.Z += z;
}
The only other relevant thing I can think of is the draw function, which may have something to do with it:
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = Matrix.Identity *
Matrix.CreateScale(mScale) *
transforms[mesh.ParentBone.Index] *
Matrix.CreateTranslation(mPosition) *
mOrientation;
effect.View = game1.getView();
effect.Projection = game1.getProjection();
}
Pls Halp!
The order you apply the translation and rotation is important. If you are applying the rotation globally then translating first will cause problems as you are moving the object away from it’s centre of rotation. In that case you need to rotate and then translate.
When applying the rotations in the local space of the missile, the order you apply the rotations is important too. Applying them in the order “Roll”, “Pitch”, “Yaw” will produce a different result to applying them in the order “Pitch”, “Yaw”, “Roll” and so on for all six combinations. The order is dependent on what you are trying to achieve so you may have to create a method that can do all six and selects which one based on a parameter.