Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (522) Games in Android Showcase (127) games submitted by our members Games in WIP (589) 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 2352 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: 361
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.
 Games published by our own members! Check 'em out!
theagentd

« JGO Bitwise Duke »

Medals: 361
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: 361
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: 355
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: 113
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:

(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: 113
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 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: 361
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: 355
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.
 trollwarrior1 (26 views) 2014-11-22 12:13:56 xFryIx (69 views) 2014-11-13 12:34:49 digdugdiggy (48 views) 2014-11-12 21:11:50 digdugdiggy (42 views) 2014-11-12 21:10:15 digdugdiggy (36 views) 2014-11-12 21:09:33 kovacsa (60 views) 2014-11-07 19:57:14 TehJavaDev (64 views) 2014-11-03 22:04:50 BurntPizza (62 views) 2014-11-03 18:54:52 moogie (77 views) 2014-11-03 06:22:04 CopyableCougar4 (77 views) 2014-11-01 23:36:41
 theagentd 29x basil_ 27x BurntPizza 23x Riven 19x Spasi 18x princec 17x HeroesGraveDev 16x KevinWorkman 13x kpars 11x SHC 11x gouessej 10x Grunnt 10x Nate 9x Longarmx 9x LiquidNitrogen 8x CopyableCougar4 8x
 Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List 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:06
 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