Some more info
o you need to understand 2d vectors
-> they are variables with an x and an y component and can be used to do stuff like described in the link above
o you need to understand the difference between vectors and points
-> technically they look the same (having x and y), but logically they are different (a point is a position, a vector represents direction and length)
o you need to understand game loops
-> the loop that periodically calls your update and render methods
o you need to understand what a timestep is
-> the time period between your update calls
o you need to understand fixed and variable timesteps
-> fixed timesteps are locked. For example to 1/60s.
o fixed timestep is easier for animating your game
-> but the gameloop needs to make sure of it by variable sleep times to wait before the next gameloop iteration
o you need to understand when to use floats vs. ints
-> only use ints for final rendering. use floats (or doubles) for variables needed in computation or to store (varying) positions
regarding your question:
o you need a direction vector pointing from the turret to the mouse
-> it’s easily calculated by substracting the turret position from the mouse position (and normalize the result)
o you need a speed variable containing the pixels per timestep
-> the amount of pixels the bullet should fly per frame
o you need a bullet position variable that adds up the directionspeedtimestep per update call
-> this needs to have float components
pseudo code:
// assume this values
float timeStep=1f/60f;
float turretX=10f;
float turrety=10f;
float bulletSpeed=3f; // speed is 3 pixels per second
// if a bullet is fired it is initialized like this
float bulletX=turretX;
float bulletY=turretY;
// and you need to calculate the direction vector for it's movement
float dirX= getMouseXFromSomeWhere()-turretX;
float dirY= getMouseYFromSomeWhere()-turretY;
// you need to "normalize" the direction vector to be able to use the speed variable
float dirLength= Math.sqrt(dirX*dirX + dirY*dirY);
dirX=dirX/dirLength;
dirY=dirY/dirLength;
// now on every update, you can add up the direction * speed * timestep to the bullet
bulletX=bulletX+(dirX*bulletSpeed*timeStep);
bulletY=bulletY+(dirY*bulletSpeed*timeStep);
// on every render, you can render the bullet sprite at that position
g.drawImage(bulletImage, (int)bulletX, (int)bulletY, null); // rendering often uses ints for coordinates
as you can see, standalone x and y variables are just the components of vectors grouped by a naming convention, so you could also use a vector class for that:
// some minimal vector class
class Vec
{
public float x, y;
public Vec(float x, float y)
{
this.x=x;
this.y=y;
}
public normalize()
{
float l=Math.sqrt(x*x + y*y);
x/=l;
y/=l;
}
}
// since vectors and points look technically similar, you can "abuse" a vector to store positions:
Vec turret= new Vec(10f,10f);
// creating the direction vector
Vec dir=new Vec(getMouseXFromSomewhere(),getMouseYFromSomewhere());
dir.x-=turret.x;
dir.y-=turret.y;
dir.normalize();
// initializing a new bullet
Vec bullet=new Vec(turret.x, turret.y);
// animate the bullet on update...
bullet.x+=speed*dir.x*timeStep;
bullet.y+=speed*dir.y*timeStep;
// render the bullet sprite
g.drawImage(bulletImage, (int)bullet.x, (int)bullet.y, null);
Of course you can add more methods to the Vec class to make the code using vectors look more concise…