Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (574) Games in Android Showcase (154) games submitted by our members Games in WIP (620) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Rotation questions  (Read 2598 times) 0 Members and 1 Guest are viewing this topic.
Huw

Senior Devvie

Medals: 1
Projects: 2

 « Posted 2012-10-19 19:23:48 »

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:
 1  2  3  4  5  6 `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;`

I'm a java noob, but I'm learning. My little blog. My Games.
theagentd

« JGO Bitwise Duke »

Medals: 429
Projects: 2
Exp: 8 years

 « Reply #1 - Posted 2012-10-19 19:33:06 »

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

2)
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.

Myomyomyo.
Huw

Senior Devvie

Medals: 1
Projects: 2

 « Reply #2 - Posted 2012-10-19 19:41:25 »

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

2)
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:
 1  2  3 `float angle = (float) Game.angle;            glTranslatef(x, y, 0);            glRotatef(angle, 0, 0 , 1);`

Sprite class which the sprite inherits:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37 `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();   }`

I'm a java noob, but I'm learning. My little blog. My Games.
theagentd

« JGO Bitwise Duke »

Medals: 429
Projects: 2
Exp: 8 years

 « Reply #3 - Posted 2012-10-19 23:11:34 »

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39 `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.

Myomyomyo.
Huw

Senior Devvie

Medals: 1
Projects: 2

 « Reply #4 - Posted 2012-10-19 23:31:29 »

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

I'm a java noob, but I'm learning. My little blog. My Games.
theagentd

« JGO Bitwise Duke »

Medals: 429
Projects: 2
Exp: 8 years

 « Reply #5 - Posted 2012-10-20 01:18:46 »

*hums* You spin me right round, baby, right round... *hums*

Myomyomyo.
ra4king

JGO Kernel

Medals: 374
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #6 - Posted 2012-10-20 03:54:29 »

*hums* You spin me right round, baby, right round... *hums*
*hums* ... like a record baby, right round round round ... *hums*

matheus23

JGO Kernel

Medals: 121
Projects: 3

You think about my Avatar right now!

 « Reply #7 - Posted 2012-10-20 07:59:49 »

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Huw

Senior Devvie

Medals: 1
Projects: 2

 « Reply #8 - Posted 2012-10-20 09:19:32 »

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

I'm a java noob, but I'm learning. My little blog. My Games.
matheus23

JGO Kernel

Medals: 121
Projects: 3

You think about my Avatar right now!

 « Reply #9 - Posted 2012-10-20 09:22:03 »

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

Oh... I like it
Never knew of the original... heh... (just like nobody knows infiniminer )

hijack!

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Huw

Senior Devvie

Medals: 1
Projects: 2

 « Reply #10 - Posted 2012-10-20 09:30:10 »

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:
 1  2  3  4  5  6  7  8 `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:
 1  2  3 `glTranslatef(x, y, 0);                  glRotatef(angle, 0, 0, 1);                  glTranslatef(-width/2, -height/2, 0);`

I'm a java noob, but I'm learning. My little blog. My Games.
theagentd

« JGO Bitwise Duke »

Medals: 429
Projects: 2
Exp: 8 years

 « Reply #11 - Posted 2012-10-20 13:30:41 »

@matheus23 and Huw
YOU LIED! THAT'S NOT THE ORIGINIAL!

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:
 1  2  3  4  5  6  7  8 `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:
 1  2  3 `glTranslatef(x, y, 0);                  glRotatef(angle, 0, 0, 1);                  glTranslatef(-width/2, -height/2, 0);`

Maybe your mouse y coordinate needs to be flipped?

Myomyomyo.
ra4king

JGO Kernel

Medals: 374
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #12 - Posted 2012-10-20 20:15:39 »

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