Forum
Contact





DirectX using C#
DirectX using C++
DirectX using Visual Basic



Latest Forum posts

 Tutorial 3 for Windows Phone 7
  Posted by: Anonymous
  When: 20/05/2013 at 02:30:13

 No download link for 2d series: shooter
  Posted by: zaboleq
  When: 07/05/2013 at 15:46:28

 Collision Class?
  Posted by: Anonymous
  When: 05/05/2013 at 19:03:59

 stack overflow
  Posted by: cityguy
  When: 07/04/2013 at 01:58:38

 Meshes looks strange.
  Posted by: ab_saratov
  When: 01/04/2013 at 04:31:08

 Lamppost Not loaded
  Posted by: Anonymous
  When: 22/03/2013 at 06:43:52

 Collision Class?
  Posted by: Da_Boom
  When: 21/03/2013 at 01:23:09

 Math boggles me
  Posted by: cityguy
  When: 17/03/2013 at 03:44:48

 Collision Class?
  Posted by: Da_Boom
  When: 16/03/2013 at 03:44:42

 Tree update
  Posted by: Anonymous
  When: 15/03/2013 at 21:11:22

 XNA 4.0
  Posted by: Anonymous
  When: 15/03/2013 at 19:43:57

 Error when I try to run.
  Posted by: Anonymous
  When: 15/03/2013 at 19:21:06

 Error With the Effect File
  Posted by: Anonymous
  When: 15/03/2013 at 18:21:01

 Can only get shadowmap
  Posted by: Anonymous
  When: 15/03/2013 at 15:48:52

 Vertex and Pixel Shader Versions?
  Posted by: Anonymous
  When: 15/03/2013 at 15:07:16

 Unsupported properties
  Posted by: Anonymous
  When: 15/03/2013 at 14:23:00

 Problem Loading Skybox
  Posted by: Rana
  When: 15/03/2013 at 10:34:45

 Black Screen Of Death - Help!
  Posted by: Anonymous
  When: 15/03/2013 at 03:43:43

 2.0 anyone?
  Posted by: Anonymous
  When: 15/03/2013 at 02:19:48

 Defitinition of tha rotation axis
  Posted by: Anonymous
  When: 15/03/2013 at 00:55:14




Topic: how to rotate camera?



  
Goto parent category
  
Create a new user account


   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.

  
Post a new reply
 





Google
 
Web www.riemers.net
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


News
Home
Forum
XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Downloads
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
XNA 4.0 using C# (89)
DirectX using C# (54)
Series 1:Terrain (14)
Opening a window
Linking to the Device
Drawing a triangle
Camera
Rotation - Translation
Indices
Terrain creation
Terrain from file
DirectInput
Importing bmp files
Colored vertices
DirectX Light basics
Mesh creation
Mesh lighting
Series 2: Flightsim (19)
Series 3: HLSL (19)
Short Tuts (2)
Resizing problem
Checking Device caps
DirectX using C++ (15)
DirectX using VB (2)