View Full Version : Orbit around object

bazalddo

12-23-2011, 06:14 AM

Hi all,

I have a problem which I am hoping someone can help me with......

I have just implemented picking functionality for a few 3d object in my applications i.e. teapot, sphere, cube

Now what I would like to do is once the object is picked, be able to orbit around the object.

The first step I have made is I have made the target of the camera equal to the position of the object. I think this is correct

What do I need to do now???

I have a a vector library to work with which includes functions such as vecAdd, vecSub, vecProject, normalise and cross product. I have tried to get it working but to no avail :(

Thanks in advance, I really appreciate it

Baz

mobeen

12-23-2011, 06:23 AM

Hi,

Use the gluLookAt function passing it

1) the camera position,

2) target is the picked object position

3) up vector is (0,1,0).

The only tricky bit is how to evaluate the camera position. The solution is simple. Create two vars theta and phi globally, with 0<=theta<PI and 0<=phi<2*PI ( you can determine the increment/decrement amount based on the drag amount in the mouse move function). Next in the render function calculate the new camera position using the spherical coordinates like this,

Here radius is the radius of the orbiting circle.

eyeX = pickObjX + radius*cos(phi)*sin(theta);

eyeY = pickObjY + radius*sin(phi)*sin(theta);

eyeZ = pickObjZ + radius*cos(theta);

Finally, call the gluLookAt function,

gluLookAt(eyeX, eyeY, eyeZ, pickObjX, pickObjY, pickObjZ, 0,1,0);

See if this helps.

bazalddo

12-24-2011, 06:42 AM

Mobeen,

Firstly thanks for your help :)

I have tried to implement the solution but could not get it working (I imagine I am doing something wrong)

Can I clarify what the expression 0<=theta<PI means? I have not seen a variable declared like this before.

Also you mention that I need to increment based on the position of the mouse. What exactly needs to be incremented?? Can you give an example?? I did try a few things but the program produced some quite bizarre results :-/

Thanks for your help with this Mobeen, I really appreciate it

Baz

mobeen

12-24-2011, 06:57 AM

Can I clarify what the expression 0<=theta<PI means?

This means that the value of theta ranges from 0 to PI.

Also you mention that I need to increment based on the position of the mouse. What exactly needs to be incremented??

I mean in your mouse move event, you would increment theta and phi. I will give you an example in glut. The logic usually involves storing the oldX and oldY value in the mouse click event like this.

int oldX, oldY;

bool rotate = false;

float theta =0, phi=0;

void OnMouseDown(int button, int state, int x, int y) {

rotate=false;

if(button == GLUT_LEFT_BUTTON) {

oldX = x;

oldY = y;

rotate = true;

}

}

Then in the mousemove event, you simply calculate the theta and phi values like this,

void OnMouseMove(int x, int y) {

if(rotate) {

//you might need to adjust this multiplier(0.01)

theta += (x-oldX)*0.01f;

phi += (y-oldY)*0.01f;

}

oldX = x;

oldY = y;

glutPostRedisplay();

}

Next your render function would calculate the new eye position like this,

void OnRender() {

eyeX = pickObjX + radius*cos(phi)*sin(theta);

eyeY = pickObjY + radius*sin(phi)*sin(theta);

eyeZ = pickObjZ + radius*cos(theta);

//assuming modelview matrix mode is set

gluLookAt(eyeX, eyeY, eyeZ, pickObjX, pickObjY, pickObjZ, 0,1,0);

//rest of stuff ...

glutSwapBuffers();

}

bazalddo

12-24-2011, 07:11 AM

Thats great Mobeen thank you so much for your help!!!!

Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.