From a54d54d9939c980795383853227ba04bbd8e63af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega?= Date: Sun, 18 Sep 2016 16:17:34 +0200 Subject: [PATCH] Use floats for less precision (we don't need doubles). --- build.xml | 14 +++-- .../entities/BaseVectorShape.java | 56 +++++++++---------- src/spaceshipsim/entities/Ship.java | 26 ++++----- 3 files changed, 50 insertions(+), 46 deletions(-) diff --git a/build.xml b/build.xml index d935e49..a853319 100644 --- a/build.xml +++ b/build.xml @@ -4,16 +4,20 @@ - + - + - + + + + + - + @@ -24,7 +28,7 @@ - + diff --git a/src/spaceshipsim/entities/BaseVectorShape.java b/src/spaceshipsim/entities/BaseVectorShape.java index 10b2934..012ce39 100644 --- a/src/spaceshipsim/entities/BaseVectorShape.java +++ b/src/spaceshipsim/entities/BaseVectorShape.java @@ -12,45 +12,45 @@ import java.awt.Shape; public class BaseVectorShape { private Shape shape; private boolean alive; - private double x, y; - private double velX, velY; - private double moveAngle, faceAngle; + private float x, y; + private float velX, velY; + private float moveAngle, faceAngle; // Constructor: public BaseVectorShape() { setShape(null); setAlive(false); - setX(0.0); - setY(0.0); - setVelX(0.0); - setVelY(0.0); - setMoveAngle(0.0); - setFaceAngle(0.0); + setX(0.0f); + setY(0.0f); + setVelX(0.0f); + setVelY(0.0f); + setMoveAngle(0.0f); + setFaceAngle(0.0f); } // Accessor methods: public Shape getShape() { return shape; } public boolean isAlive() { return alive; } - public double getX() { return x; } - public double getY() { return y; } - public double getVelX() { return velX; } - public double getVelY() { return velY; } - public double getMoveAngle() { return moveAngle; } - public double getFaceAngle() { return faceAngle; } + public float getX() { return x; } + public float getY() { return y; } + public float getVelX() { return velX; } + public float getVelY() { return velY; } + public float getMoveAngle() { return moveAngle; } + public float getFaceAngle() { return faceAngle; } // Setter methods: public void setShape(Shape shape) { this.shape = shape; } public void setAlive(boolean alive) { this.alive = alive; } - public void setX(double x) { this.x = x; } - public void incX(double iX) { this.x += iX; } - public void setY(double y) { this.y = y; } - public void incY(double iY) { this.y += iY; } - public void setVelX(double velX) { this.velX = velX; } - public void incVelX(double iVX) { this.velX += iVX; } - public void setVelY(double velY) { this.velY = velY; } - public void incVelY(double iVY) { this.velY += iVY; } - public void setMoveAngle(double nMA) { this.moveAngle = nMA; } - public void incMoveAngle(double iMA) { this.moveAngle += iMA; } - public void setFaceAngle(double nFA) { this.faceAngle = nFA; } - public void incFaceAngle(double iFA) { this.faceAngle += iFA; } -} \ No newline at end of file + public void setX(float x) { this.x = x; } + public void incX(float iX) { this.x += iX; } + public void setY(float y) { this.y = y; } + public void incY(float iY) { this.y += iY; } + public void setVelX(float velX) { this.velX = velX; } + public void incVelX(float iVX) { this.velX += iVX; } + public void setVelY(float velY) { this.velY = velY; } + public void incVelY(float iVY) { this.velY += iVY; } + public void setMoveAngle(float nMA) { this.moveAngle = nMA; } + public void incMoveAngle(float iMA) { this.moveAngle += iMA; } + public void setFaceAngle(float nFA) { this.faceAngle = nFA; } + public void incFaceAngle(float iFA) { this.faceAngle += iFA; } +} diff --git a/src/spaceshipsim/entities/Ship.java b/src/spaceshipsim/entities/Ship.java index eb96e7a..4a4db81 100644 --- a/src/spaceshipsim/entities/Ship.java +++ b/src/spaceshipsim/entities/Ship.java @@ -20,11 +20,11 @@ public class Ship extends BaseVectorShape { private boolean turnRight = false; // Acceleration variables - private double accelerateX; - private double accelerateY; + private float accelerateX; + private float accelerateY; // Constructor: - public Ship(double nx, double ny) { + public Ship(float nx, float ny) { setX(nx); setY(ny); setShape(new Polygon(shipx, shipy, shipx.length)); @@ -35,8 +35,8 @@ public class Ship extends BaseVectorShape { public void update() { if(accelerate) { setMoveAngle(getFaceAngle() - 90); - accelerateX = calcAngleMoveX(getMoveAngle()) * 0.1; - accelerateY = calcAngleMoveY(getMoveAngle()) * 0.1; + accelerateX = calcAngleMoveX(getMoveAngle()) * 0.1f; + accelerateY = calcAngleMoveY(getMoveAngle()) * 0.1f; incVelX(accelerateX); incVelY(accelerateY); } else { @@ -63,19 +63,19 @@ public class Ship extends BaseVectorShape { return r; } - public double getAccelX() { return accelerateX; } - public double getAccelY() { return accelerateY; } + public float getAccelX() { return accelerateX; } + public float getAccelY() { return accelerateY; } // Setter methods: public void setAccelerate(boolean accel) { this.accelerate = accel; } - public void setAccelX(double aX) { this.accelerateX = aX; } - public void setAccelY(double aY) { this.accelerateY = aY; } - public void incAccelX(double iaX) { this.accelerateX += iaX; } - public void incAccelY(double iaY) { this.accelerateY += iaY; } + public void setAccelX(float aX) { this.accelerateX = aX; } + public void setAccelY(float aY) { this.accelerateY = aY; } + public void incAccelX(float iaX) { this.accelerateX += iaX; } + public void incAccelY(float iaY) { this.accelerateY += iaY; } public void setTurnLeft(boolean tL) { this.turnLeft = tL; } public void setTurnRight(boolean tR) { this.turnRight = tR; } // Methods used for calculations: - public double calcAngleMoveX(double angle) { return (double) (Math.cos(angle * Math.PI / 180)); } - public double calcAngleMoveY(double angle) { return (double) (Math.sin(angle * Math.PI / 180)); } + public float calcAngleMoveX(float angle) { return (float)Math.cos(angle * Math.PI / 180); } + public float calcAngleMoveY(float angle) { return (float)Math.sin(angle * Math.PI / 180); } }