| Topic: how to rotate camera?
|
|
 | how to rotate camera? | |  |
| Poster | : EwanK | | Posts | : 5 | | Country | : Slovakia | | City | : |
| | | | Posted by EwanK on 06/02/2007 at 19:07:04
| | hey,
my circumstances require the camera to rotate around the 0,0,0 location on a circle, instead of a rotating world and a static camera. is this possible? if so, how can i achieve this goal?
thanks in advance for any tips 8-) | |
|
| | | | | | Poster | : reyjexter | | Posts | : 21 | | Country | : philippines | | City | : legazpi city |
| | | | Posted by reyjexter on 07/02/2007 at 08:20:51
| | hello!
you can rotate around a certain axis using the following code:
float angle = 0.009f;
private void ProcessKeyboard(GameTime gameTime)
{
KeyboardState state = Keyboard.GetState();
if (state.IsKeyDown(Keys.Left))
{
// rotate along the y axis
Quaternion rotate = Quaternion.CreateFromAxisAngle(Vector3.Up, angle);
cameraPosition = Vector3.Transform(cameraPosition, Matrix.CreateFromQuaternion(rotate));
}
if(state.IsKeyDown(Keys.Right))
{
// rotate along the y axis
Quaternion rotate = Quaternion.CreateFromAxisAngle(Vector3.Up, -angle);
cameraPosition = Vector3.Transform(cameraPosition, Matrix.CreateFromQuaternion(rotate));
}
if (state.IsKeyDown(Keys.R))
{
cameraPosition = new Vector3(0, 10, 10); ;
}
}
but i'm not yet sure how the camera will rotate around a certain point.
regards,
rey | |
|
| | | | | | Poster | : reyjexter | | Posts | : 21 | | Country | : philippines | | City | : legazpi city |
| | | | Posted by reyjexter on 07/02/2007 at 08:23:00
| | by the way, the last statement only brings my camera back to its default starting position sso you dont need to worry much about that.
regards,
rey | |
|
| | | | | | Poster | : reyjexter | | Posts | : 21 | | Country | : philippines | | City | : legazpi city |
| | | | Posted by reyjexter on 07/02/2007 at 08:23:10
| | by the way, the last statement only brings my camera back to its default starting position so you don't need to worry much about that.
regards,
rey | |
|
| | | | | | Poster | : EwanK | | Posts | : 5 | | Country | : Slovakia | | City | : |
| | | | Posted by EwanK on 08/02/2007 at 02:38:14
| | hey! thx for the tips but ive already figured it out with a little maths help from my cousin.
im gonna put it here in case someone finds it handy.
first you need to define three float values that will help you: angleV, angleH and radius.
angleH is a horizontal angle of the camera in radians, angleV is the vertical one and the radius is actually the distance from the center you want to rotate around. the proper use of these three floats will allow you to rotate around this center on a circle keeping the same distance from the center, with being able to modify the radius if necessary (which means to 'come closer' to the center). as you might probably guessed, we will use some goniometry of course and this is how it goes:
first you need to have something to modify the floats with. my angleH changes by 0.02f with each press of the Left/Right key, same goes with angleV but using the Up/Down keys and finally the radius can be changed with mouse scroll.
you can also limit these angles to not be able to go above certain amounts, just to make sure you dont get anything unexpected. you could especially make a top limit of vertical angle to PI/2 - 0.01f and bottom limit to 0. if you want to know why, just try to move the camera beyond these :)
now we're comeing to the final part of the problem -> modifying the device.Transform.View in the OnPaint method.
first you might change the vector that specifies the 'Up' direction to vector3(0,0,1) cause if you keep it at (0,1,0), the camere will not only rotate around the center, but also around its own direction axis (the axis that connects the position of the camere and the center).
finally we're going to modify the x,y and z coordinates of the camera vector that specifies the position of the camera, using the three floats.
the X coordinate goes as follows:
x0 + radius * (float)Math.Cos((double)angleV) * (float)Math.Sin( (double)angleH )
where x0 is the x coordinate of the center (which is actually (0,0,0) in my case, so i just omitted the x0,y0 and z0 part completely)
the Y coordinate:
y0 + radius * (float)Math.Cos((double)angleV) * (float)Math.Sin( (double)angleH )
finally the Z coordinate:
z0 + radius * (float)Math.Sin( (double)angleV )
that should be it! i hope i didnt forget anything. and that this could be useful to anyone :)
SHY | |
|
| | | | | | Poster | : EwanK | | Posts | : 5 | | Country | : Slovakia | | City | : |
| | | | Posted by EwanK on 08/02/2007 at 02:49:37
| | if anyone wonders why ive put the upper limit of the vertical angle to PI/2 - 0.01f, this is the reason:
i was displaying the three floats using the Font control as described in the 'Displaying Text' of the Series2 tutorial and noticed that when you move the camera up the vertical circle, as soon as it passes the maximum height, it goes further and comes back from the other side (which was not the thing i expected or wanted). the maximum height is actually the same as the radius here, as we're moving on circles and the vertical angle is cca 1.57 radians which is PI/2. but when i limited the angle to PI/2, it stopped at the maximal height and actually turned around to start descending from the opposite direction (hard to explain it like this.. just try it yourself) so i limited it to PI/2 - 0.01 where 0.01f is a half of the step i change the angles by.
yeah and i put the bottom limit of radius to 5. | |
|
|
 | | |  |
|
|
|
If you appreciate the amount of time I spend creating and updating these pages, feel free to donate -- any amount is welcome !
|
- Website design & DirectX code : Riemer Grootjans - ©2006 Riemer Grootjans
|
|