Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (616)
Games in Android Showcase (173)
games submitted by our members
Games in WIP (659)
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 1059 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)

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

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
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?

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

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


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

Coldstream24 (19 views)
2015-09-03 00:41:28

Andrew_3ds (31 views)
2015-09-01 19:08:10

afikri (20 views)
2015-08-31 09:30:22

afikri (27 views)
2015-08-31 09:30:07

afikri (15 views)
2015-08-31 09:27:24

afikri (17 views)
2015-08-31 09:26:40

Roquen (32 views)
2015-08-29 11:30:54

GamerC4 (41 views)
2015-08-22 20:38:50

GamerC4 (37 views)
2015-08-22 20:37:18

GamerC4 (43 views)
2015-08-22 20:37:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 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‑
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!