Jump to content
• ### What is your GameDev Story?

• Advertisement

# OpenGL Rotate an object to mouse's coordinates?

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

Hey there. I hope someone can help. I am not sure how to do this, and I have searched for it. I found all kinds of posts, but nothing helped. I am using OpenGL, and I need to use the mouse's coordinates and rotate an object (turret) at the bottom of the screen to where the mouse is pointing. I also do not know how to get the mouse coordinates in order to rotate the object. If someone can post some code I would greatly appreciate it! =) Thanks in advance, Matt U.

#### Share this post

##### Share on other sites
Advertisement
Will someone please reply? I know the answer is out there. But I cannot find it anywhere on the forums when I search. I see a lot of people that figured it out, but I can't. =(

#### Share this post

##### Share on other sites
Google for gluUnproject. There are a lot of sites with info on how to use it (including GameDev).

And don't bump a post if it isn't answered in 45 minutes. Give people some time to answer it first.

EDIT: Nevermind about gluUnproject, I think you're talking about rotating something that's already in window coordinates.

#### Share this post

##### Share on other sites
I am not sure I understand what you want, but let's try.
Firstly: getting the mouse position. That depends on your Platform. Windows I guess? Thank I would suggest you look into DirectInput.

Now, setting the angle of the turret:
ydist - Y-Distance between turret and mouse position
xdist - X-Distance between turret and mouse position

You get the angle in radiant with:
atan2(Y,X);

To get it in degree (OpenGL uses degrees):
atan2(Y,X)*180.0f/M_PI;

I hope that helped a bit also I do not think I cleared all your problems.

#### Share this post

##### Share on other sites
Hey, sorry for the bump. =P That's my own fault. I was just in a hurry to find the answer. And I get frustrated with stuff like this sometimes. =P Sorry!

And I'll try that code to see if it works. I believe that is what I am looking for. =)

#### Share this post

##### Share on other sites
Quote:
 Original post by TFS_WaldoHey, sorry for the bump. =P That's my own fault. I was just in a hurry to find the answer. And I get frustrated with stuff like this sometimes. =P Sorry!
No worries, we all get frustrated at times and it was more for future reference than admonishment. Sorry I misunderstood your post at first.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 29
• 16
• 11
• 10
• 11
• Advertisement
• ### Forum Statistics

• Total Topics
634113
• Total Posts
3015588
×

## 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!