Friday, November 4, 2016

xna - Camera wont stay behind model after pitch, then rotation


I have a camera position behind a model. Currently, if I push the left thumbstick making my model move forward, backward, or strafe, the camera stays with the model. If I push the right thumbstick left or right, the model rotates in those directions fine along with the camera rotating while maintaining its position relatively behind the model.


But when I pitch the model up or down, then rotate the model afterwards, the camera moves slightly rotates in a clock-like fashion behind the model. If I do a few rotations of the model and try to pitch the camera, the camera will eventually be looking at the side, then eventually the front of the model while also rotating in a clock-like fashion.


My question is, how do I keep the camera to pitch up and down behind the model no matter how much the model has rotated? Here is what I got:



// Rotates model and pitches camera on its own axis
public void modelRotMovement(GamePadState pController)
{
// Rotates Camera with model
Yaw = pController.ThumbSticks.Right.X * MathHelper.ToRadians(angularSpeed);
AddRotation = Quaternion.CreateFromYawPitchRoll(Yaw, 0, 0);
ModelLoad.MRotation *= AddRotation;
MOrientation = Matrix.CreateFromQuaternion(ModelLoad.MRotation);

// Pitches Camera around model

Pitch = pController.ThumbSticks.Right.Y * MathHelper.ToRadians(angularSpeed);
}

// Orbit (yaw) Camera around with model (only seeing back of model)
public void cameraYaw(Vector3 axisYaw, float yaw)
{
ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
Matrix.CreateFromAxisAngle(axisYaw, yaw)) + ModelLoad.camTarget;
}
// Raise camera above or below model's shoulders

public void cameraPitch(Vector3 axisPitch, float pitch)
{
ModelLoad.CameraPos = Vector3.Transform(ModelLoad.CameraPos - ModelLoad.camTarget,
Matrix.CreateFromAxisAngle(axisPitch, pitch)) + ModelLoad.camTarget;
}

// Call in update method
public void updateCamera()
{
cameraYaw(Vector3.Up, Yaw);

cameraPitch(Vector3.Right, Pitch);
}

NOTE: I tried to use addPitch just like addRotation but it didn't work...


UPDATE I understand the problem Im having fully. While Im facing world-North or world-South, the camera pitches around the model like normal. But lets say I'm facing world-East. The camera will still point to the model regardless of the rotation, but when I pitch the camera, it will rise and fall in a clock-like fashion around the back of the model. So the camera itself is still pitching as to world's North and South. Not the model's North and South.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...