Jump to content
  • Advertisement
Sign in to follow this  
byhisdeeds

Camera rotation

This topic is 4246 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a camera class that is based on quaternions and am having problems with the following. I have an object (a globe) that is made up of thousands of vertices, and I want to be able to: A) view the object, then rotate the camera to see what is around me (to the left and right of the camera, etc); B) view the object, then rotate the object, keeping the camera position fixed. I can get the camera to move such that I can see around it, but I can't get the object to rotate while the camera remains fixed. What is the best (most efficient) way to get B.

Share this post


Link to post
Share on other sites
Advertisement
You need to post the relevant code. You haven't given enough information to even guess at what the problem could be (though I'll bet you are using OpenGL).

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
You need to post the relevant code. You haven't given enough information to even guess at what the problem could be (though I'll bet you are using OpenGL).

Sorry about that. Yes I'm using OpenGL. The camera class is below, and I wanted to know with the code below how to rotate around the view position rather than the camera position.

public class Camera
{
// GLU instance
public static final GLU glu = new GLU();

/** View */
public final View view;

/** A quaternion used for camera rotation. */
private final Quaternion qRotation;

/** A quaternion used for camera direction. */
private final Quaternion qDirection;

/** A quaternion used for orbiting a camera around a point. */
private final Quaternion qOrbit;

/** A temporary quaternion used for camera operations. */
private final Quaternion qCamera;

/**
*
*/
public Camera()
{
// initialise camera rotation quaternion
qRotation = new Quaternion();

// initialise camera direction quaternion
qDirection = new Quaternion();

// initialise camera orbiting around a point quaternion
qOrbit = new Quaternion();

// initialise temporary quaternion used for camera operation
qCamera = new Quaternion();

// initialise camera view
view = new View();
view.viewChanged = true;
}

/**
*
*/
public Camera(Vector3 p, Vector3 v)
{
this();
set(p, v, view.vUp);
}

/**
*
*/
public void set(Vector3 p, Vector3 v, Vector3 u)
{
if (p != null)
{
view.vPosition.x = p.x;
view.vPosition.y = p.y;
view.vPosition.z = p.z;
}

if (v != null)
{
view.vView.x = v.x;
view.vView.y = v.y;
view.vView.z = v.z;
}

if (u != null)
{
view.vUp.x = u.x;
view.vUp.y = u.y;
view.vUp.z = u.z;
}

view.viewChanged = true;
}

/**
* Move forwards and backwards
*/
public void move(double amount)
{
view.vDirection.difference(view.vView, view.vPosition).crossProduct(view.X_AXIS).normalize();

moveCamera(view.vDirection, amount);
}

/**
* Move side to side
*/
public void strafe(double amount)
{
view.vDirection.difference(view.vView, view.vPosition).crossProduct(view.Y_AXIS).normalize();

moveCamera(view.vDirection, amount);
}

/**
* move up and down
*/
public void elevate(double amount)
{
view.vPosition.y += amount;
view.vView.y += amount;

// reset view changed flag
view.viewChanged = true;
}

/**
*
*/
private void moveCamera(Vector3 vDir, double amount)
{
// scale direction vector
vDir.scalarMultiply(amount);

view.vPosition.sum(vDir);
view.vView.sum(vDir);

// reset view changed flag
view.viewChanged = true;
}

/**
*
*/
public void pan(double angle)
{
Quaternion q =
rotateCamera(view.vDirection.difference(view.vView, view.vPosition),view.Y_AXIS, angle);

view.vView.sum(view.vPosition, q.v);
}

/**
*
*/
public void tilt(double angle)
{
Quaternion q =
rotateCamera(view.vDirection.difference(view.vView, view.vPosition), view.X_AXIS, angle);

view.vView.sum(view.vPosition, q.v);
}

/**
*
*/
public void roll(double angle)
{
Quaternion q =
rotateCamera(view.vDirection.difference(view.vView, view.vPosition), view.Z_AXIS, angle);

view.vView.sum(view.vPosition, q.v);
}

/**
*
*/
public void orbit(double angle, String axis)
{
Quaternion q =
rotateCamera(view.vDirection.difference(view.vPosition, view.vView),
axis.equalsIgnoreCase("z") ? view.Z_AXIS : view.Y_AXIS, angle);

view.vPosition.sum(view.vView, q.v);
}

/**
* @param angle angle of rotation in radians
*/
private Quaternion rotateCamera(Vector3 vDir, Vector3 vAxis, double angle)
{
qRotation.set(vAxis, angle);
Quaternion conjugate = qRotation.conjugate();
qDirection.set(vDir.x, vDir.y, vDir.z, 0);
qCamera.set(qRotation.multiply(qDirection).multiply(conjugate));

// reset view changed flag
view.viewChanged = true;

return qCamera;
}

/**
*
*/
public void initialise(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();

// setup some view parameters
gl.glGetIntegerv(GL.GL_VIEWPORT, view.viewport, 0);

gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(view.fovY, (double)view.viewport[2] / (double)view.viewport[3], view.near, view.far);

// save local copy of OpenGL projecion matrix
gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, view.pmat, 0);
}

/**
*
*/
public void updateView(GL gl)
throws ShutdownException
{
// setup the projection matrix just in case
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadMatrixd(view.pmat, 0);

// now setup the modelview matrix
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(view.vPosition.x, view.vPosition.y, view.vPosition.z,
view.vView.x, view.vView.y, view.vView.z,
view.Y_AXIS.x, view.Y_AXIS.y, view.Y_AXIS.z);

// save local copy of OpenGL view matrix
gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, view.vmat, 0);
}

/**
*
*/
private Vector3 NORMAL(Vector3 p0, Vector3 p1, Vector3 p2)
{
Vector3 n = new Vector3();
n.x = (p1.y - p0.y) * (p2.z - p0.z) - (p1.z - p0.z) * (p2.y - p0.y);
n.y = (p1.z - p0.z) * (p2.x - p0.x) - (p1.x - p0.x) * (p2.z - p0.z);
n.z = (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x);
return n;
}
}


Share this post


Link to post
Share on other sites
Your camera class looks ok (though I didn't check closely). Where is the code that rotates the object? Also, put code between [source] and [/source].

Also, you only have to set the projection matrix once.

[Edited by - JohnBolton on April 25, 2007 3:27:06 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
Your camera class looks ok (though I didn't check closely). Where is the code that rotates the object?


That's what I mean. How can I best (most efficiently) rotate the object (globe) that I'm looking at.

To move the camera (my eye) forwards and backwards, I use the camera.move(amount) (negative for backwards, positive for forwards) method. To look left and right, I use the camera.strafe(amount) (negative for left, positive for right) method. To look up and down, I use the camera.tilt(amount) (negative for down angle, positive for up angle) method. To move my eye up and down I use the camera.elevate(amount) (negative for down, positive for up) method. Etc, etc, etc

Now to keep my eye (the camera) fixed and rotate the object I'm thinking that I have to move the camera in various amounts in the x,y, and z direction. How do I do this, say if I wanted to rotate the object I'm looking at by a particular angle.


Quote:
Also, you only have to set the projection matrix once.

Thanks.

Share this post


Link to post
Share on other sites
Hi,

You'll just need to move your camera in a circle and keep it's rotation fixed on the middle. In your cameraclass, this would require two seperate operations. One way to do this would be like this:
Let p be the point you wish to rotate around and c be your camera's position.
p - c will be the difference between your camera and the point. By rotating this vector in the same way as your camera and subtracting the result from your position vector (p - rotate(p - c)), you can rotate your camera's position around the point.

[Edited by - Ignifex on April 25, 2007 7:08:11 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by byhisdeeds
Quote:
Original post by JohnBolton
Your camera class looks ok (though I didn't check closely). Where is the code that rotates the object?

That's what I mean. How can I best (most efficiently) rotate the object (globe) that I'm looking at.


How about using glRotate to rotate the object?

Check out the NeHe tutorials -- especially lesson 4 on rotation. The tutorials are in C++ but Java should be similar, and you can download Java versions of the code at the bottom of the page.

Share this post


Link to post
Share on other sites
Quote:
How about using glRotate to rotate the object?


I maintain the vertices of my object in a arrays, and update them whenever sections of the object change. These arrays may hold over 100K triangles. I thought that it would be faster to move the camera rather than having to process all the vectex data, everytime I want to rotate the object. Isn't this so?

Share this post


Link to post
Share on other sites
Quote:
Original post by byhisdeeds
I maintain the vertices of my object in a arrays, and update them whenever sections of the object change. These arrays may hold over 100K triangles. I thought that it would be faster to move the camera rather than having to process all the vectex data, everytime I want to rotate the object. Isn't this so?
OpenGL will process the vertex data in exactly the same way whether you 'move the camera' or 'rotate the object'. The differences between these two approaches are purely conceptual, so you should just use the method that makes the most sense to you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!