I have trouble to make my sprite(Arrow) to point at the position of my mouse cursor but i don't get the Right results. I've tried this:

SDL_Point Center{ 40,40 };
int Delta_x; int Delta_y;
int mouse_x, mouse_y;
double Result;
SDL_GetMouseState(&mouse_x, &mouse_y);
Delta_x = xPos - mouse_x;
Delta_y = yPos - mouse_y;

Result = (atan2(Delta_x, Delta_y) * 180.0000)/ 3.14159265;
m_Sprite->Render(Result, Center);


SDL_RenderCopyEx(m_Renderer, m_Texture, NULL, &m_Rect, Angle , &Center, SDL_FLIP_NONE);

This is the result I get (in the original picture, the arrow is pointing up):

enter image description here


2 Answers 2


atan2 expects the Y parameter first. See this SO question for answers as to why the Y parameter is passed in first.

void LookAt(Vector2 p2)
    int DeltaX;
    int DeltaY;
    double result;
    DeltaX = position.x - p2.x;
    DeltaY = position.y - p2.y;
    result = (atan2(-DeltaX, DeltaY) * 180.00000) / 3.141592;
    angle = result;

This is how I did it, position being the position of the object that needs to be rotated angle being the angle of the object, The negative x in the y's place actually worked perfectly,its pretty much the exact same as yours just make the DeltaX negative, I dont know if it will work for you but it was perfect for me BUT I have no idea why it works haha.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .