PDA

View Full Version : Rotating to a point.



Roach
06-11-2003, 10:20 AM
Hi gang - I am having a little problem and am looking for some ideas.

I have a sphere (actually a globe) and it sits nicely in the window. What I need to have happen is that when the user clicks on s place on the globe, I rotate to that point. i.e. say we are focused in on London England, when I click on New York city, the globe should rotate to New York City so that NYC is the new focal point.

I have a nice little function already that will rotate me to a lat/long position given an latitude angle and a longitude angle. What I don't have is a way to transform the screen(mouse) coordinates to a lat/lon coordinate.

I thought of doing a gluUnProject to get the position where the user clicks's object coordinates - but then I'm not sure how to rotate to those object coordinates.

Any ideas??

Roach

dorbie
06-11-2003, 04:39 PM
Any point on the sphere represents a vector from the center of the globe. Any two points are a pair of vectors. The best interpolation between those two vectors is a rotation about their cross product by an angle that is the acos of their dot product.

That should be simple to implement.

peter.vullings
06-11-2003, 04:39 PM
I haven't worked out the math for this, but...

your center of the sphere is known (e.g. 0,0,0).

Pretend there is a flat invisible plane infront of your globe (the screen), just touching the globe at the point closest the viewer, and the viewer is looking through it.

G S

# |
## |<- mouse
###|
###|
###|
## |
# |

G=globe, S=screen (flat plane), <- =mouse pointer

What you need to do when the user clicks the mouse is project the mouse position from the plane on to the globe by drawing a line directly in to the screen until it touches the globe (this should be easy enough to do if you image it from top view - as in the awfull image above).

Do this twice - once to calculate the point from the top, and once from the side view. From the point on the globe, you can now calculate the angle from each of these points to the origin of the globe - as measured from the imaginary line from the viewer directly to the center of the globe.

These two angles are the amount to rotate the globe by to get the new position...

These 2

peter.vullings
06-11-2003, 04:56 PM
Further to my post above:

http://www.pixelthis.co.nz/opengl/images/globe.gif

peter.vullings
06-11-2003, 05:05 PM
Using the equation of a circle - this example is for TOP VIEW:

The radius of your sphere (r) is known
The 'x' point is known (distance from where the 'viewer' line intersects the screen, and the mouse pointer touches the screen).

So then you can work out 'y' (you will get 2 values - a positive and a negative. You can work out which to use!

so r^2 = x^2 + y^2
becomes y = sqrt(r^2-x^2)

You now have x and y, which - if you think about it - is actually x and z (y can be ignored for the top view) you now have the 2d vector from (0,0) to (x,z), and can easily work out angle between this, and the vector (0,0) to (0,-1)...

[This message has been edited by peter.vullings (edited 06-11-2003).]

Roach
06-16-2003, 06:01 AM
Hey guys - Thanks a lot for the info! I do have some questions though.

First of all - how do I figgure out the line that goes from the mouse to the globe?? Don't I have to do something funky with gluUnProject to transform the mouse coordinates to an object coordinate?

If not - does anyone have some sample code that I can take a look at to further explain this process?? I'm sorry - I'm kinda new to all this stuff (Blame my Boss for assigning me this task!!).

Thanks
Roach