Jump to content
  • Advertisement
Sign in to follow this  
widecr0w

Spheric camera with Quaternions bug

This topic is 2274 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

Hi,

I'm having some problems with the implementation of a sphere camera (camera that moves on a sphere, always looking at center of this sphere).

I'm having these strange flips when rotating over 2 axes at the same time. I made a demo mov of the problem:



the bug/problem I'm talking about appears at 0:05 and 0:15.


public class SphereCamera
{
private Point3 cameraEye;
private Point3 cameraLook;
private Vector3 cameraUp;
private Vector3 u, v, n;
private float rotationSpeed = 10;
private float zoomSpeed = 10;
// Default position of the camera
public final static Point3 defaultEye = new Point3(600, 0, 0);
public final static Point3 defaultLook = new Point3(0, 0, 0);
public final static Vector3 defaultUp = new Vector3(0, 1.0f, 0);
/**
* Standard constructor.
* Creates a new CameraController object.
* @param eye position of the camera
* @param look position the camera is looking at
* @param up orientation of camera
*/
public SphereCamera(Point3 eye, Point3 look, Vector3 up)
{
setCameraEye(eye);
setCameraLook(look);
setCameraUp(up);
n = new Vector3(cameraLook, cameraEye);
u = cameraUp.cross(n);
n.normalize();
u.normalize();
v = n.cross(u);
}
/**
* Resets the camera to default position.
*/
public void reset()
{
setCameraEye(defaultEye);
setCameraLook(defaultLook);
setCameraUp(defaultUp);
n = new Vector3(cameraLook, cameraEye);
u = cameraUp.cross(n);
n.normalize();
u.normalize();
v = n.cross(u);
}
/**
* Rotate the camera upward.
*/
public void up(float rotSpeed)
{
verticalRotation(-rotSpeed);
}
/**
* Rotate the camera downward.
*/
public void down(float rotSpeed)
{
verticalRotation(rotSpeed);
}
public void verticalRotation(float rotSpeed)
{
Quaternion startQ = new Quaternion(cameraEye);
Quaternion rotationQ = new Quaternion(rotSpeed, u);
Quaternion newPointQ = (rotationQ.product(startQ)).product(rotationQ.conjugate());

cameraEye.set(newPointQ.b, newPointQ.c, newPointQ.d);
n = new Vector3(cameraLook, cameraEye);
n.normalize();


v = n.cross(u);

if (n.y == -1f || n.y == 1f)
{
cameraEye.y = -cameraEye.y;
}
}
/**
* Rotates the camera to the left.
*/
public void left(float rotSpeed)
{
horizontalRotation(-rotSpeed);
}
/**
* Rotates the camera to the right.
*/
public void right(float rotSpeed)
{
horizontalRotation(rotSpeed);
}
public void horizontalRotation(float rotSpeed)
{
Quaternion startQ = new Quaternion(cameraEye);
Quaternion rotationQ = new Quaternion(rotSpeed, v);
Quaternion newPointQ = (rotationQ.product(startQ)).product(rotationQ.conjugate());

cameraEye.set(newPointQ.b, newPointQ.c, newPointQ.d);
n = new Vector3(cameraLook, cameraEye);
n.normalize();
u = cameraUp.cross(n);
u.normalize();
}
/**
* Zooms the camera in.
*/
public void zoomIn(float zoom)
{
zoom(zoom);
}
/**
* Zooms the camera out.
*/
public void zoomOut(float zoom)
{
zoom(-zoom);
}
private void zoom(float val)
{
cameraEye.set(cameraEye.x + n.x * val, cameraEye.y + n.y * val, cameraEye.z + n.z * val);
}



I don't think there is a problem with my quaternion class.. It was tested and worked fine.

Any help would be greatly appreciated :)

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!