This gets the direction of the gun and rotates it:
GunDirection = (int) Math.toDegrees(Math.atan2(TargetShip.getOriginY() - OriginPositionY, TargetShip.getOriginX() - OriginPositionX));
This makes the bullet:
bulletList.add(new Bullet( (int) GunDirection , (int) OriginPositionX, (int) OriginPositionY, Faction, BulletTexture));
This sets the bullets position right when it spawns:
this.setBulletX((int) (bulletX + Math.cos(Math.toRadians(bulletDirection))));
this.setBulletY((int) (bulletY + Math.sin(Math.toRadians(bulletDirection))));
And this moves the bullet in the direction its supposed to go:
bulletX += Math.cos(Math.toRadians(bulletDirection))*bulletSpeed;
bulletY += Math.sin(Math.toRadians(bulletDirection))*bulletSpeed;
These are all the code snippets of where any math is used for this, I don’t understand what else you need, unless you want the actual class file of the Mother ship and bullet, then here
:
MotherShip
package com.me.eclipse.entities;
import java.util.ArrayList;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
public class Ship_MotherShip implements Ship {
public int Faction = 0, Hull = 500, upKeep;
public Vector2 Position = new Vector2();
private TextureRegion Ship_Base, Ship_Top, Ship_Gun;
private Texture ShipSelect, BulletTexture;
private int BaseDirection, TopDirection, GunDirection = 90;
private int Width, Height, OriginX, OriginY, OriginPositionX, OriginPositionY, shootDelay = 0;
private int GoalX, GoalY, Speed = 3;
private boolean isHovered = false, isSelected = false;
private Ship TargetShip;
private Rectangle rect;
private ArrayList<Bullet> bulletList;
public Ship_MotherShip(int x, int y, TextureRegion Ship_Base,
TextureRegion Ship_Top, TextureRegion Ship_Gun, Texture ShipSelect, ArrayList<Bullet> bulletList) {
this.Ship_Base = Ship_Base;
this.Ship_Top = Ship_Top;
this.Ship_Gun = Ship_Gun;
this.ShipSelect = ShipSelect;
this.Position.x = x;
this.Position.y = y;
this.GoalX = x;
this.GoalY = y;
this.Hull = 500;
this.upKeep = 0;
this.Faction = 0;
this.Width = Ship_Base.getRegionWidth();
this.Height = Ship_Base.getRegionHeight();
this.OriginX = Width/2;
this.OriginY = Height/2;
this.OriginPositionX = (int) Position.x + OriginX;
this.OriginPositionY = (int) Position.y + OriginY;
this.bulletList = bulletList;
rect = new Rectangle();
rect.setWidth(Ship_Base.getRegionWidth());
rect.setHeight(Ship_Base.getRegionHeight());
BulletTexture = new Texture(Gdx.files.internal("data/Ships/Bullet.png"));
}
@Override
public void render(SpriteBatch batch) {
System.out.println("Rednering");
batch.draw(Ship_Base, Position.x, Position.y, OriginX, OriginY, Width,
Height, 1, 1, BaseDirection, false);
batch.draw(Ship_Top, Position.x, Position.y, OriginX, OriginY, Width,
Height, 1, 1, TopDirection, false);
batch.draw(Ship_Gun, Position.x, Position.y, OriginX, OriginY, Width,
Height, 1, 1, GunDirection, false);
if (isSelected) {
batch.draw(ShipSelect, Position.x, Position.y);
}
}
@Override
public void renderGlobal(Texture GreenGlobal, Texture RedGlobal,
Texture WhiteGlobal, SpriteBatch batch) {
if (Faction == 0) {
batch.draw(WhiteGlobal, Position.x - 380, Position.y - 280);
}
if (Faction == 1) {
batch.draw(GreenGlobal, Position.x - 380, Position.y - 280);
}
if (Faction == 2) {
batch.draw(RedGlobal, Position.x - 380, Position.y - 280);
}
}
@Override
public void update(Vector3 MousePos, Sound select) {
this.OriginPositionX = (int) Position.x + OriginX;
this.OriginPositionY = (int) Position.y + OriginY;
rect.setPosition(Position.x, Position.y);
BaseDirection += 1;
shootDelay += 1;
targetEnemy();
if(shootDelay >= 150){
Shoot();
shootDelay -= shootDelay;
}
/* Check for mouse hover and selection */
if (MousePos.x < Position.x + Width && MousePos.x > Position.x
&& MousePos.y < Position.y + Height && MousePos.y > Position.y) {
isHovered = true;
} else {
isHovered = false;
}
if (isHovered && isSelected == false && Faction == 1
&& Gdx.input.isButtonPressed(Buttons.LEFT)) {
select.play();
setSelected(true);
}
if (isHovered == false && Gdx.input.isButtonPressed(Buttons.LEFT)) {
setSelected(false);
;
}
/* Check for mouse move actions */
if (isSelected == true && Gdx.input.isButtonPressed(Buttons.RIGHT)) {
moveTo((int) MousePos.x, (int) MousePos.y);
}
/* Move to Goal */
if (Position.x < GoalX) {
Position.x += Speed;
}
if (Position.x > GoalX) {
Position.x -= Speed;
}
if (Position.y < GoalY) {
Position.y += Speed;
}
if (Position.y > GoalY) {
Position.y -= Speed;
}
}
@Override
public void findEnemy(Ship ship) {
if (TargetShip == null && ship.getFaction() != this.Faction) {
this.TargetShip = ship;
System.out.println("Target Acquired");
}
}
@Override
public void targetEnemy() {
if (TargetShip != null) {
GunDirection = (int) Math.toDegrees(Math.atan2(TargetShip.getOriginY() - OriginPositionY, TargetShip.getOriginX() - OriginPositionX));
System.out.println(GunDirection);
}
}
@Override
public void moveTo(int x, int y) {
this.GoalX = x;
this.GoalY = y;
}
@Override
public int getFaction() {
return Faction;
}
@Override
public void setFaction(int faction) {
this.Faction = faction;
}
@Override
public int getHull() {
return Hull;
}
@Override
public void setHull(int hull) {
this.Hull = hull;
}
@Override
public int getUpkeep() {
return upKeep;
}
@Override
public void setUpkeep(int upkeep) {
this.upKeep = upkeep;
}
@Override
public Vector2 getPosition() {
return Position;
}
@Override
public void setPosition(Vector2 position) {
this.Position = position;
}
public int getGoalX() {
return GoalX;
}
public void setGoalX(int goalX) {
GoalX = goalX;
}
public int getGoalY() {
return GoalY;
}
public void setGoalY(int goalY) {
GoalY = goalY;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean isSelected) {
this.isSelected = isSelected;
}
@Override
public int getSpeed() {
return Speed;
}
@Override
public Rectangle getRectangle() {
return rect;
}
@Override
public void Shoot(){
bulletList.add(new Bullet( (int) GunDirection , (int) OriginPositionX, (int) OriginPositionY, Faction, BulletTexture));
}
@Override
public int getOriginX() {
return OriginPositionX;
}
@Override
public int getOriginY() {
return OriginPositionY;
}
}
Bullet
package com.me.eclipse.entities;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
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(Math.toRadians(bulletDirection))));
this.setBulletY((int) (bulletY + Math.sin(Math.toRadians(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))*bulletSpeed;
bulletY += Math.sin(Math.toRadians(bulletDirection))*bulletSpeed;
}
public boolean isAlive() {
return isAlive;
}
public void setAlive(boolean isAlive) {
this.isAlive = isAlive;
}
public int getBulletX() {
return bulletX;
}
public void setBulletX(int bulletX) {
this.bulletX = bulletX;
}
public int getBulletY() {
return bulletY;
}
public void setBulletY(int bulletY) {
this.bulletY = bulletY;
}
public Rectangle getRectangle(){
return rect;
}
public int getFaction() {
return Faction;
}
public void setFaction(int faction) {
Faction = faction;
}
}