I am using XNA -- Just in case -- Just asking for some sort of algorithm.
I have a camera, say posted at "vector3(0,10,10)", this point is added to the player's location, successfully achieving a "Stalking camera" Problem is, when the character turns, the camera does not turn --
--How do I have the camera turn with the player's rotation? (Players rotation = CharRotation, Camera's Position = cameraPosition, Player's position = CharPosition)
Liam
Update Method, where the algorithm I am asking for is being put (I know my code is inefficent, I'm working on it.
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
Vector3 mapviewingangle = Vector3.Zero;
Vector3 camadd = Vector3.Zero;
float tf = 0.05f;
KeyboardState kState = Keyboard.GetState();
if (kState.IsKeyDown(Keys.D))
if (camadd.X < 10)
camadd.X += 0.2f;
if (kState.IsKeyDown(Keys.A))
if (camadd.X < -10)
camadd.X += 0.2f;
if (plmovelock == 0){
if (kState.IsKeyDown(Keys.Right))
{
CharRotation -= tf;
}
else if (kState.IsKeyDown(Keys.Left))
{
CharRotation += tf;
}
if (kState.IsKeyDown(Keys.Down))
{
CharPosition.X += (float)(Math.Sin(CharRotation)/7);
CharPosition.Z += (float)(Math.Cos(CharRotation)/7);
}
else if (kState.IsKeyDown(Keys.Up))
{
CharPosition.X -= (float)(Math.Sin(CharRotation)/7);
CharPosition.Z -= (float)(Math.Cos(CharRotation)/7);
}
Window.Title = "[X: " + CharPosition.X + ", Y: " + CharPosition.Y + ", Z: " + CharPosition.Z + "] Map ID: " + map + ", Char Rotat: " + CharRotation + ", CamPos: " + cameraPosition;
if (map == 1)
{
if (CharPosition.Z > 8.2f) CharPosition.Z = 8.1999f;
if (CharPosition.X > 14.2f) CharPosition.X = 14.1999f;
if (CharPosition.Z < -18.0f) CharPosition.Z = -17.9999f;
if (CharPosition.X < -13.0f) map = 2;
if (map == 2) CharPosition = Vector3.Zero;
if (map == 2) CharRotation = 0.0f;
npcPosition1 = new Vector3(1000, 1000, 1000);
}
if (map == 2)
{
if (CharPosition.Z < -20.5f) CharPosition.Z = -20.4999f;
if (CharPosition.X > 21.35f) CharPosition.X = 21.3499f;
if (CharPosition.X < -20) CharPosition.X = -19.9999f;
if (CharPosition.Z > .5f) map = 1;
if (map == 1) CharPosition = new Vector3(-12,0,-4);
if (map == 1) CharRotation = -1.5f;
}
}
//HERE IS WHERE THE ALGORITHM WILL GO
if (CharRotation > 6) CharRotation = 0.0f;
cameraPosition = new Vector3();
base.Update(gameTime);
}