Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (582) Games in Android Showcase (163) games submitted by our members Games in WIP (633) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Math Problem with Rotation  (Read 1000 times) 0 Members and 1 Guest are viewing this topic.
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();`

x = X part of Player's ViewVector
y = Y part of Player's ViewVector
z = Z part of Player's ViewVector

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

EDIT: If this healps: I use JOGL.
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).
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 hereglVertex3d(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.

 Ralphanese (19 views) 2015-05-29 13:07:55 Gibbo3771 (17 views) 2015-05-29 06:54:17 Ralphanese (23 views) 2015-05-28 16:52:52 theagentd (31 views) 2015-05-27 22:21:17 theagentd (37 views) 2015-05-27 22:21:08 CopyableCougar4 (27 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (33 views) 2015-05-23 20:23:34 Waterwolf (38 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21
 Riven 26x Rayvolution 24x theagentd 22x BurntPizza 20x Spasi 17x orangepascal 16x KevinWorkman 15x Drenius 15x ra4king 13x DavidBVal 12x princec 12x opiop65 12x Husk 11x EgonOlsen 10x kevglass 9x Slyth2727 8x
 Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02
 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