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

## 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){                        glLoadIdentity();                        glTranslatef(x,y,0);                        glRotatef(angle,0.0f,0.0f,1.0f);                        glBindTexture(GL_TEXTURE_2D, textr);                        glBegin(GL_QUADS);                        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);                        }                        else                        {                        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);                        }                        glEnd();}///////////////////////////////////////////////////////////////////////////// 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?

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

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

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002131
×