Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  [LWJGL] Translate a gun at a fixed position in the screen  (Read 1775 times)
0 Members and 1 Guest are viewing this topic.
Offline LostWarrior

Senior Devvie


Medals: 7
Exp: 5 years



« Posted 2013-03-04 18:46:12 »

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:

1  
2  
3  
4  
5  
6  
7  
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!

Cows aren't sheep, because pigs already are.
Offline Mac70
« Reply #1 - Posted 2013-03-04 19:07:46 »

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.

Check out my Devblog! Smiley
Offline LostWarrior

Senior Devvie


Medals: 7
Exp: 5 years



« Reply #2 - Posted 2013-03-04 19:21:44 »

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?

Cows aren't sheep, because pigs already are.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mac70
« Reply #3 - Posted 2013-03-04 19:44:02 »

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:

1  
2  
3  
4  
5  
6  
        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.

Check out my Devblog! Smiley
Offline LostWarrior

Senior Devvie


Medals: 7
Exp: 5 years



« Reply #4 - Posted 2013-03-04 20:01:36 »

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

Cows aren't sheep, because pigs already are.
Offline Mac70
« Reply #5 - Posted 2013-03-04 20:26:32 »

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)

Check out my Devblog! Smiley
Offline Sickan

Senior Devvie


Medals: 9



« Reply #6 - Posted 2013-03-04 20:51:01 »

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.
Offline gouessej
« Reply #7 - Posted 2013-03-04 23:42:31 »

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.

Offline LostWarrior

Senior Devvie


Medals: 7
Exp: 5 years



« Reply #8 - Posted 2013-03-05 13:50:29 »

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

Cows aren't sheep, because pigs already are.
Offline gouessej
« Reply #9 - Posted 2013-03-05 21:50:43 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline LostWarrior

Senior Devvie


Medals: 7
Exp: 5 years



« Reply #10 - Posted 2013-03-07 19:01:37 »

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.

Cows aren't sheep, because pigs already are.
Offline gouessej
« Reply #11 - Posted 2013-03-07 23:24:26 »

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.

Offline LostWarrior

Senior Devvie


Medals: 7
Exp: 5 years



« Reply #12 - Posted 2013-03-08 14:50:28 »

1  
2  
3  
4  
5  
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.

Cows aren't sheep, because pigs already are.
Offline Quarry
« Reply #13 - Posted 2013-03-08 21:11:45 »

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

rwatson462 (33 views)
2014-12-15 09:26:44

Mr.CodeIt (24 views)
2014-12-14 19:50:38

BurntPizza (51 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (46 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (61 views)
2014-11-29 21:32:03

toopeicgaming1999 (124 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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