Alright, so I made a Mother Ship that can point at an enemy using the Math I'm currently learning in my High School Geometry class, but the bullet that the ship shoots does not go in the direction the ship is shooting unless the ship is pointing at a perfect right angle.
the bullets get spawned in with a ‘BulletDirection’ variable supplied by the ship that spawned it. In the ship class this is the code used to get the direction, I added 90 to the end to flip it around as they were pointing backwards.
GunDirection = (int) Math.toDegrees(Math.atan2(TargetShip.getPosition().y - Position.y, TargetShip.getPosition().x - Position.x))-90;
then later created in the bulletList
bulletList.add(new Bullet( (int) GunDirection , (int) Position.x + (240/2), (int) Position.y + (240/2), this.Faction, BulletTexture));
and then there’s the bullet class:
public class Bullet {
private int bulletDirection;
private float bulletSpeed = 3f;
private int bulletX, bulletY;
private int Faction = 0;
private boolean isAlive;
private Texture bulletTexture;
private Rectangle rect;
public Bullet(int bulletDirection, int bulletX, int bulletY, int Faction, Texture bulletTexture){
this.bulletDirection = bulletDirection;
this.setBulletX((int) ((bulletX +Math.cos(bulletDirection))));
this.setBulletY((int) ((bulletY+Math.sin(bulletDirection))));
this.bulletTexture = bulletTexture;
this.setFaction(Faction);
rect = new Rectangle();
rect.setSize(12,12);
}
public void render(SpriteBatch batch){
batch.draw(bulletTexture, bulletX, bulletY);
}
public void update(){
rect.x = bulletX;
rect.y = bulletY;
bulletX += Math.cos(Math.toRadians(bulletDirection + 90) )*bulletSpeed;
bulletY += Math.sin(Math.toRadians(bulletDirection + 90))*bulletSpeed;
}
This is what the picture for the gun starts off as:
P.S. Sorry for the barrage of questions this past week, I try my best to keep stuff specific and spent at least a few hours googling and applying before I give up in frustration and end up here. I hope my newbie-ness is not bothering anyone :clue: