Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a bullet move towards a point in 3Dimensional space

Tags:

I am currently making a 3D first person shooter with java LWJGL. I want to turn and move a bullet towards a specified point in the world. I managed to make the bullet turn on the Y-axis but not the X and Z. How can i make the bullet turn on the Z and X-axis and then move towards the point?

Here is my Bullet Class:

package entities;

import org.lwjgl.util.vector.Vector3f;

import models.TexturedModel;
import renderEngine.DisplayManager;
import toolbox.MousePicker;

public class Bullet extends Entity{

private static Vector3f currentRay = new Vector3f();
private static final float RAY_RANGE = 600;
public static boolean reset = true;
public Bullet(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, float scale) {
    super(model, position, rotX, rotY, rotZ, scale);

}
public void move(Bullet b){
    float distance =  2 * DisplayManager.getFrameTimeSeconds();
    currentRay = MousePicker.calculateMouseRay();
    Vector3f endPoint = MousePicker.getPointOnRay(currentRay, 10000);
    //I want my Bullet to move towards the Vector3f endPoint

    float zDistance = endPoint.z - this.getPosition().z;
    float xDistance = endPoint.x - this.getPosition().x;
    double angleToTurn = Math.toDegrees(Math.atan2(xDistance,     zDistance));
    this.setRotY((float)angleToTurn);
    float dx = (float) (distance * Math.sin(Math.toRadians(super.getRotY())));
    float dz = (float) (distance * Math.cos(Math.toRadians(super.getRotY())));

    super.increasePosition(dx, 0, dz);


}
    }
like image 869
Disser Avatar asked Feb 09 '17 19:02

Disser


1 Answers

What you want to do is to get the speed required to make your bullet closer to your target (here the mouse) endPoint.

So first, you get the vector between the two endPoint.sub(position);

Then you normalize() it to get the direction.

You scale() it with your desired speed to get the instant speed.

and you super.increasePosition(speed.x, speed.y, speed.z); to make it move toward the target

like image 102
Pignic Avatar answered Sep 21 '22 11:09

Pignic