TX3D Camera Rotation with Player Object
by Tony Pitman · in Torque X 3D · 11/17/2009 (12:01 am) · 2 replies
I can't seem to figure out how to get my camera to rotate with my player object.
Here is the code from my _UpdateInput in my player component.
Note:
1. _camera has been set to the camera object in the scene.
2. The camera does MOVE with my player it just doesn't rotate.
3. Even though the view of the camera does not rotate the player object does rotate because if push the rotation key (arrows) and move the movement direction changes.
So the whole question is why doesn't the camera rotate?
Here is the code from my _UpdateInput in my player component.
Note:
1. _camera has been set to the camera object in the scene.
2. The camera does MOVE with my player it just doesn't rotate.
3. Even though the view of the camera does not rotate the player object does rotate because if push the rotation key (arrows) and move the movement direction changes.
So the whole question is why doesn't the camera rotate?
if (move.Sticks.Count < 2)
return;
float rotX = move.Sticks[1].X;
rotX = (float)Math.Pow(Math.Abs(rotX), Math.E) * (rotX > 0.0f ? 1.0f : -1.0f);
float rotY = _invertedCamera ? move.Sticks[1].Y : -move.Sticks[1].Y;
rotY = (float)Math.Pow(Math.Abs(rotY), Math.E) * (rotY > 0.0f ? 1.0f : -1.0f);
_playerAngle = (_playerAngle - (_turnSpeed * dt * rotX)) % (2.0f * (float)Math.PI);
SceneGroup.Rotation = Quaternion.CreateFromYawPitchRoll(0.0f, 0.0f, _playerAngle);
Matrix playerTranslationMatrix = _playerSceneComponent.Transform;
Vector3 right = MatrixUtil.MatrixGetRow(0, ref playerTranslationMatrix);
Vector3 forward = MatrixUtil.MatrixGetRow(1, ref playerTranslationMatrix);
Vector3 vel = ((forward * move.Sticks[0].Y) + (right * move.Sticks[0].X)) * 4.5f;
_rigidComponent.Velocity = vel;
if (_camera != null) {
_camera.SceneGroup.Rotation = Quaternion.CreateFromYawPitchRoll(0.0f, 0.0f, _playerAngle);
Vector3 cameraPosition = SceneGroup.Position;
_camera.SetTransform(Matrix.CreateTranslation(cameraPosition), false);
}
#2
Thank you again!
11/17/2009 (10:55 am)
Thanks! That did it. In the end I actually only want to rotation the camera as my game is one where the player stays in one place. I was only moving him to get a starting point and make sure my camera was set up correctly. Your code doesn't deal with the camera pitching up and down, but from what you gave I was able to figure that out. Here is my final code:float rotX = move.Sticks[1].X;
rotX = (float)Math.Pow(Math.Abs(rotX), Math.E) * (rotX > 0.0f ? 1.0f : -1.0f);
_playerRollAngle = (_playerRollAngle - (_turnSpeed * dt * rotX)) % (2.0f * (float)Math.PI);
float rotY = _invertedCamera ? move.Sticks[1].Y : -move.Sticks[1].Y;
rotY = (float)Math.Pow(Math.Abs(rotY), Math.E) * (rotY > 0.0f ? 1.0f : -1.0f);
_playerPitchAngle = (_playerPitchAngle - (_turnSpeed * dt * rotY)) % (2.0f * (float)Math.PI);
SceneGroup.Rotation = Quaternion.CreateFromYawPitchRoll(_playerPitchAngle, 0.0f, _playerRollAngle);
if (_camera != null) {
Vector3 cameraPosition = SceneGroup.Position;
_camera.SetTransform(Matrix.CreateRotationX(_playerPitchAngle) * Matrix.CreateRotationZ(_playerRollAngle) * Matrix.CreateTranslation(cameraPosition), false);
}Thank you again!
Torque 3D Owner Tom Ogburn
Starlit Sky Games
private void MoveCameraBehindPlayer() { //Set camera's location behind us if (_camera != null) { Vector3 cameraPosition = Vector3.Zero; cameraPosition = Vector3.Transform(_cameraOffset, Matrix.CreateFromQuaternion(SceneGroup.Rotation)); cameraPosition += _rigidComponent.RigidBody.Position; _camera.SetTransform(Matrix.CreateRotationZ(_playerAngle) * Matrix.CreateTranslation(cameraPosition), false); } } Vector3 _cameraOffset = new Vector3(1.0f, -8.0f, 2.0f);I call it each update. You can change the _cameraOffset to be closer or further away. I have a 1.0f for the X because my models aren't centered in their bounding box so I have to shift them a little to have them centered on the screen.