Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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 1521 times)
0 Members and 1 Guest are viewing this topic.
Offline Huw

Senior Member


Medals: 1
Projects: 2



« Posted 2012-10-19 21: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.
Offline theagentd
« Reply #1 - Posted 2012-10-19 21: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.
Offline Huw

Senior Member


Medals: 1
Projects: 2



« Reply #2 - Posted 2012-10-19 21: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!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-10-20 01: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.
Offline Huw

Senior Member


Medals: 1
Projects: 2



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

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

Myomyomyo.
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


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

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

Offline matheus23

JGO Wizard


Medals: 71
Projects: 3


You think about my Avatar right now!


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

<a href="http://www.youtube.com/v/d1rdkJz8nxM?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/d1rdkJz8nxM?version=3&amp;hl=en_US&amp;start=</a>

Take a look at my development Blog: http://matheusdev.tumblr.com
Also look at my RPG Ruins of Revenge
Offline Huw

Senior Member


Medals: 1
Projects: 2



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

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

http://www.youtube.com/watch?v=PGNiXGX2nLU

(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.
Offline matheus23

JGO Wizard


Medals: 71
Projects: 3


You think about my Avatar right now!


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

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

http://www.youtube.com/watch?v=PGNiXGX2nLU

(Putting it in [youtube]..[/youtube] bbcodes doesn't seem to work...
Oh... I like it Smiley
Never knew of the original... heh... (just like nobody knows infiniminer Grin )

hijack! Cheesy

Take a look at my development Blog: http://matheusdev.tumblr.com
Also look at my RPG Ruins of Revenge
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Huw

Senior Member


Medals: 1
Projects: 2



« Reply #10 - Posted 2012-10-20 11: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.
Offline theagentd
« Reply #11 - Posted 2012-10-20 15:30:41 »

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

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.
Offline ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


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

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

http://www.youtube.com/watch?v=PGNiXGX2nLU

(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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (31 views)
2013-05-17 21:29:12

alaslipknot (40 views)
2013-05-16 21:24:48

gouessej (70 views)
2013-05-16 00:53:38

gouessej (69 views)
2013-05-16 00:17:58

theagentd (78 views)
2013-05-15 15:01:13

theagentd (73 views)
2013-05-15 15:00:54

StreetDoggy (113 views)
2013-05-14 15:56:26

kutucuk (137 views)
2013-05-12 17:10:36

kutucuk (136 views)
2013-05-12 15:36:09

UnluckyDevil (145 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.095 seconds with 21 queries.