Hello Processing forum users,
I am currently having an issue with my Tank simulation program: there are a few tanks roaming around on my screen and they shoot bullets(ellipses) at each other. The fire()
method adds a bullet to an ArrayList of bullets located in main, which draws and moves the bullets every 10 or so seconds.
public class AttackerTank extends TankType {
Main main;
// class variables..
}
public void draw() {
xDest = Math.cos(turretAngle)*40+this.x; //draws turret
yDest = Math.sin(turretAngle)*40+this.y;
if(this.turretAngle > 2*PI) {
this.turretAngle = 0;
} else if (this.turretAngle < 0) {
this.turretAngle = 2*PI;
}
displayTank();
}
public void displayTank() {
if(Math.random() > 0.4) {
moveHorizontal();
}
findTarget();
main.rectMode(PConstants.CENTER);
main.noFill();
main.pushMatrix();
rotateGradually();
main.print(angle + "Angle? ");
rotateBody(angle);
main.strokeWeight(5);
main.line(x, y, (float)xDest, (float)yDest);
main.fill(this.team.color[0], this.team.color[1], this.team.color[2]);
main.rect((float)x, (float)y, 20, 50); //draw rotated rect(body)
main.fill(0);
main.strokeWeight(10);
main.popMatrix();
}
public void rotateBody(float baseAngle) {
main.rectMode(PConstants.CENTER);
main.translate(this.x/2, this.y/2); //rotate around center(?)
// main.translate((float)x, (float)y);
main.rotate(main.radians(baseAngle));
main.translate(-this.x/2, -this.y/2);//return to proper location
}
public void moveHorizontal() {
this.x += this.moveSpeed;
if(this.x > main.WIDTH) {
this.x = 0;
}
if(this.y > main.HEIGHT) {
this.y = 0;
}
}
public void rotateGradually() {
if(this.bodyTargetAngle > this.angle) {
this.angle++;
}
}
public void findTarget() {
if(this.target != null) {
if (this.target.hitPoints <= 0) {
this.target = null;
}
}
for(AttackerTank at: main.tanks2) {
if(inRange(at.x, at.y)) {
this.target = at;
}
}
}
public boolean inRange(float otherX, float otherY) {
return Math.sqrt((otherY - this.y) * (otherY - this.y) + (otherX - this.x) * (otherX - this.x)) < this.tankType.attackRange;
}
public void fire() {
this.targetTurretAngle = Math.atan2(this.target.y - this.y, this.target.x - this.x);
Projectile awesome = new Projectile(main, x, y, 20, (float)(8 * Math.cos(targetTurretAngle)), (float)(8 * Math.sin(targetTurretAngle)), this.team.color[0], this.team.color[1], this.team.color[2], this.target.name);
main.projectiles.add(awesome);
System.out.println(fireX + " my FireY: " + fireY);
}
}
Yet, when my bullets are spawned, their spawn points gradually shift away from the tank. Could anyone provide some pointers?
Start
I cannot provide more than one picture, though the bullets spawn exactly where the end of the turret is.
10 - 20 seconds later