Need help with a rotation

I have issues bringing rotations into my app. I want to rotate a sprite around its center, consisting of 2 Polygons.
My Setup:
I set the projection matrix in the shader. I want to do the rotation on the cpu using glm. Otherwise I won’t be able to batch every sprite that needs rotation.

My approach:


f32 x_origin = position.x - size.x / 2;
f32 y_origin = position.y - size.y / 2;

x = ((pos.x - x_origin) * cos(angle)) - ((y_origin - pos.y) * sin(angle))
y = ((y_origin - pos.y) * cos(angle)) - ((pos.x - x_origin) * sin(angle))

But the sprite just vanishes from the screen.
Can someone help me understanding this?

I have updated my code:


	for ( int i=0; i < 6; i++ )
	{
		f32 x_origin = m_vertices[i].x - m_size.x / 2;
		f32 y_origin = m_vertices[i].y - m_size.y / 2;

		f32 x = (m_vertices[i].x * std::cos(angle)) -  (m_vertices[i].y * std::sin(angle) );
		f32 y = (m_vertices[i].y * std::cos(angle)) +  (m_vertices[i].x * std::sin(angle) );

		m_vertices[i].x = x + x_origin;
		m_vertices[i].y = y + y_origin;
	}

I made this according to a tutorial, but now nothing is happening. I noticed values going to minus. Ofc, when I substract the vertice which has 10 (position) and substract size(32) then it is -22.
What to do?

What you want:

rotate( m_vertices[i] - translate ) + translate

That’s not what you’re doing.

have you tried using matrices, they are really good once you learn them

I know, but when I make + translate the sprite goes away :(.

I tried to use glm. But I could not figure out which function to use to multiply it with the vertex. The documentation is somewhat “spare”.

The problem is:
I batch nearly all my sprites on the screen. So I cannot simply send a Rotation Matrix and multiply it with the Projection Matrix in the Shader.


	f32 rad = angle * (NL_PI / 180.0f );

	for (int i=0; i<6; i++)
	{
		f32 center_x = m_vertices[i].x - m_size.x / 2;
		f32 center_y = m_vertices[i].y - m_size.y / 2;

		f32 x = ((m_vertices[i].x - center_x) * std::cos(rad)) -  ((m_vertices[i].y - center_y) * std::sin(rad) );
		f32 y = ((m_vertices[i].x - center_x) * std::sin(rad)) +  ((m_vertices[i].y - center_y) * std::cos(rad) );

		m_vertices[i].x = x - center_x;
		m_vertices[i].y = y - center_y;
	}

I got this equetation running and it is rotating around a point, but it is flickering and I want it to behave like glRotatef.
Unfortunately I am not smart enough to implement the matrix from the documentation of glRotatef.


	// Center of Sprite
	glm::vec3 center = glm::vec3(m_position.x + m_size.x / 2, m_position.y + m_size.y / 2, m_position.z);
	
	// Translate to center
	glm::mat4 trans_tmp  = glm::translate(glm::mat4(1), center);

	// Rotate
	glm::mat4 rotation_tmp = glm::rotate(trans_tmp, angle, glm::vec3(0,0,1)); 

	// Translate back
	m_rotation = glm::translate(rotation_tmp, -center);

Solved it :slight_smile: