I’m using LWJGL an am trying to implement mechanics for shooting. So far I got it working correctly, but I have it slowing down as the mouse get’s closer to the starting position. This is correct, but not what im looking for. How can I make the bullet shoot at a constant speed no matter how close to the initial position?
Normalize the (dirX, dirY) vector. It’s a direction, it’s length should always be 1. The magnitude is specified later as bulletSpeed.
err, im not getting it.
class bul
{
float timeStep=1f/60f;
float turretX=300f;
float turretY=10f;
float bulletSpeed=2f;
double bulletX;
double bulletY;
double dirX;
double dirY;
public bul( float startX, float startY)
{
this.bulletX = startX;
this.bulletY = startY;
dirX = Math.sqrt(dirX * Mouse.getX());//where do i apply the mouse?
dirY = Math.sqrt(dirY * Mouse.getY());
}
public void update()
{
bulletX = bulletX + (dirX * bulletSpeed * timeStep);
bulletY = bulletY + (dirY * bulletSpeed * timeStep);
}
}
In your code, you are setting bullet direction to be mouseX/Y - startX/Y, so as you move your mouse further away, the result will be a bigger number, as you move it closer, it will be a smaller number. You use these numbers directly in your bullet update function in multiplication, so they end moving further and shorter depending on your mouse.
What BurntPizza is saying is that you need to normalize these values, which means to set the distance of a vector to 1. You have a function that already does this in your Vec2. Turn dirX/Y in to a Vec2 and normalize it. You then use the values inside the Vec2 in your bullet update function instead.