Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Math Problem with Rotation  (Read 837 times)
0 Members and 1 Guest are viewing this topic.
Offline bk0

Senior Newbie





« Posted 2012-02-24 10:49:44 »

Hi everybody

In my game, the user can look around using the mouse. The mouse position is translated into viewangle_vertical and viewangle_horizontal. I use those variables to compute the correct values for GLULookAt. That works great so far.
(Just to clarify: viewangle_vertical is always between -PI/2 and PI/2)
Now I'd like to have a model at the player location that looks in the same direction as the player does. Similar to a Gun in a Shooter (the gun moves with the player, and always points in the player's direction).

I already have my Object Loader, so rendering the thing isn't a problem. Here's my rendering code: (simplieifed)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
glPushMatrix();

glLoadIdentity();
gluLookAt(pos.x, pos.y, pos.z, lookat.x, lookat.y, lookat.z, 0, 1, 0);
     
      glTranslated(x, y, z);
      glRotated(rot_x, 1, 0, 0);
      glRotated(rot_y, 0, 1, 0);
      glRotated(rot_z, 0, 0, 1);
     
      //Actual call to glDrawArrays or similar
     
      glPopMatrix();


I already figured out that
x = X part of Player's ViewVector
y = Y part of Player's ViewVector
z = Z part of Player's ViewVector
rot_y = viewangle_horizontal*RAD2ANG (RAD2ANG = 180.0 / Math.PI)

So as long as viewangle_vertical equals 0, this works perfectly. (My gun stays clearly visible in front of the player, and points away from the player)
If I look up or down, however, the Gun is at the correct position, but isn't rotated correctly (well, rot_x and rot_z = 0, so this isn't surprising)

I tried setting
1  
2  
rot_x = Math.abs(Math.cos(viewangle_horizontal))*180;
rot_z = Math.abs(Math.sin(viewangle_horizontal))*180;

But this turns out VERY bad, the gun just flings aroundwith no clear pattern (I have no idea ;( )

Can you guys give me some pointers to a tutorial that deals with this kind of math?

Thanks
Offline Danny02
« Reply #1 - Posted 2012-02-24 11:14:51 »

You can just display the gun not in world coordinats, reset the view matrix and place the gun directly in front of your view.
Perhaps you also want to reset the depthbuffer before rendering your gun so it won't intersect with world geometrie
Offline bk0

Senior Newbie





« Reply #2 - Posted 2012-02-24 12:59:34 »

That sounds like a great idea.

There are two things that I'd like to know:
1. How exactly do you render something "not in world coordinates"? Is there some gl* command that tells OpenGL to go to player coordinates?
2. How would this work in multiplayer? Say I have 3 people running around, I have to draw 3 guns facing away from each player. In the case of multiplayer I can't just use my view - is there a way to go to a point P, set the view towards another point V, then render the gun with respect to the view, and then go back again?

Thanks for the help Wink

EDIT: If this healps: I use JOGL.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-02-24 16:34:16 »

Everything you draw is relative to the camera looking down the Z axis through the origin.  When you apply the camera transform, you're moving everything to where it should be based on the camera's assumed position and orientation (even though it never really moves).

Since the gun is going to superimpose over the rest of the world, you're probably going to want to draw it last (then you can avoid having to depth-test against it).  To draw something always in front of the camera, simply undo the camera transform by either applying its inverse or loading identity or otherwise restoring the pre-camera coordinate system. 

In multiplayer you can just draw other players' model with their equipped weapon like you would any other model.  It's just the first-person view that doesn't need the full player model, just the gun (and probably a more detailed pair of hands).
Offline bk0

Senior Newbie





« Reply #4 - Posted 2012-02-24 16:49:21 »

Ah, I got it working. Indeed, all I needed was
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
glLoadIdentity();

glEnableClientState(GL.GL_POINTS);
glPointSize(25);
       
glBegin(GL.GL_POINTS);     //Instead of POINTS render an object here
glVertex3d(0, 0, -1);  
glEnd();

glDisableClientState(GL.GL_POINTS);


(The code is just if someone later finds this thread and would like a copy-paste solution)

Thanks again!
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.

BurntPizza (14 views)
2014-09-19 03:14:18

Dwinin (31 views)
2014-09-12 09:08:26

Norakomi (58 views)
2014-09-10 13:57:51

TehJavaDev (79 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (62 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (31 views)
2014-09-07 01:12:14

Longarmx (36 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!