Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  glRotate  (Read 2311 times)
0 Members and 1 Guest are viewing this topic.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Posted 2007-11-14 20:02:33 »

I've finally done some texturing of the ball in my game Hyper Blazer (http://www.gagaplay.com/hypblazer/hypblazer.jnlp but then of course it has to rotate according to movement:
It has to rotate forwards according to your speed, and left-right according to steering.

So what I did is this, which I already feared wouldn't do what I want:
1  
2  
3  
4  
5  
6  
GL11.glPushMatrix();
GL11.glTranslatef(0, y + DIAMETER, -3); // translate to pos of the ball
GL11.glRotatef(zRot, 1, 0, 0); // forward rotation
GL11.glRotatef(xRot, 0, 0, 1); // left-right rotation
drawBall();            
GL11.glPopMatrix();


Obviously, the left-right rotation doesn't do what I want because the orientation has been changed by the previous (forward) rotation.
The thing is, It's probably something trivial but I can't wrap my head around how the rotation should be done. I've googled for it, but what came up is some hefty matrix math that's waaaay above my head.  Embarrassed

Any ideas?

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #1 - Posted 2007-11-14 22:59:36 »

After reading some more about it, it doesn't seem as trivial as I hoped it would be (I googled for 'rotating around an arbitrary axis').

Although not really what I want, I now worked around it a bit in a somewhat acceptable way by 'steering' the ball like a car (I uploaded this version).
So now the ball is rotated around the y axis for steering, and then around the x axis for forward motion.

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 74
Projects: 15


★★★★★


« Reply #2 - Posted 2007-11-15 00:00:59 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
GL11.glPushMatrix();
GL11.glTranslatef(0, y + DIAMETER, -3); // translate to pos of the ball

GL11.glRotatef(zRot, 1, 0, 0); // forward rotation

GL11.glPushMatrix();
GL11.glRotatef(xRot, 0, 0, 1); // left-right rotation
drawBall();
GL11.glPopMatrix();

           
GL11.glPopMatrix();


bit rusty with my opengl but i believe you can use some glpush and glpop matrix stuff to solve this, not sure if the above code is correct but should point you in right direction.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tom
« Reply #3 - Posted 2007-11-15 00:51:37 »

Pseudo code to rotate a sphere so that it matches its movement:
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  
// level init
// the current ball transform
Matrix transform;

// per frame
Point a // position at start of frame
Point b // position at end of frame
Matrix frameRotation = getFrameRotation(a, b);

// add frame rotation to ball transform
rotation.mul(frameRotation)

// TODO: add frame translation

// set opengl matrix
GLXX.setMatrix(transform)

/**
 * Gets frame rotation given translation vector
 */

private Matrix getFrameRotation(Point a, Point b) {
   Vector ab = b - a;

   // rotate around the side axis wich is perpendicular to the forward and up vectors
  Vector axis = cross(ab, UP)

   // amount of rotation depends on the distance traveled and ball radius
  double angle = ab.length() / (2 * PI * radius)

   return matrixRotatedUsingAxisAngle (angke, axis);
}

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #4 - Posted 2007-11-15 10:38:48 »

@kapta
Thanks, but unless I'm totally misunderstanding something, doesn't your example just push the matrix once more without changing anything otherwise?  Huh

@tom
Thanks! I'll see if I can translate that into compilable code.

Offline cylab

JGO Ninja


Medals: 38



« Reply #5 - Posted 2007-11-15 11:17:51 »

Maybe you need to use quaternations to get your rotations right. They are merily a different notation which allows for easier combination of rotations. The Ogre guys have a primer on this: http://www.ogre3d.org/wiki/index.php/Quaternion_and_Rotation_Primer. There should be code java available somewhere as well... just google.

Mathias - I Know What [you] Did Last Summer!
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #6 - Posted 2007-11-15 18:39:34 »

Thanks for that, cylab.
Quarternations did come up during my googling. I was hoping there was an easy solution to my problem, but I guess just have to bite the bullet and learn to use those quarterthings  Smiley.

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 74
Projects: 15


★★★★★


« Reply #7 - Posted 2007-11-15 19:20:15 »

@kapta
Thanks, but unless I'm totally misunderstanding something, doesn't your example just push the matrix once more without changing anything otherwise?  Huh

it separates the rotations, so the first rotation does not affect the second, have you given it a try?
Offline tom
« Reply #8 - Posted 2007-11-15 22:46:39 »

It helps if you've got a vecmath lib when doing this kind of things. The LWJGL vector package is a bit too simplistic. Java3d got a good vecmath.jar that can be used.
Javadoc: http://java.sun.com/products/java-media/3D/forDevelopers/J3D_1_3_API/j3dapi/index.html.
download: https://java3d.dev.java.net/binary-builds.html (inside the zip binaries)

Quaternions and matrices can both be used to represent roatations, and they both do pretty much the same job. Since you need to send a matrix to oengl, you might as well use it all the way.

Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #9 - Posted 2007-11-16 18:48:01 »

Quote
it separates the rotations, so the first rotation does not affect the second, have you given it a try?

As a sanity check, I just tried it but there was indeed no change.
My understanding of pushing and popping the matrix is that glPushMatrix simply pushes the matrix on the stack and glPopMatrix restores it (pops it off the stack) again, so glPushMatrix doesn't affect the state of the current matrix. Your example simply stores the matrix once more but makes no difference otherwise; the second rotation is still affected by the first one.

@tom
Thanks again, that might indeed be useful.

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.

E.R. Fleming (8 views)
2014-07-29 03:07:13

E.R. Fleming (4 views)
2014-07-29 03:06:25

pw (38 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!