1.

Solve : 3D Camera Algorithms -- XNA?

Answer»

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.

Code: [Select]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);
}so it's a "chase cam" and you want to to appear behind the player a certain distance?

if thats the case, you would need to use a wee bit of trig.


Say you want it X units behind the player, and Y units up. Assume P is the players angle:

If you don't have the player's angle stored anywhere, you can acquire it by solving the right angle triangle formed by your players vector- the X vector and Y vector:

Sin(Angle)= Yvector/XVector

Angle=ArcSin(YVector/Xvector)

*Sigh* of course this might mean implementing the ArcSin Function:

Code: [Select]Arcsin(x) = atan(X)/sqrt(-X * X + 1));


For this I'll assume that the camera isn't following the up-down angle of the player, or that the player doesn't have an up-down movement- if you need that, I might be able to whip something up, but my trig is so rusty right now I'd rather not complicate things unnecessarily

Xdist= Cos(viewangle) * amount
Ydist=Sin(viewangle)*amount

of course you want it to be behind the player so you would multiply these results by -1.

Place the camera Xdist and Ydist away from your player after these calculations, and give it the same angle as provided by the previous functions. (change: I noticed you do have the players rotation stored- that removes that necessary step)Ho-le crap.

I didn't follow ANYTHING PAST "--Assume P is the players angle:"

But chase cam should be enough for me to Google it -- what I did INSTEAD of this was to have the map turn instead of the player-- so the player never really is turning, giving the same effect. =D



Discussion

No Comment Found