Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (427)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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 2111 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: 322
Projects: 2
Exp: 4 years


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 Kernel


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

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
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 Kernel


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

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!
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: 322
Projects: 2
Exp: 4 years


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.

 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (78 views)
2014-04-15 18:08:23

BurntPizza (70 views)
2014-04-15 03:46:01

UprightPath (81 views)
2014-04-14 17:39:50

UprightPath (66 views)
2014-04-14 17:35:47

Porlus (82 views)
2014-04-14 15:48:38

tom_mai78101 (106 views)
2014-04-10 04:04:31

BurntPizza (166 views)
2014-04-08 23:06:04

tom_mai78101 (262 views)
2014-04-05 13:34:39

trollwarrior1 (212 views)
2014-04-04 12:06:45

CJLetsGame (221 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
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!