So I want to rotate a 2D object so that it is always “looking” at the cursor.

I am using glad; glfw; glm; SDL2 for the mouse capturing;

I tried this quite a time now and I am definitly stuck here.

After some time I got the expression the best Idea would be to define a “Obj.LookatVector” which is pretty much just a vector from the center of the object to the cursor.

And whenever the cursor is moved I’d calculate the angle btw. the first and the second lookatvector.

In otherwords I’d imagine a line drawn from the center of my object to the cursor and it simply follows that line.

My current Code snippet looks like this:

```
centerX = PlayerPos.x + PlayerSize.x/2;
centerY = PlayerPos.y + PlayerSize.y/2;
xoffset = xmouse - centerX;
yoffset = ymouse - centerY;
MousePointer = glm::vec2(xoffset, yoffset);
angletemp = glm::atan(MousePointer.y/ MousePointer.x) - glm::atan(ObjLookAt.y/ObjLookAt.x);
if(angletemp > 3,14159)
angletemp -= 2*3,14159;
if(angletemp < 3,14159)
angletemp +=2*3,14159;
angletemp = glm::degrees(angletemp);
angle += angletemp;
Triangle->update(PlayerPos.x, PlayerPos.y, angle);
ObjLookAt = MousePointer;
```

Mousepointer is the vector from the center to the cursor.

ObjLookAt is initialised as (centerX, centerY-10) simply to simulate a “lookat” vector at the start.

This is my Matrixstuff going on; I return the angle to “rotate” in the rotate matrix in degrees.

```
model = glm::translate(model, glm::vec3(position, 0.0f));
model = glm::translate(model, glm::vec3(0.5f * size.x, 0.5f * size.y, 0.0f));
model = glm::rotate(model, rotate, glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, glm::vec3(-0.5f * size.x, -0.5f * size.y, 0.0f));
model = glm::scale(model, glm::vec3(size, 1.0f));
```

So what I am basically do is scaling my object to its size; translate it to its center, rotate it around the z axis, as it’s 2D, bringig it back to its origin and drawing it at its position.

However I cannot calculate the right angle.

My object rotates in very funny ways whatever method I tried until now (and I tried a lot), but not in the way I want it.

I know this is probably an easy thing, but as far as I have seen in in the virtual space most questions are tackled around moving objects in 3D or pushing them around by clicking on them etc.

So far I could not find any solution that seemed to tackle my problem (or I am simply not able to adapt it mentally to my problem)…

Any suggestions/ help?