[LWJGL] Translate a gun at a fixed position in the screen

Hello forum,

I’m am very new to this forum and this is my first post. I’m sorry for my English, it could be a bit wrong sometimes but i’ll try my best.

Now the actual question:

In my 3d game I have a model of a gun. Like in almost every shooter you have a gun in the bottom-right corner of the screen, independently of the point you’re looking at. I want this in my game to and I’ve already managed to turn the model as it should and it is in the correct position for turning left and right. The only problem is now, that I can’t manage it for looking up and down, the gun stays in the wrong spot.

Here is some of my code:

float z = (float) ((Math.sin(Math.toRadians(cam.getRY() + 90 * 2))) / -1 + (Math.sin(Math.toRadians(cam.getRY() + 90 * 1))) * -2);
			float x = (float) ((Math.cos(Math.toRadians(cam.getRY() + 90 * 2))) / -1 + (Math.cos(Math.toRadians(cam.getRY() + 90 * 1))) * -2);
			glTranslatef(-cam.getX() + x, -cam.getY() - 1, -cam.getZ() + z);
			glRotatef(-cam.getRY() - 90, 0, 1, 0);
			glRotatef(cam.getRX(), 0, 0, 1);
			glScalef(0.05f, 0.05f, 0.05f);
			glCallList(model);

I hope someone can help me!

I think that you should set camera position to “neutral” (like for interface rendering) and then render weapon - it will be fully independend from player rotation, position etc.

What do you mean with neutral? I can’t imagine what you are saying. Could you give me a bit more information, maybe with some sample code?

Sample rendering code from one of my prototypes. I am writing 2d games, but I think that this should look rather similar in 3d games:

        GCore.renderer.startDrawing(); //function which is initializing my rendering system
            GCore.renderer.setCamera(logic.ship.x, logic.ship.y); //set camera position to ship center - in your case also set perspective matrix.
            logic.ship.draw(); //draw ship
            GCore.renderer.setCamera(WIDTH/2, HEIGHT/2); //set "neutral" camera for drawing interface - in your case you should keep perspective matrix there, render your gun and then set ortho matrix and render 2d elements of interface
            logic.menu.draw(); //interface rendering
        GCore.renderer.endRendering();

By “neutral” I mean constant camera positions.

Sorry, but this does not look like regular LWJGL openGL rendering code. Is this correct?

I am using another OpenGL versions, so my code probably will not tell you too much, but I can always help from the theoretical side. :wink:

In short, you should:
-set your “standard” 3d rendering camera
-draw things like map, enemies etc.
-set contant 3d rendering camera
-draw 3d things which are always in the same place (gun)
-set constant 2d rendering camera
-draw 2d things which are always in the same place (interface)

You wrapping OpenGL in other methods is probably why the OP got confused.

Hi

Look at this at line 364 and this at line 858. This code comes from the alpha version of my first person shooter and should be trivial to port to LWJGL. Good luck.

Thanks. Could you say which rocket launcher position is the one for up and down in the second file?

The array contains [x,y,z,angle]. The angle is the rotation angle around the axis Oy centered on the rocket launcher. This source code is far from perfect as it was not possible to look up and down in this version. It’s possible in the pre-beta version but the source code relies on an engine, it wouldn’t be helpful for you as you wish to use a low level Java binding.

With the information of your project I can’t seem to fix it. And I don’t know how I use the stuff with fixed camera or something.

Can you show me the code you use to handle a single rotation that works so that I can understand what is really wrong? The ordinate shouldn’t be constant.

float z = (float) ((Math.sin(Math.toRadians(Camera.getRY() + 90 * 2))) / -1 + (Math.sin(Math.toRadians(Camera.getRY() + 90 * 1))) * -2);
            float x = (float) ((Math.cos(Math.toRadians(Camera.getRY() + 90 * 2))) / -1 + (Math.cos(Math.toRadians(Camera.getRY() + 90 * 1))) * -2);
            glTranslatef(-Camera.getX() + x, -Camera.getY() - 1, -Camera.getZ() + z);
            glRotatef(-Camera.getRY() - 90, 0, 1, 0);
            glRotatef(Camera.getRX(), 0, 0, 1);

This is my code for the left and right turning.

What about using glLoadIdentity() and glClear(GL_DEPTH_BUFFER_BIT)? That’s what I do to render anything at a fixed point on screen and have it not collide with terrain etc.