Skip to main content
gamedev.net
🔒 Locked

Camera Rotation

Started by Jiia 6 replies 450+ views
Original Post
Jiia
Jiia
My math skills suck pretty bad. All I''m trying to do is rotate my Eye point around an object. I''m trying to do this by converting Positions->Angle->ModifiedAngle->Positions. Here is my code:
FLOAT Angle = atan2f(Shape[0].Pos.x - CEye.x,Shape[0].Pos.z - CEye.z);

if(KEYDOWN(DIK_A))
{
	Angle += FrameDelay * 0.01f;
}
else if(KEYDOWN(DIK_D))
{
	Angle -= FrameDelay * 0.01f;
}

CEye.x = Shape[0].Pos.x + (cosf(Angle) * 150.0f);
CEye.z = Shape[0].Pos.z + (sinf(Angle) * 150.0f); 
Shape[0].Pos is the object''s vector. CEye is the cemera position vector. Can anyone show me what I''m doing wrong?
Jiia
Jiia
Umm I think I had part of it backwards. It was supposed to be
FLOAT Angle = atan2f(CEye.x - Shape[0].Pos.x,CEye.z - Shape[0].Pos.z); 
But it doesn''t make it work correctly.
JesusDesoles
JesusDesoles

Quaternions aside and assuming you are using DirectX why not experiment with the following:

D3DXVECTOR3 vEyePos = ...;
D3DXMATRIX matRot;
D3DXMatrixRotationYawPitchRoll( &matRot, fYawAngle,
fPitchAngle,
fRollAngle );
D3DXVec3TransformCoord( &vEyePos, &vEyepos, &matRot );

This would rotate the vEyePos around the origo. If the target is not at origo you need to translate the vEyePos correctly before and after the rotation.

This is not the fastest implementation but might get you on track for building one.

JD
Jiia
Jiia
All of the code I posted works except the atan2 line. If I just use an "Angle" member, I can add and subtract from it to rotate around the object. But something is wrong where I''m trying to get the same angle from their current positions (atan2 part).
Jiia
Jiia
Okay. For some reason, switching x & z in atan2 makes it work perfectly:
FLOAT Angle = atan2f(CEye.z - Shape[0].Pos.z, CEye.x - Shape[0].Pos.x); 
Can anyone understand why? Umm, I don''t have my cos and sin backwards, do I?? cos is x, right?? Umm, err, maybe I better go read up on this stuff.

Please feel free to reply some advice here. This is day #2 in the 3D world for me. Oh yeah, and thanks JesusDesoles. Especially since you were the only one kind enough to reply about cameras at 4:am in the morning
oliii
oliii
it all really depends what 0 degree angle represent for you. is 0 degree referenced from the X axis, or the Z axis? Are the angles increasing clockwise or anti-clockwise?

if your origin is the shape position, and you want to go anti-clockwise, and angle 0 is on the X axis of a triginimetric circle, then yeah, you''d have to do

float dx = eye.x - shape.x;
float dy = eye.y - shape.y;
float dz = eye.z - shape.z;

angle = atan2(dz, dx);

eye.x = shape.x + cos(angle) * distance;
eye.z = shape.z + sin(angle) * distance;



then, also, for the Y value of the eye,

float dxz = sqrt(dx*dx + dz*dz);
float elevation = atan2f(dy, dxz);
eye.y = shape.y + sin(elevation + delta_elevation) * distance;
Everything is better with Metal.
JesusDesoles
JesusDesoles

I was just about to point that atan2-thing out to you but you both beat me to it. Besides, from where I''m sitting the 3D world means 11 am so no worries, mate.

JD

Jiia
Jiia
I really appreciate the help, thanks to both of you

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.