Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (497) Games in Android Showcase (114) games submitted by our members Games in WIP (563) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Rotation questions  (Read 2253 times) 0 Members and 1 Guest are viewing this topic.
Huw

Senior Member

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
 « 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 Member

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.
 Games published by our own members! Check 'em out!
theagentd
 « 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 Member

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
 « Reply #5 - Posted 2012-10-20 01:18:46 »

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

Myomyomyo.
ra4king

JGO Kernel

Medals: 345
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: 107
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 Member

Medals: 1
Projects: 2

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

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

(Putting it in [youtube]..[/youtube] bbcodes doesn't seem to work...

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

JGO Kernel

Medals: 107
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:

(Putting it in [youtube]..[/youtube] bbcodes doesn't seem to work...
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
 Games published by our own members! Check 'em out!
Huw

Senior Member

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
 « 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: 345
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:

(Putting it in [youtube]..[/youtube] bbcodes doesn't seem to work...
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=".

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 UprightPath (21 views) 2014-09-20 20:14:06 BurntPizza (27 views) 2014-09-19 03:14:18 Dwinin (40 views) 2014-09-12 09:08:26 Norakomi (70 views) 2014-09-10 13:57:51 TehJavaDev (96 views) 2014-09-10 06:39:09 Tekkerue (49 views) 2014-09-09 02:24:56 mitcheeb (70 views) 2014-09-08 06:06:29 BurntPizza (52 views) 2014-09-07 01:13:42 Longarmx (39 views) 2014-09-07 01:12:14 Longarmx (45 views) 2014-09-07 01:11:22
 BurntPizza 36x Riven 18x Rayvolution 18x ags1 16x princec 16x basil_ 16x KevinWorkman 15x LiquidNitrogen 12x theagentd 11x kevglass 11x deathpat 11x nsigma 11x HeroesGraveDev 8x TehJavaDev 6x PandaMoniumHUN 6x Gibbo3771 6x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59:08
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org