Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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  
  Unable to rotate a 2D triangular polygon via glRotatef().  (Read 463 times)
0 Members and 1 Guest are viewing this topic.
Offline tom_mai78101
« Posted 2014-01-04 12:23:48 »

Here's a (big) GIF of me holding down the "TURN" key and "FORWARD" key together, and below is the code:

Click to Play


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   public void draw() {
      GL11.glColor3f(0f, 1f, 1f);
      GL11.glBegin(GL11.GL_LINE_LOOP);
      {
         GL11.glPushMatrix();
         GL11.glTranslatef(-x, -y, 0f);
         GL11.glRotatef(angle, 0, 0, 1f);
         GL11.glVertex2f(x, (y - 10));
         GL11.glVertex2f((x + 8), (y + 10));
         GL11.glVertex2f(x, (y + 5));
         GL11.glVertex2f((x - 8), (y + 10));
         GL11.glVertex2f(x, (y - 10));
         GL11.glTranslatef(x, y, 0f);
         GL11.glPopMatrix();
      }
      GL11.glEnd();
      GL11.glColor3f(1f, 1f, 1f);
   }


I don't know how to rotate the "ship" around the Z axis.

EDIT: Forgot to mention that I did tried many different combinations of "glTranslate()" and "glRotate()" in different spots in the draw() code. They either skew the ship's vertices around, or they don't do anything. And I'm getting all confused as to why.
Offline Seiya02

JGO Coder


Medals: 3
Projects: 4
Exp: 5 years


No Text Here :x


« Reply #1 - Posted 2014-01-04 12:49:37 »

1. Why is there another glTranslate right before glPopMatrix? this won't do anything cause glPopMatrix undos it.

2. If you set The Position of the Ship with a glTranslate why is there x and y coordinates in the glVertex2f methods? doesn't make sense to me
Offline Seiya02

JGO Coder


Medals: 3
Projects: 4
Exp: 5 years


No Text Here :x


« Reply #2 - Posted 2014-01-04 12:53:58 »

Try this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void draw() {
      GL11.glPushMatrix();
      GL11.glTranslatef(-x, -y, 0f);
      GL11.glRotatef(angle, 0, 0, 1f);
      GL11.glColor3f(0f, 1f, 1f);
      GL11.glBegin(GL11.GL_LINE_LOOP);
      {
         GL11.glVertex2f(0,  - 10);
         GL11.glVertex2f( 8, 10);
         GL11.glVertex2f(0, 5);
         GL11.glVertex2f( - 8, 10);
         GL11.glVertex2f(0, - 10);
      }
      GL11.glEnd();
      GL11.glPopMatrix();
   }


That's how i would do it
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tom_mai78101
« Reply #3 - Posted 2014-01-05 00:38:04 »

1. Why is there another glTranslate right before glPopMatrix? this won't do anything cause glPopMatrix undos it.

2. If you set The Position of the Ship with a glTranslate why is there x and y coordinates in the glVertex2f methods? doesn't make sense to me

1. Hm... I guess I didn't clean up my code after doing many different glTranslate() and glRotate() combos.

2. I thought that glTranslatef() moves the rotation pivot point to (x, y), then glRotate3f() rotates the entire view around the pivot point, and then we do glTranslatef() to move the pivot point back to the origin.

Try this:
<code>
That's how i would do it

Okay, it's fixed. The glTranslatef() in your code has the X and Y coordinates inverted, so I didn't see my ship at first. It was when I was playing around with it, do then realized my ship had been rendering at the wrong spot.

And then once the ship is showing up, the ship's rotation became incorrect. Been working on that for quite a while, until it clicked that I was passing radians to glRotate3f(), when it actually needed inverted degrees. So, got that fixed, and now my ship is working.

Here's the working code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
   public void draw() {
      GL11.glPushMatrix();
      GL11.glTranslatef(x, y, 0f);
      GL11.glRotatef((float) -(Math.toDegrees(angle)), 0f, 0f, 1f);
      GL11.glColor3f(0f, 1f, 1f);
      GL11.glBegin(GL11.GL_LINE_LOOP);
      {
         GL11.glVertex2i(0, -10);
         GL11.glVertex2i(8, 10);
         GL11.glVertex2i(0, 5);
         GL11.glVertex2i(-8, 10);
         GL11.glVertex2i(0, -10);
      }
      GL11.glEnd();
      GL11.glPopMatrix();
      GL11.glColor3f(1f, 1f, 1f);
   }


In the end, Seiya02 gave me a wonderful hint. Thank you!
Offline Seiya02

JGO Coder


Medals: 3
Projects: 4
Exp: 5 years


No Text Here :x


« Reply #4 - Posted 2014-01-05 16:52:43 »

Couldn't know your angle was in radians haha Cheesy
I used inverted x and y coordinates cause you did it too in your example code Cheesy
Glad i could help you
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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (48 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!