OpenGL Moving Image in circular motion

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

Recommended Posts

Hi,
I want to move an image in a circular motion ( not rotating the image like using glRotatef() in Opengl) while moving the mouse.

I am able to move the image in translational way using mouse motion. But I am not getting on how to move it in a CIRCULAR MOTION
where the image moves in a fixed radius.

what i mean is when the user clicks on the image and decides to move it in circle, then as the user drags the mouse the image starts to move
around in circular motion.

I tried using circle equation for it like after getting the 'X' position from mouse position I calculated the 'Y' position using the circle
equation like newY = sqrtf( abs( ( r*r) - (x* x) ) ); // x*x + y*y = r*r

I have kept the radius constant like r=100.0, since the random mouse position varies in range from ( 1024, 768 ) ie the screen width and height.

then using the 'X' and the 'newY' I use glTranslatef(X, newY, 0.0); to move the image, but its not giving the correct behavior.

What should i do for it. I am using C/C++ and Opengl

All suggestions are welcome.

Share on other sites

but its not giving the correct behavior.
[/quote]

This is too vague, can you elaborate a bit? Maybe post your code. Are you centering your x coordinate around the center of where you want the circle to be, or just using the pixel x value of the mouse?

Share on other sites
Unless I'm missing something your formula ignores the point your image is supposed to rotate around (unless it is actually supposed to be (0,0) which is most likely the top left of the screen). Also, how is that formula supposed to ever result in a negative Y? Unless you intentionally only use the bottom (or top) half of the circle, just knowing X is NOT enough to calculate Y, because each X has two possible Ys (except for X=r and X=-r).

Also, did you limit X to r (or scale it appropriately)? What part of your result is wrong? At least X should be right, or your problem has nothing to do with the circular motion itself.

The lazy way (if you know center and angle) would be

glTranslate( <position of pivot> )
glRotate( <degree of rotation> )
glTranslate( radius, 0,0 ) (assuming that 0° equals "right")
glRotate( -<degree of rotation> )

Share on other sites

but its not giving the correct behavior.

This is too vague, can you elaborate a bit? Maybe post your code. Are you centering your x coordinate around the center of where you want the circle to be, or just using the pixel x value of the mouse?
[/quote]

To give a better view about what i am trying to implement, just run this application in browser http://phet.colorado...n/bending-light

Click the Prism Break section and see just how the laser is moving according to the mouse movement. In that you can see both the rotational and translational movement.

Are you centering your x coordinate around the center of where you want the circle to be, or just using the pixel x value of the mouse?
[/quote]

I am using the pixel x value of the mouse

Share on other sites
to move an object in a circular motion I use the following code (taken from my python for openGL experimentation solar system example)

 def calculateOrbit(self,rotation,distance,height): x = distance*math.sin(rotation*math.pi/180.0) z = distance*math.cos(rotation*math.pi/180.0) y = height*math.sin(rotation*math.pi/180.0) return (x,y,z) 

rotation is a floating point number from 0.0 to 360.0 (although to stop divide by zero errors you can make this range 1.1 to 361.1)
distance is the distance from the central point which is 0.0 for me but just change = distance* to =center+distance*
height allows you to make the transition from circular to elipsoid so that it is not perfectly circular but more akin to an orbit with wobble

for your perfect circle, switch the z= to y = and do not set z from variable, but a constant that fits your viewport correctly

hope this helps

1. 1
Rutin
19
2. 2
3. 3
JoeJ
15
4. 4
5. 5

• 18
• 19
• 10
• 13
• 17
• Forum Statistics

• Total Topics
631694
• Total Posts
3001751
×