Rotation questions

Hi, I’m currently learning to use opengl and I am trying to get my sprite to rotate around it’s center, at the moment it seems to be rotating around the top left corner doing a huge circle in(and out) of the screen.
Question1: How can I get the sprite to rotate around it’s center?

Also, I’m trying to get the sprite to rotate to face where the mouse is positioned. I did a bit of searching and found a solution to get the angle of 2 points, but It doesnt seem to be working very well…
Question2: How can I get a sprite to follow(by rotating) the mouse position?

Current code to get the angle between mouse and sprite:

x1 = ship.x+50;
		y1 = ship.y+50;
		x2 = Mouse.getX();
		y2 = Mouse.getY();
		
		angle = Math.atan2(y1 - y2, x1 - x2) * 360/ Math.PI*-1;

glTranslatef(shipCenterX, shipCenterY, 0);
glRotatef(…);
//Then draw the ship centered at (0, 0).

Assuming you’re using OpenGL you need to convert that to degrees. Math.toDegrees(radianAngle);

EDIT: Your current code does not convert it to degrees. 360 degrees = 2 pi radians, not 1 pi. The *-1 also makes no sense.

Thanks, I think the second answer has worked, but if I do glTranslatef(x, y, 0) it moves the sprite to the edge of the screen, but the sprite should already be at x y…so I’m, not sure what I’m doing wrong:/
rotation code:

float angle = (float) Game.angle;
				glTranslatef(x, y, 0);
				glRotatef(angle, 0, 0 , 1);

Sprite class which the sprite inherits:

public void draw(int x, int y) {
		/
		glPushMatrix();

		
		texture.bind();

		
		glTranslatef(x, y, 0);
		
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		
		
		
		
		glBegin(GL_QUADS);
		{
			glTexCoord2f(0, 0);
			glVertex2f(0, 0);

			glTexCoord2f(0, texture.getHeight());
			glVertex2f(0, height);

			glTexCoord2f(texture.getWidth(), texture.getHeight());
			glVertex2f(width, height);

			glTexCoord2f(texture.getWidth(), 0);
			glVertex2f(width, 0);
			
			
		}
		glEnd();

		
		glPopMatrix();
	}
public void draw(int x, int y) {

		glPushMatrix();

		
		texture.bind();
		
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

		
		glTranslatef(x, y, 0);
                glRotatef(angle, 0, 0, 1);
                glTranslate(-width/2, -height/2, 0);
		
		
		
		
		glBegin(GL_QUADS);
		{
			glTexCoord2f(0, 0);
			glVertex2f(0, 0);

			glTexCoord2f(0, texture.getHeight());
			glVertex2f(0, height);

			glTexCoord2f(texture.getWidth(), texture.getHeight());
			glVertex2f(width, height);

			glTexCoord2f(texture.getWidth(), 0);
			glVertex2f(width, 0);
			
			
		}
		glEnd();

		
		glPopMatrix();
	}

This is how I’d do it.

Thankyou, the sprite now spins on it’s center, still has a few little glitches but I’m sure I can sort them myself.

+1

hums You spin me right round, baby, right round… hums

hums … like a record baby, right round round round … hums

d1rdkJz8nxM

That’s flo rida’s rubbish version,
They were singing this:

(Putting it in

bbcodes doesn’t seem to work…

Oh… I like it :slight_smile:
Never knew of the original… heh… (just like nobody knows infiniminer ;D )

hijack! :smiley:

Back on topic, my current code doesn’t follow the mouse, it moves when the mouse does, but seems to be moving the wrong way and it’s not in line with the mouse movements…

Getting the angle:

x1 = ship.x+50;
		y1 = ship.y+50;
		x2 = Mouse.getX();
		y2 = Mouse.getY();
		
		angle = Math.atan2(y1 - y2, x1 - x2) * 360/ (Math.PI*2);
		
		Math.toDegrees(angle);

Rotating/translating:

glTranslatef(x, y, 0);
	               glRotatef(angle, 0, 0, 1);
	               glTranslatef(-width/2, -height/2, 0);

@matheus23 and Huw
YOU LIED! THAT’S NOT THE ORIGINIAL!
;D

Maybe your mouse y coordinate needs to be flipped?

Sorry for off-topic, but using the youtube tags, you only put the video ID which is all the numbers and letters after the “v=”.