Jump to content
  • Advertisement
Sign in to follow this  

2D pointing to a location [problem]

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

So I've been using opengl for a few weeks, and using SDL for I think 9-10 months
I have the basic idea of openGL and I decided it was better to use SDL (for input and such) with openGL (for graphics rendering), and all I have right now is one cube with a texture displayed

now here is the point of this:

I have the shape displayed at a point on the screen and I want it to be able to face the mouse but I can't figure out how to do so... I'm not the best at math but I do understand most of what I'm told

Here is some of my code that I've been using:

float pointto(float x1, float y1, float x2, float y2)
float n1 = sqrt(x1*x1+y1*y1);
float n2 = sqrt(x2*x2+y2*y2);
float angle = acos((x1*x2+y1*y2)/(n1*n2)) * 180 / PI;
return angle;


void DisplayImage(double x, double y, SDL_Surface* image, GLuint textr, double angle, double scale = 1, bool center=false)
glBindTexture(GL_TEXTURE_2D, textr);
if (center == true)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-(image->w/2)*scale, -(image->h/2)*scale,0);
glTexCoord2f(1.0f, 0.0f); glVertex3f((image->w/2)*scale, -(image->h/2)*scale, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f((image->w/2)*scale, (image->h/2)*scale, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-(image->w/2)*scale, (image->h/2)*scale, 0);
glTexCoord2f(0.0f, 0.0f); glVertex3f(1*scale, 1*scale,0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(image->w*scale, 1*scale, 0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(image->w*scale, image->h*scale, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(1*scale, image->h*scale, 0);


// in my main function

DisplayImage(100,100,zombie1,texture2, pointto(100,100,mouseX,mouseY),1,true);

that gets it to wiggle on the Z axis a little (I do want the z axis) but it doesn't face the mouse... what am I doing wrong?

mouseX and mouseY are stored in INT because of sdl... could that be a problem?

Share this post

Link to post
Share on other sites
Try using atan2. Also, what direction is the shape facing normally?
Usually in 2D zero degrees means facing right, and up is positive on the Y axis. The mouse position is usually larger at the bottom of the screen however.
Something like the following might give you a correct angle, but you must consider which direction is up and which direction is zero degrees, and perhaps modify the coordinates depending on that. Also, the mouse and shape coordinates should be in the same coordinate system, so if you have a moving camera or if OpenGL coordinates don't correspond to pixels you might need to transform your mouse coordinates.
angle = atan2(mouseY - shapeY, mouseX - shapeX) * 180.0 / PI.

Share this post

Link to post
Share on other sites
Holy :D it works haha thanks a lot, i've been trying to figure this out for the last 3 days.. thanks A LOT haha

Share this post

Link to post
Share on other sites
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!