Jump to content

Java LWJGL How to calculate direction when firing a gun.

SirPaul

Hello Guys,

 

So i am totally lost with my "project".  I am developing a lot in Unity but i wanted to go a classic way and create a game from screatch using lwjgl. I figured out many things (procedural generation, chunks, loading mesh e.t.c) but my problem is : How it is with those Math.cos , Math.sin , when calculating an angle to fire at.  I can fire in front of me. which  is good, but i cannot fire 'Up' or 'Down'  so basicallly direction of Particle is not altered by Camera's YAW or Pitch.

Render method in my Main Class.private void render() {    // Clear the screen and depth buffer    GL11.glLoadIdentity();           camera.lookThrough();   GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);    //ch0.render();             GL11.glPushMatrix();    x = -camera.getPosition().x;    y = -camera.getPosition().y;    z = -camera.getPosition().z;         GL11.glTranslatef(-camera.getPosition().x ,camera.getPosition().y  ,-camera.getPosition().z );     GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);      GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);      if (Mouse.isButtonDown(0)){      yaw = -camera.getYaw();      pitch =-camera.getPitch();    //Spawn a box          Box b = new Box(x ,y  ,z,0.1f);               spawnedBoxes.add(b);         }      GL11.glPopMatrix();      for(int i = 0 ; i < spawnedBoxes.size();i++){                            // push box forward..       spawnedBoxes.get(i).centerZ-=4f;   --------------------- I think Here is the problem, i should Alter value of spawnedBoxes.get(i).centerX and centerY with the camera.getYaw or camera.getPitch  but i cant get the math right.                                                                    ---------------------           spawnedBoxes.get(i).render();        }                      //}          }

Camera CLASS : 

import org.lwjgl.opengl.GL11;import org.lwjgl.util.vector.Vector3f;import static org.lwjgl.opengl.GL11.*;public class FPSCameraController {  //3d vector to store the camera's position in    private Vector3f    position    = null;    /**  * @return the position  */ public Vector3f getPosition() {  return position; } //the rotation around the Y axis of the camera    private float       yaw         = 0.0f;    public float getYaw() {  return yaw; } public float getPitch() {  return pitch; } //the rotation around the X axis of the camera    private float       pitch       = 0.0f;  //Constructor that takes the starting x, y, z location of the camera    public FPSCameraController(float x, float y, float z)    {        //instantiate position Vector3f to the x y z params.        position = new Vector3f(x, y, z);    }  //increment the camera's current yaw rotation    public void yaw(float amount)    {        //increment the yaw by the amount param        yaw += amount;    }  //increment the camera's current yaw rotation    public void pitch(float amount)    {        //increment the pitch by the amount param        pitch += amount;    }  //moves the camera forward relative to its current rotation (yaw)    public void walkForward(float distance)    {        position.x -= distance * (float)Math.sin(Math.toRadians(yaw));        position.z += distance * (float)Math.cos(Math.toRadians(yaw));    }        public void flyUp(float distance)    {        position.y -= distance * 2;      //  position.z += distance * (float)Math.cos(Math.toRadians(yaw));    }    public void flyDown(float distance)    {        position.y += distance * 2;      //  position.z += distance * (float)Math.cos(Math.toRadians(yaw));    }         //moves the camera backward relative to its current rotation (yaw)    public void walkBackwards(float distance)    {        position.x += distance * (float)Math.sin(Math.toRadians(yaw));        position.z -= distance * (float)Math.cos(Math.toRadians(yaw));    }         //strafes the camera left relitive to its current rotation (yaw)    public void strafeLeft(float distance)    {        position.x -= distance * (float)Math.sin(Math.toRadians(yaw-90));        position.z += distance * (float)Math.cos(Math.toRadians(yaw-90));    }         //strafes the camera right relitive to its current rotation (yaw)    public void strafeRight(float distance)    {        position.x -= distance * (float)Math.sin(Math.toRadians(yaw+90));        position.z += distance * (float)Math.cos(Math.toRadians(yaw+90));    }    //translates and rotate the matrix so that it looks through the camera    //this dose basic what gluLookAt() does    public void lookThrough()    {        //roatate the pitch around the X axis        GL11.glRotatef(pitch, 1.0f, 0.0f, 0.0f);        //roatate the yaw around the Y axis        GL11.glRotatef(yaw, 0.0f, 1.0f, 0.0f);        //translate to the position vector's location        GL11.glTranslatef(position.x, position.y, position.z);    } }
Box.java import org.lwjgl.opengl.GL11;public class Box { private boolean isActive = false; float centerX, centerY, centerZ, size, yTop; public float getyTop() {  return yTop; } public void setyTop(float yTop) {  this.yTop = yTop; } public float getCenterZ() {  return centerZ; } public void setCenterZ(float centerZ) {  this.centerZ = centerZ; } public float getCenterY() {  return centerY; } public void setCenterY(float centerY) {  this.centerY = centerY; } public float getCenterX() {  return centerX; } public void setCenterX(float centerX) {  this.centerX = centerX; } public float getSize() {  return size; } public void setSize(float size) {  this.size = size; } float red, green, blue; public Box(float a, float b, float c, float d) {  centerX = -a;  centerY = b;  centerZ = -c;  yTop = -(centerY + d/2);  size = d;  red = 128;  green = 128;  blue = 1; } public Box(float a, float b, float c, float d, float r, float g, float bl) {  this(a, b, c, d);  red = r;  green = g;  blue = bl; } public void render() {  // Top//  TextureManager.textureWall.bind();  GL11.glBegin(GL11.GL_QUADS);  //GL11.glPolygonMode( GL11.GL_FRONT_AND_BACK, GL11.GL_FILL );  //GL11.glTranslatef(-centerX, centerY, -centerZ);    GL11.glTexCoord2f(0, 0);  GL11.glVertex3f(-centerX + size, centerY + size, -centerZ - size);  GL11.glTexCoord2f(1, 0);  GL11.glVertex3f(-centerX - size, centerY + size, -centerZ - size);  GL11.glTexCoord2f(1, 1);  GL11.glVertex3f(-centerX - size, centerY + size, -centerZ + size);  GL11.glTexCoord2f(0, 1);  GL11.glVertex3f(-centerX + size, centerY + size, -centerZ + size);  // Bottom  GL11.glTexCoord2f(0, 0);  GL11.glVertex3f(-centerX + size, centerY - size, -centerZ + size);  GL11.glTexCoord2f(1, 0);  GL11.glVertex3f(-centerX - size, centerY - size, -centerZ + size);  GL11.glTexCoord2f(1, 1);  GL11.glVertex3f(-centerX - size, centerY - size, -centerZ - size);  GL11.glTexCoord2f(0, 1);  GL11.glVertex3f(-centerX + size, centerY - size, -centerZ - size);  // One side  GL11.glTexCoord2f(0, 0);  GL11.glVertex3f(-centerX + size, centerY + size, -centerZ + size);  GL11.glTexCoord2f(1, 0);  GL11.glVertex3f(-centerX - size, centerY + size, -centerZ + size);  GL11.glTexCoord2f(1, 1);  GL11.glVertex3f(-centerX - size, centerY - size, -centerZ + size);  GL11.glTexCoord2f(0, 1);  GL11.glVertex3f(-centerX + size, centerY - size, -centerZ + size);  // Moar sides  GL11.glTexCoord2f(0, 0);  GL11.glVertex3f(-centerX + size, centerY - size, -centerZ - size);  GL11.glTexCoord2f(1, 0);  GL11.glVertex3f(-centerX - size, centerY - size, -centerZ - size);  GL11.glTexCoord2f(1, 1);  GL11.glVertex3f(-centerX - size, centerY + size, -centerZ - size);  GL11.glTexCoord2f(0, 1);  GL11.glVertex3f(-centerX + size, centerY + size, -centerZ - size);  // Last side  GL11.glTexCoord2f(0, 0);  GL11.glVertex3f(-centerX - size, centerY + size, -centerZ + size);  GL11.glTexCoord2f(1, 0);  GL11.glVertex3f(-centerX - size, centerY + size, -centerZ - size);  GL11.glTexCoord2f(1, 1);  GL11.glVertex3f(-centerX - size, centerY - size, -centerZ - size);  GL11.glTexCoord2f(0, 1);  GL11.glVertex3f(-centerX - size, centerY - size, -centerZ + size);  // Real last side  GL11.glTexCoord2f(0, 0);  GL11.glVertex3f(-centerX + size, centerY + size, -centerZ - size);  GL11.glTexCoord2f(1, 0);  GL11.glVertex3f(-centerX + size, centerY + size, -centerZ + size);  GL11.glTexCoord2f(1, 1);  GL11.glVertex3f(-centerX + size, centerY - size, -centerZ + size);  GL11.glTexCoord2f(0, 1);  GL11.glVertex3f(-centerX + size, centerY - size, -centerZ - size);  GL11.glEnd(); } public boolean isActive() {  return isActive; } public void setActive(boolean isActive) {  this.isActive = isActive; }}

Would someone be so kind to explain me how to do such math ?

 

Here is a video somewhat describing the issue :

 

 

Thank you all for all suggestions .

Link to comment
Share on other sites

Link to post
Share on other sites

Oh,I remember bumping into that too.

Lemme check to see if I still have the project inside my Eclipse workspace...

Ah,no I don't.

 

Here :

spawnedBoxes.get(i).centerZ-=4f;

 

When you do that,you push the box whatever it the direction of the Z axis ( X , Y and Z come together in the camera).

You need to calculate where the box actually goes.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Oh,I remember bumping into that too.

Lemme check to see if I still have the project inside my Eclipse workspace...

Ah,no I don't.

:D But thanks for the effort :)   

Link to comment
Share on other sites

Link to post
Share on other sites

:D But thanks for the effort :)   

I updated my initial post.

 

You need to move the box in all 3 directions.

Does 

 

spawnedBoxes.get(i).centerX-=4f;

spawnedBoxes.get(i).centerY-=4f;

spawnedBoxes.get(i).centerZ-=4f;

 

Work?

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

I updated my initial post.

 

You need to move the box in all 3 directions.

Does 

 

spawnedBoxes.get(i).centerX-=4f;

spawnedBoxes.get(i).centerY-=4f;

spawnedBoxes.get(i).centerZ-=4f;

 

Work?

Well , how do you mean it, because the code does work.  but cube is sent somewhere diagonaly across the screen (to the 4,4,4 point );

It seems like simple mathematics (and it most certainly is) but i just cannot figure it out.

Link to comment
Share on other sites

Link to post
Share on other sites

Well , how do you mean it, because the code does work.  but cube is sent somewhere diagonaly across the screen (to the 4,4,4 point );

It seems like simple mathematics (and it most certainly is) but i just cannot figure it out.

Oh,right.Didn't think about that.

Let me think a little bit.

We need to calculate the distance it is offset in each axes.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Oh,right.Didn't think about that.

Let me think a little bit.

We need to calculate the distance it is offset in each axes.

If we will get this today you will totally return my confidence to continue coding :D i have spent too much time by this . 

Link to comment
Share on other sites

Link to post
Share on other sites

If we will get this today you will totally return my confidence to continue coding :D i have spent too much time by this . 

Well, we already managed to walk diagonally.

(float)Math.sin(Math.toRadians(yaw))

Try using that or something.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Well, we already managed to walk diagonally.

(float)Math.sin(Math.toRadians(yaw))

Try using that or something.

 yea i tried exactly that, but it doesnt work as it should .  

Link to comment
Share on other sites

Link to post
Share on other sites

Well, we already managed to walk diagonally.

(float)Math.sin(Math.toRadians(yaw))

Try using that or something.

 

Actually, that kind of works, but have a very weak range.   so it should be something along those lines, but dont really know how.. 

Link to comment
Share on other sites

Link to post
Share on other sites

Actually, that kind of works, but have a very weak range.   so it should be something along those lines, but dont really know how.. 

 

Okey so this is the finest i could get .

 

  spawnedBoxes.get(i).centerX-=(float)Math.sin(Math.toRadians(yaw * 4));
  spawnedBoxes.get(i).centerY-=(float)Math.sin(Math.toRadians(-pitch  * 4  ));

This does something like this : 

 

Any suggestions ?

Link to comment
Share on other sites

Link to post
Share on other sites

 

Okey so this is the finest i could get .

 

  spawnedBoxes.get(i).centerX-=(float)Math.sin(Math.toRadians(yaw * 4));
  spawnedBoxes.get(i).centerY-=(float)Math.sin(Math.toRadians(-pitch  * 4  ));

This does something like this : 

 

Any suggestions ?

 

That still moves all boxes.You need to create different instances(objects) for each one of them.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

That still moves all boxes.You need to create different instances(objects) for each one of them.

 

I know thats not the problem, i can solve that in a second, its just for testing.   

I need to actually fix the 'range' of how much to the left and right can box spawn.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×