From 9de35c4ad1e316e1c307e2135688a7b4ea7ecc11 Mon Sep 17 00:00:00 2001 From: Chimonas <33982690+Chimonas@users.noreply.github.com> Date: Wed, 23 May 2018 18:54:02 +0200 Subject: [PATCH 1/9] RK4 improvements Starting implementing queue to store moving coordinates --- core/src/com/group/golf/Ball.java | 18 ++- .../com/group/golf/Physics/Coordinates.java | 14 +++ core/src/com/group/golf/Physics/Physics.java | 106 +++++++++--------- core/src/com/group/golf/Physics/Queue.java | 42 +++++++ 4 files changed, 125 insertions(+), 55 deletions(-) create mode 100644 core/src/com/group/golf/Physics/Coordinates.java create mode 100644 core/src/com/group/golf/Physics/Queue.java diff --git a/core/src/com/group/golf/Ball.java b/core/src/com/group/golf/Ball.java index ff49c33..06d8e4f 100644 --- a/core/src/com/group/golf/Ball.java +++ b/core/src/com/group/golf/Ball.java @@ -1,17 +1,21 @@ package com.group.golf; import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.math.Circle; +import com.group.golf.Physics.Queue; import com.group.golf.math.MathLib; import com.group.golf.screens.CourseScreen; +import java.util.Deque; + /** * A class to save the data of the ball */ public class Ball { public static final float RADIUS = 10; + private Queue movementCoord; + private double mass; private double x; @@ -19,6 +23,7 @@ public class Ball { private double velocityX; private double velocityY; + private Texture texture; private CourseScreen courseScreen; private Circle collisionCircle; @@ -36,6 +41,7 @@ public Ball(double mass, double x, double y) { this.velocityX = 0; this.velocityY = 0; this.collisionCircle = new Circle((float) this.x, (float) this.y, RADIUS); + this.movementCoord = new Queue(); } /** @@ -49,6 +55,12 @@ public Ball(double mass) { this.velocityX = 0; this.velocityY = 0; this.collisionCircle = new Circle((float) this.x, (float) this.y, RADIUS); + this.movementCoord = new Queue(); + + } + + public void addCoord(double[] coord) { + movementCoord.enqueue(coord); } /** @@ -205,4 +217,8 @@ public void setY(double y) { this.y = y; this.updateCollisionCircle(); } + + public Queue getMovementCoord() { + return movementCoord; + } } diff --git a/core/src/com/group/golf/Physics/Coordinates.java b/core/src/com/group/golf/Physics/Coordinates.java new file mode 100644 index 0000000..4c22b82 --- /dev/null +++ b/core/src/com/group/golf/Physics/Coordinates.java @@ -0,0 +1,14 @@ +package com.group.golf.Physics; + +/** + * Created by alex_ on 23-May-18. + */ +public class Coordinates { + double[] coord; + Coordinates next; + + public Coordinates(double[] coord) { + this.coord = coord; + next = null; + } +} diff --git a/core/src/com/group/golf/Physics/Physics.java b/core/src/com/group/golf/Physics/Physics.java index 849f721..950761f 100644 --- a/core/src/com/group/golf/Physics/Physics.java +++ b/core/src/com/group/golf/Physics/Physics.java @@ -1,10 +1,8 @@ package com.group.golf.Physics; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.math.Vector2; import com.group.golf.Ball; import com.group.golf.Course; -import com.group.golf.math.Computable; import java.util.ArrayList; @@ -17,6 +15,7 @@ public class Physics { private Course course; private Ball ball; private double[] hitCoord; + private final int fps = 30; /** * Construct a Physics engine @@ -32,34 +31,54 @@ public Physics(Course course, Ball ball) { //https://www.haroldserrano.com/blog/visualizing-the-runge-kutta-method - public double[] RK4(double h){ + public void RK4(double h){ - double[] grav = gravForce(ball, new double[]{ball.getX(),ball.getY()}); - double[] friction = frictionForce(ball,ball.getVelocityX(),ball.getVelocityY()); + double[] grav = gravForce(new double[]{ball.getX(),ball.getY()}); + double[] friction = frictionForce(ball.getVelocityX(),ball.getVelocityY()); + double[][] accel = new double[4][2]; + accel[0] = new double[]{grav[0]+friction[0],grav[1]+friction[1]}; - double v1x = ball.getVelocityX(); double v1y = ball.getVelocityY(); + double[][] velo = new double[4][2]; + velo[0] = new double[]{ball.getVelocityX(),ball.getVelocityY()}; - double a1x = grav[0] + friction[0]; double a1y = grav[1] + friction[1]; - double v2x = v1x + a1x * h/2; double v2y = v1y + a1y * h/2; + for (int i = 1; i < 4; i++) { + double[] v = updateRKStep(velo[0],accel[i-1],h,i); + velo[i] = v; - friction = frictionForce(ball,v2x,v2y); - double a2x = grav[0] + friction[0]; double a2y = grav[1] + friction[1]; - double v3x = v1x + a2x * h/2; double v3y = v1y + a2y * h/2; + double[] p = updateRKStep(new double[]{ball.getX(),ball.getY()},v,h,i); + grav = gravForce(p); - friction = frictionForce(ball,v3x,v3y); - double a3x = grav[0] + friction[0]; double a3y = grav[1] + friction[1]; - double v4x = v1x + h * a3x; double v4y = v1y + h * a3y; + friction = frictionForce(v[0],v[1]); + accel[i] = new double[]{grav[0] + friction[0], grav[1] + friction[1]}; + } - friction = frictionForce(ball,v4x,v4y); - double a4x = grav[0] + friction[0]; double a4y = grav[1] + friction[1]; + double accelX = (accel[0][0] + 2 * accel[1][0] + 2 * accel[2][0] + accel[3][0])/6; + double accelY = (accel[0][1] + 2 * accel[1][1] + 2 * accel[2][1] + accel[3][1])/6; - double[] resV = {v1x + h/6 * (a1x + 2*a2x + 2*a3x + a4x), v1y + h/6 * (a1y + 2*a2y + 2*a3y + a4y)}; - return resV; + ball.setVelocityX(velo[0][0] + h * accelX); + ball.setVelocityY(velo[0][1] + h * accelY); - } + System.out.println("Acceleration x: " + accelX + " Acceleration y: " + accelY); +// if (Math.abs(this.ball.getVelocityX()) < 0.0776 && Math.abs(this.ball.getVelocityY()) < 0.0776) { +// this.ball.reset(); +// } + double[] coord = new double[]{ball.getX() + h/6 * (velo[0][0] + 2 * velo[1][0] + 2 * velo[2][0] + velo[3][0]),ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])}; + ball.addCoord(coord); + + ball.setX(ball.getX() + h/6 * (velo[0][0] + 2 * velo[1][0] + 2 * velo[2][0] + velo[3][0])); + ball.setY(ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])); + } + + private double[] updateRKStep(double[] startPos, double[] prev, double h, double k) { + if (k < 3) { + return new double[]{startPos[0] + prev[0] * h/2,startPos[1] + prev[1] * h/2}; + } else { + return new double[]{startPos[0] + prev[0] * h, startPos[1] + prev[1] * h}; + } + } /** * Hit the ball @@ -71,6 +90,12 @@ public void hit(double forceX, double forceY) { hitCoord[0] = ball.getX(); hitCoord[1] = ball.getY(); + +// double frameRate = 1/fps; +// while (this.ball.isMoving()) { +// RK4(frameRate); +// } + ball.setVelocityX(Gdx.graphics.getDeltaTime() * forceX / ball.getMass()); ball.setVelocityY(Gdx.graphics.getDeltaTime() * forceY / ball.getMass()); } @@ -81,28 +106,22 @@ public void hit(double forceX, double forceY) { */ public void movement(float delta) { - ball.setX(ball.getX() + delta * ball.getVelocityX()); - ball.setY(ball.getY() + delta * ball.getVelocityY()); + RK4(delta); - double[] vel = RK4(delta); - ball.setVelocityX(vel[0]); - ball.setVelocityY(vel[1]); - - System.out.println("VelocityX: " + ball.getVelocityX() + " VelocityY: " + ball.getVelocityY()); - if (Math.abs(this.ball.getVelocityX()) < 0.0776 && Math.abs(this.ball.getVelocityY()) < 0.0776) { - this.ball.reset(); - } +// System.out.println("VelocityX: " + ball.getVelocityX() + " VelocityY: " + ball.getVelocityY()); +// if (Math.abs(this.ball.getVelocityX()) < 0.0776 && Math.abs(this.ball.getVelocityY()) < 0.0776) { +// this.ball.reset(); +// } } /** * Compute the friction force that oposes to the movement of the ball - * @param ball the ball rolling * @param velocityX the x-component of the velocity of the ball * @param velocityY the y-component of the velocity of the ball * @return a Vector2 instace containig the friction force */ - public double[] frictionForce(Ball ball,double velocityX, double velocityY) { + public double[] frictionForce(double velocityX, double velocityY) { double multiplier = - this.course.getMu() * this.course.getG() / normalLength(velocityX,velocityY); return new double[]{(multiplier * velocityX) ,(multiplier * velocityY)}; @@ -120,11 +139,10 @@ public double normalLength(double velocityX, double velocityY) { /** * Compute the gravitational force that the ball suffers - * @param ball the ball rolling * @param coord the Vector2 containing the actual position of the ball * @return a Vector2 instance containing the gravity force */ - public double[] gravForce(Ball ball, double[] coord) { + public double[] gravForce(double[] coord) { double multiplier = - this.course.getG(); double[] slopeMultiplier = calculateSlope(coord); return new double[] {multiplier * slopeMultiplier[0],multiplier * slopeMultiplier[1]}; @@ -142,27 +160,7 @@ public double[] calculateSlope(double[] coord) { slope[0] = (this.course.getHeight(coord[0]+step,coord[1]) - this.course.getHeight(coord[0]-step,coord[1]))/(2*step); slope[1] = ((this.course.getHeight(coord[0],coord[1]+step) - this.course.getHeight(coord[0],coord[1]-step))/(2*step)); -// -// if (this.course.getHeight(coord.x-H,coord.y) != this.course.getHeight(coord.x,coord.y) && -// (this.course.getHeight(coord.x+H,coord.y) != this.course.getHeight(coord.x,coord.y))) { -// if (this.course.getHeight(coord.x-H,coord.y) < this.course.getHeight(coord.x+H,coord.y)) { -// slope.x = (float) ((this.course.getHeight(coord.x-H,coord.y)-this.course.getHeight(coord.x,coord.y))/((coord.x-H)-coord.x)); -// } else { -// slope.x = (float) ((this.course.getHeight(coord.x+H,coord.y)-this.course.getHeight(coord.x,coord.y))/((coord.x+H)-coord.x)); -// } -// } else { -// slope.x = 0; -// } -// if (this.course.getHeight(coord.x,coord.y-H) != this.course.getHeight(coord.x,coord.y) && -// (this.course.getHeight(coord.x,coord.y+H) != (this.course.getHeight(coord.x,coord.y)))) { -// if (this.course.getHeight(coord.x,coord.y-H) < this.course.getHeight(coord.x,coord.y+H)) { -// slope.y = (float) ((this.course.getHeight(coord.x,coord.y-H)-this.course.getHeight(coord.x,coord.y))/((coord.y-H) - coord.y)); -// } else { -// slope.y = (float) ((this.course.getHeight(coord.x,coord.y+H)-this.course.getHeight(coord.x,coord.y))/((coord.y+H) - coord.y)); -// } -// } else { -// slope.y = 0; -// } + return slope; } diff --git a/core/src/com/group/golf/Physics/Queue.java b/core/src/com/group/golf/Physics/Queue.java new file mode 100644 index 0000000..3252de9 --- /dev/null +++ b/core/src/com/group/golf/Physics/Queue.java @@ -0,0 +1,42 @@ +package com.group.golf.Physics; + +/** + * Created by alex_ on 23-May-18. + */ +public class Queue { + private Coordinates head; + private Coordinates tail; + private int size; + + public Queue() { + head = null; + tail = null; + size = 0; + } + + public void enqueue(double[] coord) { + if (head == null) { + head = new Coordinates(coord); + tail = head; + } else { + Coordinates newTail = new Coordinates(coord); + Coordinates temp = tail; + tail.next = newTail; + tail = temp; + } +// System.out.println("Enqueue works"); + size++; + } + + public double[] dequeue() { + double[] temp = head.coord; + head = head.next; + size--; + return temp; + } + + public int getSize() { + return size; + } + +} From bec68ee54df7889a33b4083d6e461f1034b3d42a Mon Sep 17 00:00:00 2001 From: Chimonas <33982690+Chimonas@users.noreply.github.com> Date: Wed, 23 May 2018 23:26:57 +0200 Subject: [PATCH 2/9] Improvements Looks to be working Need to implement collision in physics --- core/src/com/group/golf/Ball.java | 36 +++++++++++++++-- .../com/group/golf/Physics/Coordinates.java | 1 + core/src/com/group/golf/Physics/Physics.java | 30 +++++++------- core/src/com/group/golf/Physics/Queue.java | 40 ++++++++++++++++++- .../com/group/golf/screens/CourseScreen.java | 9 ++++- 5 files changed, 94 insertions(+), 22 deletions(-) diff --git a/core/src/com/group/golf/Ball.java b/core/src/com/group/golf/Ball.java index 06d8e4f..07e74b4 100644 --- a/core/src/com/group/golf/Ball.java +++ b/core/src/com/group/golf/Ball.java @@ -22,6 +22,8 @@ public class Ball { private double y; private double velocityX; private double velocityY; + private double accelerationX; + private double accelerationY; private Texture texture; @@ -59,6 +61,13 @@ public Ball(double mass) { } + public double[] dequeue() { + double[] coord = movementCoord.dequeue(); + setX(coord[0]); + setY(coord[1]); + return coord; + } + public void addCoord(double[] coord) { movementCoord.enqueue(coord); } @@ -139,8 +148,7 @@ public double calcVelocity() { * Get access to the x-component of the velocity * @return the x-component of the velocity */ - public double getVelocityX() { - return velocityX; + public double getVelocityX() {return velocityX; } /** @@ -188,7 +196,8 @@ public void setMass(double mass) { * @return the x-coordinate for position */ public double getX() { - return x; +// System.out.print("X: " + movementCoord.last()[0]); + return movementCoord.last()[0]; } @@ -198,6 +207,7 @@ public double getX() { */ public void setX(double x) { this.x = x; + movementCoord.setX(x); this.updateCollisionCircle(); } @@ -206,7 +216,8 @@ public void setX(double x) { * @return the y-coordinate for position */ public double getY() { - return y; +// System.out.println( " Y: " + movementCoord.last()[1]); + return movementCoord.last()[1]; } /** @@ -215,10 +226,27 @@ public double getY() { */ public void setY(double y) { this.y = y; + movementCoord.setY(y); this.updateCollisionCircle(); } public Queue getMovementCoord() { return movementCoord; } + + public void setAccelerationX(double accelerationX) { + this.accelerationX = accelerationX; + } + + public void setAccelerationY(double accelerationY) { + this.accelerationY = accelerationY; + } + + public double getAccelerationX() { + return accelerationX; + } + + public double getAccelerationY() { + return accelerationY; + } } diff --git a/core/src/com/group/golf/Physics/Coordinates.java b/core/src/com/group/golf/Physics/Coordinates.java index 4c22b82..9789e51 100644 --- a/core/src/com/group/golf/Physics/Coordinates.java +++ b/core/src/com/group/golf/Physics/Coordinates.java @@ -6,6 +6,7 @@ public class Coordinates { double[] coord; Coordinates next; + Coordinates prev; public Coordinates(double[] coord) { this.coord = coord; diff --git a/core/src/com/group/golf/Physics/Physics.java b/core/src/com/group/golf/Physics/Physics.java index 950761f..e7dde1c 100644 --- a/core/src/com/group/golf/Physics/Physics.java +++ b/core/src/com/group/golf/Physics/Physics.java @@ -59,17 +59,18 @@ public void RK4(double h){ ball.setVelocityX(velo[0][0] + h * accelX); ball.setVelocityY(velo[0][1] + h * accelY); - System.out.println("Acceleration x: " + accelX + " Acceleration y: " + accelY); - -// if (Math.abs(this.ball.getVelocityX()) < 0.0776 && Math.abs(this.ball.getVelocityY()) < 0.0776) { -// this.ball.reset(); -// } +// System.out.println("VelocityX: " + ball.getVelocityX() + " VelocityY: " + ball.getVelocityY()); + if (Math.abs(this.ball.getVelocityX()) < 0.0776 && Math.abs(this.ball.getVelocityY()) < 0.0776) { + this.ball.reset(); + } - double[] coord = new double[]{ball.getX() + h/6 * (velo[0][0] + 2 * velo[1][0] + 2 * velo[2][0] + velo[3][0]),ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])}; + double[] coord = new double[]{ball.getX() + h/6 * (velo[0][0] + 2 * velo[1][0] + 2 * velo[2][0] + velo[3][0]), + ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])}; ball.addCoord(coord); - ball.setX(ball.getX() + h/6 * (velo[0][0] + 2 * velo[1][0] + 2 * velo[2][0] + velo[3][0])); - ball.setY(ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])); + //this is going to be removed +// ball.setX(ball.getX() + h/6 * (velo[0][0] + 2 * velo[1][0] + 2 * velo[2][0] + velo[3][0])); +// ball.setY(ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])); } private double[] updateRKStep(double[] startPos, double[] prev, double h, double k) { @@ -90,14 +91,15 @@ public void hit(double forceX, double forceY) { hitCoord[0] = ball.getX(); hitCoord[1] = ball.getY(); + double frameRate = 0.04; -// double frameRate = 1/fps; -// while (this.ball.isMoving()) { -// RK4(frameRate); -// } + ball.setVelocityX(frameRate * forceX / ball.getMass()); + ball.setVelocityY(frameRate * forceY / ball.getMass()); + + while (this.ball.isMoving()) { + RK4(frameRate); + } - ball.setVelocityX(Gdx.graphics.getDeltaTime() * forceX / ball.getMass()); - ball.setVelocityY(Gdx.graphics.getDeltaTime() * forceY / ball.getMass()); } /** diff --git a/core/src/com/group/golf/Physics/Queue.java b/core/src/com/group/golf/Physics/Queue.java index 3252de9..9488a92 100644 --- a/core/src/com/group/golf/Physics/Queue.java +++ b/core/src/com/group/golf/Physics/Queue.java @@ -7,6 +7,8 @@ public class Queue { private Coordinates head; private Coordinates tail; private int size; + private double x; + private double y; public Queue() { head = null; @@ -18,11 +20,14 @@ public void enqueue(double[] coord) { if (head == null) { head = new Coordinates(coord); tail = head; + System.out.println("First element"); } else { Coordinates newTail = new Coordinates(coord); Coordinates temp = tail; - tail.next = newTail; - tail = temp; + temp.next = newTail; + newTail.prev = temp; + tail = newTail; + System.out.println(coord[0]); } // System.out.println("Enqueue works"); size++; @@ -31,12 +36,43 @@ public void enqueue(double[] coord) { public double[] dequeue() { double[] temp = head.coord; head = head.next; +// head.prev = null; size--; return temp; } + public double[] last() { + if (tail == null) { +// System.out.println("in here"); + return new double[]{x,y}; + } else + return tail.coord; + } + + public double[] peak() { + if (head == null) { + return new double[]{x,y}; + } else + return head.coord; + } + public int getSize() { return size; } + public void setX(double x) { + this.x = x; + } + + public void setY(double y) { + this.y = y; + } + + public double getX() { + return x; + } + + public double getY() { + return y; + } } diff --git a/core/src/com/group/golf/screens/CourseScreen.java b/core/src/com/group/golf/screens/CourseScreen.java index cc05840..9681e76 100644 --- a/core/src/com/group/golf/screens/CourseScreen.java +++ b/core/src/com/group/golf/screens/CourseScreen.java @@ -339,8 +339,10 @@ public void render(float delta) { // Update pixel position of ball this.computeBallPixels(); + + // Check if the ball is stopped - if (!this.ball.isMoving()) { + if (this.ball.getMovementCoord().getSize() == 0) { // Check if the goal is achieved if (this.collision.isGoalAchieved()) { this.winSound.play(); @@ -364,7 +366,10 @@ else if (this.moves != null && this.counter < this.moves.size()) { // Mode 2 is this.userMoves(); } } else { - this.engine.movement(delta); + + + + this.ball.dequeue(); } this.ball.limit(this.course.getVmax()); From 42c5bb88de9300c4489b4d67de50feddf1e968c8 Mon Sep 17 00:00:00 2001 From: Chimonas <33982690+Chimonas@users.noreply.github.com> Date: Wed, 23 May 2018 23:34:48 +0200 Subject: [PATCH 3/9] no message --- core/src/com/group/golf/Ball.java | 22 +++++++------------ .../com/group/golf/screens/CourseScreen.java | 4 +--- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/core/src/com/group/golf/Ball.java b/core/src/com/group/golf/Ball.java index 07e74b4..f42fd93 100644 --- a/core/src/com/group/golf/Ball.java +++ b/core/src/com/group/golf/Ball.java @@ -6,15 +6,13 @@ import com.group.golf.math.MathLib; import com.group.golf.screens.CourseScreen; -import java.util.Deque; /** * A class to save the data of the ball */ -public class Ball { +public class Ball extends Queue{ public static final float RADIUS = 10; - private Queue movementCoord; private double mass; @@ -43,7 +41,6 @@ public Ball(double mass, double x, double y) { this.velocityX = 0; this.velocityY = 0; this.collisionCircle = new Circle((float) this.x, (float) this.y, RADIUS); - this.movementCoord = new Queue(); } /** @@ -57,19 +54,19 @@ public Ball(double mass) { this.velocityX = 0; this.velocityY = 0; this.collisionCircle = new Circle((float) this.x, (float) this.y, RADIUS); - this.movementCoord = new Queue(); +// this.movementCoord = new Queue(); } public double[] dequeue() { - double[] coord = movementCoord.dequeue(); + double[] coord = super.dequeue(); setX(coord[0]); setY(coord[1]); return coord; } public void addCoord(double[] coord) { - movementCoord.enqueue(coord); + super.enqueue(coord); } /** @@ -197,7 +194,7 @@ public void setMass(double mass) { */ public double getX() { // System.out.print("X: " + movementCoord.last()[0]); - return movementCoord.last()[0]; + return super.last()[0]; } @@ -207,7 +204,7 @@ public double getX() { */ public void setX(double x) { this.x = x; - movementCoord.setX(x); + super.setX(x); this.updateCollisionCircle(); } @@ -217,7 +214,7 @@ public void setX(double x) { */ public double getY() { // System.out.println( " Y: " + movementCoord.last()[1]); - return movementCoord.last()[1]; + return super.last()[1]; } /** @@ -226,13 +223,10 @@ public double getY() { */ public void setY(double y) { this.y = y; - movementCoord.setY(y); + super.setY(y); this.updateCollisionCircle(); } - public Queue getMovementCoord() { - return movementCoord; - } public void setAccelerationX(double accelerationX) { this.accelerationX = accelerationX; diff --git a/core/src/com/group/golf/screens/CourseScreen.java b/core/src/com/group/golf/screens/CourseScreen.java index 9681e76..d74f73c 100644 --- a/core/src/com/group/golf/screens/CourseScreen.java +++ b/core/src/com/group/golf/screens/CourseScreen.java @@ -342,7 +342,7 @@ public void render(float delta) { // Check if the ball is stopped - if (this.ball.getMovementCoord().getSize() == 0) { + if (this.ball.getSize() == 0) { // Check if the goal is achieved if (this.collision.isGoalAchieved()) { this.winSound.play(); @@ -367,8 +367,6 @@ else if (this.moves != null && this.counter < this.moves.size()) { // Mode 2 is } } else { - - this.ball.dequeue(); } this.ball.limit(this.course.getVmax()); From 9ef738a246b86c74214cc0ae82d07d554288a071 Mon Sep 17 00:00:00 2001 From: Chimonas <33982690+Chimonas@users.noreply.github.com> Date: Thu, 24 May 2018 00:15:13 +0200 Subject: [PATCH 4/9] no message --- core/src/com/group/golf/Ball.java | 10 +-- .../com/group/golf/Physics/Coordinates.java | 15 ----- core/src/com/group/golf/Physics/Physics.java | 35 ++++++++--- core/src/com/group/golf/Physics/Queue.java | 63 +++++++++++-------- core/src/com/group/golf/Physics/node.java | 15 +++++ .../com/group/golf/screens/CourseScreen.java | 12 ++-- 6 files changed, 86 insertions(+), 64 deletions(-) delete mode 100644 core/src/com/group/golf/Physics/Coordinates.java create mode 100644 core/src/com/group/golf/Physics/node.java diff --git a/core/src/com/group/golf/Ball.java b/core/src/com/group/golf/Ball.java index f42fd93..37069cd 100644 --- a/core/src/com/group/golf/Ball.java +++ b/core/src/com/group/golf/Ball.java @@ -10,7 +10,7 @@ /** * A class to save the data of the ball */ -public class Ball extends Queue{ +public class Ball extends Queue{ public static final float RADIUS = 10; @@ -193,7 +193,8 @@ public void setMass(double mass) { * @return the x-coordinate for position */ public double getX() { -// System.out.print("X: " + movementCoord.last()[0]); + if (super.tail == null) + return x; return super.last()[0]; } @@ -204,7 +205,6 @@ public double getX() { */ public void setX(double x) { this.x = x; - super.setX(x); this.updateCollisionCircle(); } @@ -213,7 +213,8 @@ public void setX(double x) { * @return the y-coordinate for position */ public double getY() { -// System.out.println( " Y: " + movementCoord.last()[1]); + if (super.tail == null) + return y; return super.last()[1]; } @@ -223,7 +224,6 @@ public double getY() { */ public void setY(double y) { this.y = y; - super.setY(y); this.updateCollisionCircle(); } diff --git a/core/src/com/group/golf/Physics/Coordinates.java b/core/src/com/group/golf/Physics/Coordinates.java deleted file mode 100644 index 9789e51..0000000 --- a/core/src/com/group/golf/Physics/Coordinates.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.group.golf.Physics; - -/** - * Created by alex_ on 23-May-18. - */ -public class Coordinates { - double[] coord; - Coordinates next; - Coordinates prev; - - public Coordinates(double[] coord) { - this.coord = coord; - next = null; - } -} diff --git a/core/src/com/group/golf/Physics/Physics.java b/core/src/com/group/golf/Physics/Physics.java index e7dde1c..782c639 100644 --- a/core/src/com/group/golf/Physics/Physics.java +++ b/core/src/com/group/golf/Physics/Physics.java @@ -33,28 +33,31 @@ public Physics(Course course, Ball ball) { //https://www.haroldserrano.com/blog/visualizing-the-runge-kutta-method public void RK4(double h){ - double[] grav = gravForce(new double[]{ball.getX(),ball.getY()}); - double[] friction = frictionForce(ball.getVelocityX(),ball.getVelocityY()); + double[][] accel = new double[4][2]; - accel[0] = new double[]{grav[0]+friction[0],grav[1]+friction[1]}; + accel[0] = new double[]{ball.getAccelerationX(),ball.getAccelerationY()}; double[][] velo = new double[4][2]; velo[0] = new double[]{ball.getVelocityX(),ball.getVelocityY()}; for (int i = 1; i < 4; i++) { + System.out.println(i); double[] v = updateRKStep(velo[0],accel[i-1],h,i); velo[i] = v; double[] p = updateRKStep(new double[]{ball.getX(),ball.getY()},v,h,i); - grav = gravForce(p); - friction = frictionForce(v[0],v[1]); + double[] grav = gravForce(p); + double[] friction = frictionForce(v[0],v[1]); + accel[i] = new double[]{grav[0] + friction[0], grav[1] + friction[1]}; } double accelX = (accel[0][0] + 2 * accel[1][0] + 2 * accel[2][0] + accel[3][0])/6; double accelY = (accel[0][1] + 2 * accel[1][1] + 2 * accel[2][1] + accel[3][1])/6; + ball.setAccelerationX(accelX); + ball.setAccelerationY(accelY); ball.setVelocityX(velo[0][0] + h * accelX); ball.setVelocityY(velo[0][1] + h * accelY); @@ -68,6 +71,12 @@ public void RK4(double h){ ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])}; ball.addCoord(coord); +// double[] grav = gravForce(new double[]{ball.getX(),ball.getY()}); +// double[] friction = frictionForce(ball.getVelocityX(),ball.getVelocityY()); + +// ball.setAccelerationX(accelX); +// ball.setAccelerationY(accelY); + //this is going to be removed // ball.setX(ball.getX() + h/6 * (velo[0][0] + 2 * velo[1][0] + 2 * velo[2][0] + velo[3][0])); // ball.setY(ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])); @@ -83,19 +92,25 @@ private double[] updateRKStep(double[] startPos, double[] prev, double h, double /** * Hit the ball - * @param forceX the velocityX done to the ball - * @param forceY the velocityY done to the ball + * @param xLength the length that the mouse was dragged horizontally + * @param yLength the length that the mouse was dragged vertically */ - public void hit(double forceX, double forceY) { + public void hit(double xLength, double yLength) { hitCoord[0] = ball.getX(); hitCoord[1] = ball.getY(); double frameRate = 0.04; - ball.setVelocityX(frameRate * forceX / ball.getMass()); - ball.setVelocityY(frameRate * forceY / ball.getMass()); + xLength *= 90; + yLength *= 90; + + ball.setAccelerationX(xLength/ball.getMass()); + ball.setAccelerationY(yLength/ball.getMass()); +// ball.setVelocityX(frameRate * forceX / ball.getMass()); +// ball.setVelocityY(frameRate * forceY / ball.getMass()); + RK4(frameRate); while (this.ball.isMoving()) { RK4(frameRate); } diff --git a/core/src/com/group/golf/Physics/Queue.java b/core/src/com/group/golf/Physics/Queue.java index 9488a92..71d5c85 100644 --- a/core/src/com/group/golf/Physics/Queue.java +++ b/core/src/com/group/golf/Physics/Queue.java @@ -3,12 +3,13 @@ /** * Created by alex_ on 23-May-18. */ -public class Queue { - private Coordinates head; - private Coordinates tail; +public class Queue { + protected node head; + protected node tail; private int size; private double x; private double y; + private E defaultE; public Queue() { head = null; @@ -16,63 +17,71 @@ public Queue() { size = 0; } - public void enqueue(double[] coord) { + public void enqueue(E element) { if (head == null) { - head = new Coordinates(coord); + head = new node(element); tail = head; System.out.println("First element"); } else { - Coordinates newTail = new Coordinates(coord); - Coordinates temp = tail; + node newTail = new node(element); + node temp = tail; temp.next = newTail; newTail.prev = temp; tail = newTail; - System.out.println(coord[0]); + System.out.println(size + " element added"); } // System.out.println("Enqueue works"); size++; } - public double[] dequeue() { - double[] temp = head.coord; + public E dequeue() { + E temp = head.element; head = head.next; // head.prev = null; size--; return temp; } - public double[] last() { + public E last() { if (tail == null) { // System.out.println("in here"); - return new double[]{x,y}; + return defaultE; } else - return tail.coord; + return tail.element; } - public double[] peak() { + public E peak() { if (head == null) { - return new double[]{x,y}; + return defaultE; } else - return head.coord; + return head.element; } public int getSize() { return size; } - public void setX(double x) { - this.x = x; + public void setE(E element) { + defaultE = element; } - public void setY(double y) { - this.y = y; + public E getE() { + return defaultE; } - public double getX() { - return x; - } - - public double getY() { - return y; - } +// public void setX(double x) { +// this.x = x; +// } +// +// public void setY(double y) { +// this.y = y; +// } +// +// public double getX() { +// return x; +// } +// +// public double getY() { +// return y; +// } } diff --git a/core/src/com/group/golf/Physics/node.java b/core/src/com/group/golf/Physics/node.java new file mode 100644 index 0000000..7db5cb4 --- /dev/null +++ b/core/src/com/group/golf/Physics/node.java @@ -0,0 +1,15 @@ +package com.group.golf.Physics; + +/** + * Created by alex_ on 23-May-18. + */ +public class node { + E element; + node next; + node prev; + + public node(E element) { + this.element = element; + next = null; + } +} diff --git a/core/src/com/group/golf/screens/CourseScreen.java b/core/src/com/group/golf/screens/CourseScreen.java index d74f73c..f447768 100644 --- a/core/src/com/group/golf/screens/CourseScreen.java +++ b/core/src/com/group/golf/screens/CourseScreen.java @@ -423,21 +423,19 @@ else if (this.touchFlag) { Vector3 secondV = new Vector3(this.lastX, this.lastY,0); this.cam.unproject(secondV); - System.out.println("firstX=" + this.firstX + ", firstY=" + this.firstY); - System.out.println("lastX=" + this.lastX + ", lastY=" + this.lastY); - double forceX = Math.abs(lastX - firstX) * 20; - double forceY = Math.abs(lastY - firstY) * 20; + double xLength = Math.abs(lastX - firstX); + double yLength = Math.abs(lastY - firstY); if (lastX < firstX ) - forceX *= -1; + xLength *= -1; if (lastY > firstY) - forceY *= -1; + yLength *= -1; double modulus = Math.sqrt(Math.pow((lastX - firstX), 2) + Math.pow((lastY - firstY), 2)); double force = MathLib.map(modulus, 0, 300, 0, 600); - this.engine.hit(forceX, forceY); + this.engine.hit(xLength, yLength); this.hitSound.play(); } From 2a0a9b8370e2db03f77a19b6a3e8fdf895ec59af Mon Sep 17 00:00:00 2001 From: Chimonas <33982690+Chimonas@users.noreply.github.com> Date: Thu, 24 May 2018 00:30:58 +0200 Subject: [PATCH 5/9] all are minor changes --- core/src/com/group/golf/Physics/Physics.java | 28 ++++--------- core/src/com/group/golf/Physics/Queue.java | 41 -------------------- 2 files changed, 8 insertions(+), 61 deletions(-) diff --git a/core/src/com/group/golf/Physics/Physics.java b/core/src/com/group/golf/Physics/Physics.java index 782c639..588b8d9 100644 --- a/core/src/com/group/golf/Physics/Physics.java +++ b/core/src/com/group/golf/Physics/Physics.java @@ -15,7 +15,7 @@ public class Physics { private Course course; private Ball ball; private double[] hitCoord; - private final int fps = 30; + /** * Construct a Physics engine @@ -54,16 +54,15 @@ public void RK4(double h){ accel[i] = new double[]{grav[0] + friction[0], grav[1] + friction[1]}; } - double accelX = (accel[0][0] + 2 * accel[1][0] + 2 * accel[2][0] + accel[3][0])/6; - double accelY = (accel[0][1] + 2 * accel[1][1] + 2 * accel[2][1] + accel[3][1])/6; - ball.setAccelerationX(accelX); - ball.setAccelerationY(accelY); - ball.setVelocityX(velo[0][0] + h * accelX); - ball.setVelocityY(velo[0][1] + h * accelY); + ball.setAccelerationX((accel[0][0] + 2 * accel[1][0] + 2 * accel[2][0] + accel[3][0])/6); + ball.setAccelerationY((accel[0][1] + 2 * accel[1][1] + 2 * accel[2][1] + accel[3][1])/6); -// System.out.println("VelocityX: " + ball.getVelocityX() + " VelocityY: " + ball.getVelocityY()); - if (Math.abs(this.ball.getVelocityX()) < 0.0776 && Math.abs(this.ball.getVelocityY()) < 0.0776) { + ball.setVelocityX(velo[0][0] + h * ball.getAccelerationX()); + ball.setVelocityY(velo[0][1] + h * ball.getAccelerationY()); + + + if (Math.abs(this.ball.getVelocityX()) < 0.05 && Math.abs(this.ball.getVelocityY()) < 0.05) { this.ball.reset(); } @@ -71,15 +70,6 @@ public void RK4(double h){ ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])}; ball.addCoord(coord); -// double[] grav = gravForce(new double[]{ball.getX(),ball.getY()}); -// double[] friction = frictionForce(ball.getVelocityX(),ball.getVelocityY()); - -// ball.setAccelerationX(accelX); -// ball.setAccelerationY(accelY); - - //this is going to be removed -// ball.setX(ball.getX() + h/6 * (velo[0][0] + 2 * velo[1][0] + 2 * velo[2][0] + velo[3][0])); -// ball.setY(ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])); } private double[] updateRKStep(double[] startPos, double[] prev, double h, double k) { @@ -107,8 +97,6 @@ public void hit(double xLength, double yLength) { ball.setAccelerationX(xLength/ball.getMass()); ball.setAccelerationY(yLength/ball.getMass()); -// ball.setVelocityX(frameRate * forceX / ball.getMass()); -// ball.setVelocityY(frameRate * forceY / ball.getMass()); RK4(frameRate); while (this.ball.isMoving()) { diff --git a/core/src/com/group/golf/Physics/Queue.java b/core/src/com/group/golf/Physics/Queue.java index 71d5c85..23a4a5f 100644 --- a/core/src/com/group/golf/Physics/Queue.java +++ b/core/src/com/group/golf/Physics/Queue.java @@ -7,9 +7,6 @@ public class Queue { protected node head; protected node tail; private int size; - private double x; - private double y; - private E defaultE; public Queue() { head = null; @@ -21,67 +18,29 @@ public void enqueue(E element) { if (head == null) { head = new node(element); tail = head; - System.out.println("First element"); } else { node newTail = new node(element); node temp = tail; temp.next = newTail; newTail.prev = temp; tail = newTail; - System.out.println(size + " element added"); } -// System.out.println("Enqueue works"); size++; } public E dequeue() { E temp = head.element; head = head.next; -// head.prev = null; size--; return temp; } public E last() { - if (tail == null) { -// System.out.println("in here"); - return defaultE; - } else return tail.element; } - public E peak() { - if (head == null) { - return defaultE; - } else - return head.element; - } public int getSize() { return size; } - - public void setE(E element) { - defaultE = element; - } - - public E getE() { - return defaultE; - } - -// public void setX(double x) { -// this.x = x; -// } -// -// public void setY(double y) { -// this.y = y; -// } -// -// public double getX() { -// return x; -// } -// -// public double getY() { -// return y; -// } } From 9c89db723e691b64382263ed3700987c672e2ad6 Mon Sep 17 00:00:00 2001 From: Chimonas <33982690+Chimonas@users.noreply.github.com> Date: Thu, 24 May 2018 14:14:21 +0200 Subject: [PATCH 6/9] Hitting walls works --- core/src/com/group/golf/Ball.java | 4 +- .../src/com/group/golf/Physics/Collision.java | 3 + core/src/com/group/golf/Physics/Physics.java | 106 +++++++++++++++- core/src/com/group/golf/Physics/Queue.java | 1 + .../com/group/golf/screens/CourseScreen.java | 118 ++++-------------- 5 files changed, 134 insertions(+), 98 deletions(-) diff --git a/core/src/com/group/golf/Ball.java b/core/src/com/group/golf/Ball.java index 37069cd..040749d 100644 --- a/core/src/com/group/golf/Ball.java +++ b/core/src/com/group/golf/Ball.java @@ -93,8 +93,8 @@ public void limit(double max) { public void render() { this.courseScreen.getGame().batch.begin(); - double[] offsets = new double[]{this.courseScreen.getXoffset(), this.courseScreen.getYoffset()}; - double[] scales = new double[]{this.courseScreen.getScaleX(), this.courseScreen.getScaleY()}; + double[] offsets = new double[]{this.courseScreen.getEngine().getXoffset(), this.courseScreen.getEngine().getYoffset()}; + double[] scales = new double[]{this.courseScreen.getEngine().getScaleX(), this.courseScreen.getEngine().getScaleY()}; double[] real = MathLib.toPixel(new double[]{this.x, this.y}, offsets, scales); this.courseScreen.getGame().batch.draw(this.texture, (float) real[0] - RADIUS, (float) real[1] - RADIUS, RADIUS * 2,RADIUS * 2); diff --git a/core/src/com/group/golf/Physics/Collision.java b/core/src/com/group/golf/Physics/Collision.java index ef74d4a..d6af9e9 100644 --- a/core/src/com/group/golf/Physics/Collision.java +++ b/core/src/com/group/golf/Physics/Collision.java @@ -50,10 +50,13 @@ public boolean isGoalAchieved() { */ public void checkForWalls(double ballX, double ballY) { if (ballX < Ball.RADIUS || ballX > Golf.VIRTUAL_WIDTH - Ball.RADIUS) { + System.out.println("x wall"); + this.ball.setVelocityX(-this.ball.getVelocityX()); } if (ballY < Ball.RADIUS || ballY > Golf.VIRTUAL_HEIGHT - Ball.RADIUS) { + System.out.println("y wall"); this.ball.setVelocityY(-this.ball.getVelocityY()); } } diff --git a/core/src/com/group/golf/Physics/Physics.java b/core/src/com/group/golf/Physics/Physics.java index 588b8d9..92d2897 100644 --- a/core/src/com/group/golf/Physics/Physics.java +++ b/core/src/com/group/golf/Physics/Physics.java @@ -3,6 +3,8 @@ import com.badlogic.gdx.Gdx; import com.group.golf.Ball; import com.group.golf.Course; +import com.group.golf.Golf; +import com.group.golf.math.MathLib; import java.util.ArrayList; @@ -15,7 +17,13 @@ public class Physics { private Course course; private Ball ball; private double[] hitCoord; - + private Collision collision; + private double scaleX; + private double scaleY; + private double xoffset; + private double yoffset; + private double ballX; + private double ballY; /** * Construct a Physics engine @@ -27,12 +35,57 @@ public Physics(Course course, Ball ball) { this.course = course; this.ball = ball; this.hitCoord = new double[2]; + this.collision = new Collision(this.ball, this.course); + + } + + + /** + * Compute the pixel coordinates of the ball + */ + public void computeBallPixels() { + double[] ballPixels = MathLib.toPixel(new double[]{this.ball.getX(), this.ball.getY()}, + new double[]{this.xoffset, this.yoffset}, new double[]{this.scaleX, this.scaleY}); + this.ballX = ballPixels[0]; + this.ballY = ballPixels[1]; + } + + /** + * Compute the scale + */ + public void calcScale() { + double dist = this.course.getDistance(); + double limitDist = 0.40625; + this.scaleX = 0.000625; + while (dist > limitDist) { + this.scaleX *= 2; + limitDist *= 2; + } + this.scaleY = scaleX; + } + + /** + * Compute the screen offsets + */ + public void calcOffsets() { + double x1 = this.course.getStart()[0]; + double x2 = this.course.getGoal()[0]; + double xUnits = Golf.VIRTUAL_WIDTH / (1/this.scaleX); + this.xoffset = (x1 + x2 - xUnits) / 2.0; + double y1 = this.course.getStart()[1]; + double y2 = this.course.getGoal()[1]; + double yUnits = Golf.VIRTUAL_HEIGHT / (1/this.scaleY); + this.yoffset = (y1 + y2 - yUnits) / 2.0; } + //https://www.haroldserrano.com/blog/visualizing-the-runge-kutta-method public void RK4(double h){ - + calcOffsets(); + calcScale(); + computeBallPixels(); + this.collision.checkForWalls(this.ballX, this.ballY); double[][] accel = new double[4][2]; @@ -102,7 +155,6 @@ public void hit(double xLength, double yLength) { while (this.ball.isMoving()) { RK4(frameRate); } - } /** @@ -216,4 +268,52 @@ public double[] getHitCoord() { public void setHitCoord(double[] hitCoord) { this.hitCoord = hitCoord; } + + public void setScaleX(double scaleX) { + this.scaleX = scaleX; + } + + public void setScaleY(double scaleY) { + this.scaleY = scaleY; + } + + public void setXoffset(double xoffset) { + this.xoffset = xoffset; + } + + public void setYoffset(double yoffset) { + this.yoffset = yoffset; + } + + public double getScaleX() { + return scaleX; + } + + public double getScaleY() { + return scaleY; + } + + public double getXoffset() { + return xoffset; + } + + public double getYoffset() { + return yoffset; + } + + public void setBallX(double ballX) { + this.ballX = ballX; + } + + public void setBallY(double ballY) { + this.ballY = ballY; + } + + public double getBallX() { + return ballX; + } + + public double getBallY() { + return ballY; + } } diff --git a/core/src/com/group/golf/Physics/Queue.java b/core/src/com/group/golf/Physics/Queue.java index 23a4a5f..35a4df6 100644 --- a/core/src/com/group/golf/Physics/Queue.java +++ b/core/src/com/group/golf/Physics/Queue.java @@ -3,6 +3,7 @@ /** * Created by alex_ on 23-May-18. */ + public class Queue { protected node head; protected node tail; diff --git a/core/src/com/group/golf/screens/CourseScreen.java b/core/src/com/group/golf/screens/CourseScreen.java index f447768..2952c8d 100644 --- a/core/src/com/group/golf/screens/CourseScreen.java +++ b/core/src/com/group/golf/screens/CourseScreen.java @@ -54,19 +54,13 @@ public class CourseScreen implements Screen { // Graphing things private int goalSize; - private double scaleX; - private double scaleY; - private double xoffset; - private double yoffset; private double[][] heights; private double maximum; private double minimum; private Color[][] colors; - private double ballX; - private double ballY; // Gaming stuff - boolean touchFlag; + private boolean touchFlag; private int firstX; private int firstY; private int lastX; @@ -173,6 +167,10 @@ private void setupCommon() { this.music.setVolume(0.2f); this.music.setLooping(true); + // Setup engine and collision system + this.engine = new Physics(this.course, this.ball); + this.collision = new Collision(this.ball, this.course); + // Setup Course this.setUpCourse(); @@ -180,19 +178,7 @@ private void setupCommon() { this.goalSize = 20; this.flag = new Texture(Gdx.files.internal("golf_flag.png")); - // Setup engine and collision system - this.engine = new Physics(this.course, this.ball); - this.collision = new Collision(this.ball, this.course); - } - /** - * Compute the pixel coordinates of the ball - */ - private void computeBallPixels() { - double[] ballPixels = MathLib.toPixel(new double[]{this.ball.getX(), this.ball.getY()}, - new double[]{this.xoffset, this.yoffset}, new double[]{this.scaleX, this.scaleY}); - this.ballX = ballPixels[0]; - this.ballY = ballPixels[1]; } /** @@ -200,8 +186,8 @@ private void computeBallPixels() { */ private void setUpCourse() { if (!this.course.isSpline()) { // Do normal setup - this.calcScale(); - this.calcOffsets(); + this.engine.calcScale(); + this.engine.calcOffsets(); } else { // Do spline setup this.splineSetup(); } @@ -212,33 +198,6 @@ private void setUpCourse() { this.calcColorsMatrix(); } - /** - * Compute the scale - */ - private void calcScale() { - double dist = this.course.getDistance(); - double limitDist = 0.40625; - this.scaleX = 0.000625; - while (dist > limitDist) { - this.scaleX *= 2; - limitDist *= 2; - } - this.scaleY = scaleX; - } - - /** - * Compute the screen offsets - */ - private void calcOffsets() { - double x1 = this.course.getStart()[0]; - double x2 = this.course.getGoal()[0]; - double xUnits = Golf.VIRTUAL_WIDTH / (1/this.scaleX); - this.xoffset = (x1 + x2 - xUnits) / 2.0; - double y1 = this.course.getStart()[1]; - double y2 = this.course.getGoal()[1]; - double yUnits = Golf.VIRTUAL_HEIGHT / (1/this.scaleY); - this.yoffset = (y1 + y2 - yUnits) / 2.0; - } /** * Compute the heights matrix @@ -249,8 +208,8 @@ private void calcHeightsMatrix() { this.minimum = Double.MAX_VALUE; for (int x = 0; x < this.heights.length; x++) { for (int y = 0; y < this.heights[x].length; y++) { - double value = this.course.getHeight(this.xoffset + x*this.scaleX, - this.yoffset + y*this.scaleY); + double value = this.course.getHeight(this.engine.getXoffset() + x*this.engine.getScaleX(), + this.engine.getYoffset() + y*this.engine.getScaleY()); if (value > this.maximum) this.maximum = value; else if (value < this.minimum) this.minimum = value; this.heights[x][y] = value; @@ -292,8 +251,8 @@ private void splineSetup() { // Setup Offsets BicubicInterpolator botLeftInterp = (BicubicInterpolator) this.course.getFunctions()[0][0]; Point3D[][] points = botLeftInterp.getPoints(); - this.xoffset = points[0][0].getX(); - this.yoffset = points[0][0].getY(); + this.engine.setXoffset(points[0][0].getX()); + this.engine.setYoffset(points[0][0].getY()); // Setup scales int xLength = this.course.getFunctions().length; @@ -303,13 +262,13 @@ private void splineSetup() { BicubicInterpolator botRightInterp = (BicubicInterpolator) this.course.getFunctions()[xLength - 1][0]; Point3D[][] botRightPoints = botRightInterp.getPoints(); double rightX = botRightPoints[1][0].getX(); - this.scaleX = (rightX - this.xoffset) / Golf.VIRTUAL_WIDTH; + this.engine.setScaleX((rightX - this.engine.getXoffset()) / Golf.VIRTUAL_WIDTH); // scaleY BicubicInterpolator topLeftInterp = (BicubicInterpolator) this.course.getFunctions()[0][yLength - 1]; Point3D[][] topLeftPoints = topLeftInterp.getPoints(); double topY = botRightPoints[0][1].getY(); - this.scaleY = (topY - this.yoffset) / Golf.VIRTUAL_HEIGHT; + this.engine.setScaleY((topY - this.engine.getYoffset()) / Golf.VIRTUAL_HEIGHT); } @Override @@ -334,10 +293,10 @@ public void render(float delta) { this.renderGoal(); // Check the walls - this.collision.checkForWalls(this.ballX, this.ballY); +// this.collision.checkForWalls(this.ballX, this.ballY); // Update pixel position of ball - this.computeBallPixels(); + this.engine.computeBallPixels(); @@ -372,7 +331,7 @@ else if (this.moves != null && this.counter < this.moves.size()) { // Mode 2 is this.ball.limit(this.course.getVmax()); // Recompute pixel positions of the ball - this.computeBallPixels(); + this.engine.computeBallPixels(); // Check for water if (this.collision.ballInWater()) { @@ -383,7 +342,7 @@ else if (this.moves != null && this.counter < this.moves.size()) { // Mode 2 is } // And again, before every check recompute pixel position of the ball - this.computeBallPixels(); + this.engine.computeBallPixels(); // Render the ball this.ball.render(); @@ -448,8 +407,8 @@ else if (this.touchFlag) { */ private void renderGoal() { this.game.shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); - double[] real = MathLib.toPixel(this.course.getGoal(), new double[]{this.xoffset, this.yoffset}, - new double[]{this.scaleX, this.scaleY}); + double[] real = MathLib.toPixel(this.course.getGoal(), new double[]{this.engine.getXoffset(), this.engine.getYoffset()}, + new double[]{this.engine.getScaleX(), this.engine.getScaleY()}); float realX = (float) real[0]; float realY = (float) real[1]; this.game.shapeRenderer.setColor(0, 0, 0, 1); @@ -458,8 +417,8 @@ private void renderGoal() { this.game.shapeRenderer.end(); this.game.shapeRenderer.begin(ShapeRenderer.ShapeType.Line); double tolerance = this.course.getTolerance(); - float toleranceX = (float) (tolerance * 1/(this.scaleX)); - float toleranceY = (float) (tolerance * 1/(this.scaleY)); + float toleranceX = (float) (tolerance * 1/(this.engine.getScaleX())); + float toleranceY = (float) (tolerance * 1/(this.engine.getScaleY())); this.game.shapeRenderer.setColor(1, 0, 0, 1); this.game.shapeRenderer.ellipse(realX - toleranceX, realY - toleranceY, toleranceX*2, toleranceY*2); @@ -564,37 +523,6 @@ public void setGoalSize(int goalSize) { this.goalSize = goalSize; } - public double getScaleX() { - return scaleX; - } - - public void setScaleX(double scaleX) { - this.scaleX = scaleX; - } - - public double getScaleY() { - return scaleY; - } - - public void setScaleY(double scaleY) { - this.scaleY = scaleY; - } - - public double getXoffset() { - return xoffset; - } - - public void setXoffset(double xoffset) { - this.xoffset = xoffset; - } - - public double getYoffset() { - return yoffset; - } - - public void setYoffset(double yoffset) { - this.yoffset = yoffset; - } public double[][] getHeights() { return heights; @@ -627,4 +555,8 @@ public Color[][] getColors() { public void setColors(Color[][] colors) { this.colors = colors; } + + public Physics getEngine() { + return engine; + } } From 924aa3c92ca1f6f0229d94c51fd7dda67fb0c85b Mon Sep 17 00:00:00 2001 From: Chimonas <33982690+Chimonas@users.noreply.github.com> Date: Fri, 25 May 2018 18:25:01 +0200 Subject: [PATCH 7/9] everything is working --- core/src/com/group/golf/Ball.java | 8 +- .../src/com/group/golf/Physics/Collision.java | 18 ++- core/src/com/group/golf/Physics/Physics.java | 127 ++++----------- core/src/com/group/golf/Physics/Queue.java | 5 + .../com/group/golf/screens/CourseScreen.java | 152 ++++++++++++++---- 5 files changed, 175 insertions(+), 135 deletions(-) diff --git a/core/src/com/group/golf/Ball.java b/core/src/com/group/golf/Ball.java index 040749d..2e232a6 100644 --- a/core/src/com/group/golf/Ball.java +++ b/core/src/com/group/golf/Ball.java @@ -54,8 +54,6 @@ public Ball(double mass) { this.velocityX = 0; this.velocityY = 0; this.collisionCircle = new Circle((float) this.x, (float) this.y, RADIUS); -// this.movementCoord = new Queue(); - } public double[] dequeue() { @@ -93,8 +91,8 @@ public void limit(double max) { public void render() { this.courseScreen.getGame().batch.begin(); - double[] offsets = new double[]{this.courseScreen.getEngine().getXoffset(), this.courseScreen.getEngine().getYoffset()}; - double[] scales = new double[]{this.courseScreen.getEngine().getScaleX(), this.courseScreen.getEngine().getScaleY()}; + double[] offsets = new double[]{this.courseScreen.getXoffset(), this.courseScreen.getYoffset()}; + double[] scales = new double[]{this.courseScreen.getScaleX(), this.courseScreen.getScaleY()}; double[] real = MathLib.toPixel(new double[]{this.x, this.y}, offsets, scales); this.courseScreen.getGame().batch.draw(this.texture, (float) real[0] - RADIUS, (float) real[1] - RADIUS, RADIUS * 2,RADIUS * 2); @@ -199,6 +197,8 @@ public double getX() { } + + /** * Set a new x-position * @param x the new x-position diff --git a/core/src/com/group/golf/Physics/Collision.java b/core/src/com/group/golf/Physics/Collision.java index d6af9e9..ebf7777 100644 --- a/core/src/com/group/golf/Physics/Collision.java +++ b/core/src/com/group/golf/Physics/Collision.java @@ -20,6 +20,8 @@ public class Collision { private double lastX; private double lastY; + + /** * Create a new instace of Collision * @param ball the ball to evaluate @@ -50,13 +52,11 @@ public boolean isGoalAchieved() { */ public void checkForWalls(double ballX, double ballY) { if (ballX < Ball.RADIUS || ballX > Golf.VIRTUAL_WIDTH - Ball.RADIUS) { - System.out.println("x wall"); - this.ball.setVelocityX(-this.ball.getVelocityX()); } if (ballY < Ball.RADIUS || ballY > Golf.VIRTUAL_HEIGHT - Ball.RADIUS) { - System.out.println("y wall"); +// System.out.println("y wall"); this.ball.setVelocityY(-this.ball.getVelocityY()); } } @@ -87,9 +87,15 @@ public boolean ballInWater() { } } - // Make the current position of the ball the last - this.lastX = ballX; - this.lastY = ballY; + + if (water) { + // Make the current position of the ball the last + this.lastX = Physics.hitCoord[0]; + this.lastY = Physics.hitCoord[1]; + } else { + this.lastX = ballX; + this.lastY = ballY; + } return water; } diff --git a/core/src/com/group/golf/Physics/Physics.java b/core/src/com/group/golf/Physics/Physics.java index 92d2897..2a05277 100644 --- a/core/src/com/group/golf/Physics/Physics.java +++ b/core/src/com/group/golf/Physics/Physics.java @@ -16,14 +16,13 @@ public class Physics { private Course course; private Ball ball; - private double[] hitCoord; private Collision collision; - private double scaleX; - private double scaleY; - private double xoffset; - private double yoffset; - private double ballX; - private double ballY; + private double[] offsets; + private double[] scales; + private boolean water; + public static double[] hitCoord; + + /** * Construct a Physics engine @@ -40,54 +39,12 @@ public Physics(Course course, Ball ball) { } - /** - * Compute the pixel coordinates of the ball - */ - public void computeBallPixels() { - double[] ballPixels = MathLib.toPixel(new double[]{this.ball.getX(), this.ball.getY()}, - new double[]{this.xoffset, this.yoffset}, new double[]{this.scaleX, this.scaleY}); - this.ballX = ballPixels[0]; - this.ballY = ballPixels[1]; - } - /** - * Compute the scale - */ - public void calcScale() { - double dist = this.course.getDistance(); - double limitDist = 0.40625; - this.scaleX = 0.000625; - while (dist > limitDist) { - this.scaleX *= 2; - limitDist *= 2; - } - this.scaleY = scaleX; - } - - /** - * Compute the screen offsets - */ - public void calcOffsets() { - double x1 = this.course.getStart()[0]; - double x2 = this.course.getGoal()[0]; - double xUnits = Golf.VIRTUAL_WIDTH / (1/this.scaleX); - this.xoffset = (x1 + x2 - xUnits) / 2.0; - double y1 = this.course.getStart()[1]; - double y2 = this.course.getGoal()[1]; - double yUnits = Golf.VIRTUAL_HEIGHT / (1/this.scaleY); - this.yoffset = (y1 + y2 - yUnits) / 2.0; - } //https://www.haroldserrano.com/blog/visualizing-the-runge-kutta-method public void RK4(double h){ - calcOffsets(); - calcScale(); - computeBallPixels(); - this.collision.checkForWalls(this.ballX, this.ballY); - - double[][] accel = new double[4][2]; accel[0] = new double[]{ball.getAccelerationX(),ball.getAccelerationY()}; @@ -95,7 +52,6 @@ public void RK4(double h){ velo[0] = new double[]{ball.getVelocityX(),ball.getVelocityY()}; for (int i = 1; i < 4; i++) { - System.out.println(i); double[] v = updateRKStep(velo[0],accel[i-1],h,i); velo[i] = v; @@ -114,15 +70,28 @@ public void RK4(double h){ ball.setVelocityX(velo[0][0] + h * ball.getAccelerationX()); ball.setVelocityY(velo[0][1] + h * ball.getAccelerationY()); + ball.limit(this.course.getVmax()); - if (Math.abs(this.ball.getVelocityX()) < 0.05 && Math.abs(this.ball.getVelocityY()) < 0.05) { - this.ball.reset(); - } double[] coord = new double[]{ball.getX() + h/6 * (velo[0][0] + 2 * velo[1][0] + 2 * velo[2][0] + velo[3][0]), ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])}; ball.addCoord(coord); + double[] ballCoords = MathLib.toPixel(new double[]{ball.getX(),ball.getY()},offsets,scales); + this.collision.checkForWalls(ballCoords[0], ballCoords[1]); + + if (this.collision.ballInWater()) { + ball.reset(); + water = true; + } + + + if (Math.abs(this.ball.getVelocityX()) < 0.07 && Math.abs(this.ball.getVelocityY()) < 0.07) { + this.ball.reset(); + } + + + } private double[] updateRKStep(double[] startPos, double[] prev, double h, double k) { @@ -139,6 +108,7 @@ private double[] updateRKStep(double[] startPos, double[] prev, double h, double * @param yLength the length that the mouse was dragged vertically */ public void hit(double xLength, double yLength) { + water = false; hitCoord[0] = ball.getX(); hitCoord[1] = ball.getY(); @@ -152,9 +122,10 @@ public void hit(double xLength, double yLength) { ball.setAccelerationY(yLength/ball.getMass()); RK4(frameRate); - while (this.ball.isMoving()) { + while (this.ball.isMoving() && !water) { RK4(frameRate); } + } /** @@ -181,6 +152,7 @@ public void movement(float delta) { public double[] frictionForce(double velocityX, double velocityY) { double multiplier = - this.course.getMu() * this.course.getG() / normalLength(velocityX,velocityY); +// System.out.println(multiplier * velocityX); return new double[]{(multiplier * velocityX) ,(multiplier * velocityY)}; } @@ -269,51 +241,20 @@ public void setHitCoord(double[] hitCoord) { this.hitCoord = hitCoord; } - public void setScaleX(double scaleX) { - this.scaleX = scaleX; - } - - public void setScaleY(double scaleY) { - this.scaleY = scaleY; - } - - public void setXoffset(double xoffset) { - this.xoffset = xoffset; - } - - public void setYoffset(double yoffset) { - this.yoffset = yoffset; - } - - public double getScaleX() { - return scaleX; - } - - public double getScaleY() { - return scaleY; - } - - public double getXoffset() { - return xoffset; - } - - public double getYoffset() { - return yoffset; - } - public void setBallX(double ballX) { - this.ballX = ballX; + public void setOffsets(double[] offsets) { + this.offsets = offsets; } - public void setBallY(double ballY) { - this.ballY = ballY; + public void setScales(double[] scales) { + this.scales = scales; } - public double getBallX() { - return ballX; + public boolean isWater() { + return water; } - public double getBallY() { - return ballY; + public void setWater(boolean water) { + this.water = water; } } diff --git a/core/src/com/group/golf/Physics/Queue.java b/core/src/com/group/golf/Physics/Queue.java index 35a4df6..6c0f4c4 100644 --- a/core/src/com/group/golf/Physics/Queue.java +++ b/core/src/com/group/golf/Physics/Queue.java @@ -40,6 +40,11 @@ public E last() { return tail.element; } + public void clear() { + head = null; + tail = null; + size = 0; + } public int getSize() { return size; diff --git a/core/src/com/group/golf/screens/CourseScreen.java b/core/src/com/group/golf/screens/CourseScreen.java index 2952c8d..60a6de5 100644 --- a/core/src/com/group/golf/screens/CourseScreen.java +++ b/core/src/com/group/golf/screens/CourseScreen.java @@ -49,9 +49,6 @@ public class CourseScreen implements Screen { private List moves; private int counter; - // Ball stuff - double[] lastStop; - // Graphing things private int goalSize; private double[][] heights; @@ -66,6 +63,13 @@ public class CourseScreen implements Screen { private int lastX; private int lastY; + private double scaleX; + private double scaleY; + private double xoffset; + private double yoffset; + private double ballX; + private double ballY; + // Bot private Bot bot; @@ -150,9 +154,6 @@ public CourseScreen(final Golf game, Course course, Ball ball, Bot bot) { */ private void setupCommon() { - // Setup lastStop - this.lastStop = MathLib.copyDoubleArr(this.course.getStart()); - // Setup sounds this.hitSound = Gdx.audio.newSound(Gdx.files.internal("golf_hit_1.wav")); this.loseSound = Gdx.audio.newSound(Gdx.files.internal("defeat_2.wav")); @@ -174,6 +175,10 @@ private void setupCommon() { // Setup Course this.setUpCourse(); + this.engine.setOffsets(new double[]{this.xoffset,this.yoffset}); + this.engine.setScales(new double[]{this.scaleX,this.scaleY}); + + // Setup Goal this.goalSize = 20; this.flag = new Texture(Gdx.files.internal("golf_flag.png")); @@ -186,8 +191,8 @@ private void setupCommon() { */ private void setUpCourse() { if (!this.course.isSpline()) { // Do normal setup - this.engine.calcScale(); - this.engine.calcOffsets(); + this.calcScale(); + this.calcOffsets(); } else { // Do spline setup this.splineSetup(); } @@ -208,8 +213,8 @@ private void calcHeightsMatrix() { this.minimum = Double.MAX_VALUE; for (int x = 0; x < this.heights.length; x++) { for (int y = 0; y < this.heights[x].length; y++) { - double value = this.course.getHeight(this.engine.getXoffset() + x*this.engine.getScaleX(), - this.engine.getYoffset() + y*this.engine.getScaleY()); + double value = this.course.getHeight(this.getXoffset() + x*this.getScaleX(), + this.getYoffset() + y*this.getScaleY()); if (value > this.maximum) this.maximum = value; else if (value < this.minimum) this.minimum = value; this.heights[x][y] = value; @@ -251,8 +256,8 @@ private void splineSetup() { // Setup Offsets BicubicInterpolator botLeftInterp = (BicubicInterpolator) this.course.getFunctions()[0][0]; Point3D[][] points = botLeftInterp.getPoints(); - this.engine.setXoffset(points[0][0].getX()); - this.engine.setYoffset(points[0][0].getY()); + this.setXoffset(points[0][0].getX()); + this.setYoffset(points[0][0].getY()); // Setup scales int xLength = this.course.getFunctions().length; @@ -262,13 +267,13 @@ private void splineSetup() { BicubicInterpolator botRightInterp = (BicubicInterpolator) this.course.getFunctions()[xLength - 1][0]; Point3D[][] botRightPoints = botRightInterp.getPoints(); double rightX = botRightPoints[1][0].getX(); - this.engine.setScaleX((rightX - this.engine.getXoffset()) / Golf.VIRTUAL_WIDTH); + this.setScaleX((rightX - this.getXoffset()) / Golf.VIRTUAL_WIDTH); // scaleY BicubicInterpolator topLeftInterp = (BicubicInterpolator) this.course.getFunctions()[0][yLength - 1]; Point3D[][] topLeftPoints = topLeftInterp.getPoints(); double topY = botRightPoints[0][1].getY(); - this.engine.setScaleY((topY - this.engine.getYoffset()) / Golf.VIRTUAL_HEIGHT); + this.setScaleY((topY - this.getYoffset()) / Golf.VIRTUAL_HEIGHT); } @Override @@ -292,11 +297,8 @@ public void render(float delta) { // Render the goal this.renderGoal(); - // Check the walls -// this.collision.checkForWalls(this.ballX, this.ballY); - // Update pixel position of ball - this.engine.computeBallPixels(); + this.computeBallPixels(); @@ -312,9 +314,8 @@ public void render(float delta) { return; } - // Store position - this.lastStop[0] = this.ball.getX(); - this.lastStop[1] = this.ball.getY(); + + // Make a move if (this.bot != null && Gdx.input.isKeyPressed(Input.Keys.SPACE)) this.bot.makeMove(); @@ -328,21 +329,21 @@ else if (this.moves != null && this.counter < this.moves.size()) { // Mode 2 is this.ball.dequeue(); } - this.ball.limit(this.course.getVmax()); + // Recompute pixel positions of the ball - this.engine.computeBallPixels(); + this.computeBallPixels(); // Check for water - if (this.collision.ballInWater()) { - this.ball.reset(); - this.ball.setX(this.lastStop[0]); - this.ball.setY(this.lastStop[1]); + if (this.engine.isWater() && this.ball.getSize() == 0) { + this.ball.clear(); + this.ball.setX(this.engine.getHitCoord()[0]); + this.ball.setY(this.engine.getHitCoord()[1]); this.loseSound.play(0.2f); } // And again, before every check recompute pixel position of the ball - this.engine.computeBallPixels(); + this.computeBallPixels(); // Render the ball this.ball.render(); @@ -402,13 +403,52 @@ else if (this.touchFlag) { } } + + /** + * Compute the pixel coordinates of the ball + */ + public void computeBallPixels() { + double[] ballPixels = MathLib.toPixel(new double[]{this.ball.getX(), this.ball.getY()}, + new double[]{this.xoffset, this.yoffset}, new double[]{this.scaleX, this.scaleY}); + this.ballX = ballPixels[0]; + this.ballY = ballPixels[1]; + } + + /** + * Compute the scale + */ + public void calcScale() { + double dist = this.course.getDistance(); + double limitDist = 0.40625; + this.scaleX = 0.000625; + while (dist > limitDist) { + this.scaleX *= 2; + limitDist *= 2; + } + this.scaleY = scaleX; + } + + /** + * Compute the screen offsets + */ + public void calcOffsets() { + double x1 = this.course.getStart()[0]; + double x2 = this.course.getGoal()[0]; + double xUnits = Golf.VIRTUAL_WIDTH / (1/this.scaleX); + this.xoffset = (x1 + x2 - xUnits) / 2.0; + double y1 = this.course.getStart()[1]; + double y2 = this.course.getGoal()[1]; + double yUnits = Golf.VIRTUAL_HEIGHT / (1/this.scaleY); + this.yoffset = (y1 + y2 - yUnits) / 2.0; + } + /** * Render the goal */ private void renderGoal() { this.game.shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); - double[] real = MathLib.toPixel(this.course.getGoal(), new double[]{this.engine.getXoffset(), this.engine.getYoffset()}, - new double[]{this.engine.getScaleX(), this.engine.getScaleY()}); + double[] real = MathLib.toPixel(this.course.getGoal(), new double[]{this.getXoffset(), this.getYoffset()}, + new double[]{this.getScaleX(), this.getScaleY()}); float realX = (float) real[0]; float realY = (float) real[1]; this.game.shapeRenderer.setColor(0, 0, 0, 1); @@ -417,8 +457,8 @@ private void renderGoal() { this.game.shapeRenderer.end(); this.game.shapeRenderer.begin(ShapeRenderer.ShapeType.Line); double tolerance = this.course.getTolerance(); - float toleranceX = (float) (tolerance * 1/(this.engine.getScaleX())); - float toleranceY = (float) (tolerance * 1/(this.engine.getScaleY())); + float toleranceX = (float) (tolerance * 1/(this.getScaleX())); + float toleranceY = (float) (tolerance * 1/(this.getScaleY())); this.game.shapeRenderer.setColor(1, 0, 0, 1); this.game.shapeRenderer.ellipse(realX - toleranceX, realY - toleranceY, toleranceX*2, toleranceY*2); @@ -559,4 +599,52 @@ public void setColors(Color[][] colors) { public Physics getEngine() { return engine; } + + public double getScaleX() { + return scaleX; + } + + public void setScaleX(double scaleX) { + this.scaleX = scaleX; + } + + public double getScaleY() { + return scaleY; + } + + public void setScaleY(double scaleY) { + this.scaleY = scaleY; + } + + public double getXoffset() { + return xoffset; + } + + public void setXoffset(double xoffset) { + this.xoffset = xoffset; + } + + public double getYoffset() { + return yoffset; + } + + public void setYoffset(double yoffset) { + this.yoffset = yoffset; + } + + public double getBallX() { + return ballX; + } + + public void setBallX(double ballX) { + this.ballX = ballX; + } + + public double getBallY() { + return ballY; + } + + public void setBallY(double ballY) { + this.ballY = ballY; + } } From 02f4ab5fa16ff18fd5c70b5e72e243c744e81af5 Mon Sep 17 00:00:00 2001 From: Chimonas <33982690+Chimonas@users.noreply.github.com> Date: Fri, 25 May 2018 19:44:42 +0200 Subject: [PATCH 8/9] no changes made here --- core/src/com/group/golf/Physics/Collision.java | 2 -- core/src/com/group/golf/Physics/Physics.java | 16 ++++++++-------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/core/src/com/group/golf/Physics/Collision.java b/core/src/com/group/golf/Physics/Collision.java index ebf7777..77e8ad6 100644 --- a/core/src/com/group/golf/Physics/Collision.java +++ b/core/src/com/group/golf/Physics/Collision.java @@ -53,10 +53,8 @@ public boolean isGoalAchieved() { public void checkForWalls(double ballX, double ballY) { if (ballX < Ball.RADIUS || ballX > Golf.VIRTUAL_WIDTH - Ball.RADIUS) { this.ball.setVelocityX(-this.ball.getVelocityX()); - } if (ballY < Ball.RADIUS || ballY > Golf.VIRTUAL_HEIGHT - Ball.RADIUS) { -// System.out.println("y wall"); this.ball.setVelocityY(-this.ball.getVelocityY()); } } diff --git a/core/src/com/group/golf/Physics/Physics.java b/core/src/com/group/golf/Physics/Physics.java index 2a05277..2136ecf 100644 --- a/core/src/com/group/golf/Physics/Physics.java +++ b/core/src/com/group/golf/Physics/Physics.java @@ -20,9 +20,8 @@ public class Physics { private double[] offsets; private double[] scales; private boolean water; - public static double[] hitCoord; - + public static double[] hitCoord; /** * Construct a Physics engine @@ -39,10 +38,11 @@ public Physics(Course course, Ball ball) { } - - - - + /** + * Sets the acceleration , velocity and position + * after an h time-step using runge-kutta 4th order + * @param h time step + */ //https://www.haroldserrano.com/blog/visualizing-the-runge-kutta-method public void RK4(double h){ double[][] accel = new double[4][2]; @@ -77,7 +77,7 @@ public void RK4(double h){ ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])}; ball.addCoord(coord); - double[] ballCoords = MathLib.toPixel(new double[]{ball.getX(),ball.getY()},offsets,scales); + double[] ballCoords = MathLib.toPixel(coord,offsets,scales); this.collision.checkForWalls(ballCoords[0], ballCoords[1]); if (this.collision.ballInWater()) { @@ -113,7 +113,7 @@ public void hit(double xLength, double yLength) { hitCoord[0] = ball.getX(); hitCoord[1] = ball.getY(); - double frameRate = 0.04; + double frameRate = 0.04; // xLength *= 90; yLength *= 90; From 4b20ea9c1b7a3e774058bff0272c486a83bd4efb Mon Sep 17 00:00:00 2001 From: Julian Marrades Date: Mon, 28 May 2018 16:06:33 +0200 Subject: [PATCH 9/9] FUCK YOU --- core/src/com/group/golf/Ball.java | 9 ++-- .../src/com/group/golf/Physics/Collision.java | 53 ++++--------------- core/src/com/group/golf/Physics/Physics.java | 8 +-- core/src/com/group/golf/ai/GeneticBot.java | 18 +++---- core/src/com/group/golf/ai/botMartijn.java | 28 ++++------ .../golf/listeners/RandomBotListener.java | 0 .../com/group/golf/screens/CourseScreen.java | 6 ++- 7 files changed, 42 insertions(+), 80 deletions(-) delete mode 100644 core/src/com/group/golf/listeners/RandomBotListener.java diff --git a/core/src/com/group/golf/Ball.java b/core/src/com/group/golf/Ball.java index 64755bf..b70f98a 100644 --- a/core/src/com/group/golf/Ball.java +++ b/core/src/com/group/golf/Ball.java @@ -201,9 +201,9 @@ public void setMass(double mass) { * @return the x-coordinate for position */ public double getX() { - if (super.tail == null) +// if (super.tail == null) return x; - return super.last()[0]; +// return super.last()[0]; } @@ -223,9 +223,9 @@ public void setX(double x) { * @return the y-coordinate for position */ public double getY() { - if (super.tail == null) +// if (super.tail == null) return y; - return super.last()[1]; +// return super.last()[1]; } /** @@ -246,6 +246,7 @@ public void setPosition(double x, double y) { this.x = x; this.y = y; this.updateCollisionCircle(); + } public void setAccelerationX(double accelerationX) { this.accelerationX = accelerationX; diff --git a/core/src/com/group/golf/Physics/Collision.java b/core/src/com/group/golf/Physics/Collision.java index 97384c2..bee22b5 100644 --- a/core/src/com/group/golf/Physics/Collision.java +++ b/core/src/com/group/golf/Physics/Collision.java @@ -18,9 +18,6 @@ public class Collision { private Ball ball; private final Course course; - private double[] offsets; - private double[] scales; - private double lastX; private double lastY; @@ -30,29 +27,19 @@ public class Collision { * Create a new instace of Collision * @param ball the ball to evaluate * @param course the course in which the ball rolls - * @param offsets the offsets of the course - * @param scales the scales of the course */ - public Collision(Ball ball, Course course, double[] offsets, double[] scales) { + public Collision(Ball ball, Course course) { this.ball = ball; this.course = course; - this.offsets = offsets; - this.scales = scales; this.lastX = this.course.getStart()[0]; this.lastX = this.course.getStart()[1]; } - /** - * Create a new instance of Collision from a template - * @param other the template - */ public Collision(Collision other) { - this.ball = other.ball; this.course = other.course; - this.offsets = other.offsets; - this.scales = other.scales; + this.ball = other.ball; this.lastX = other.lastX; - this.lastX = other.lastY; + this.lastY = other.lastY; } /** @@ -68,15 +55,17 @@ public boolean isGoalAchieved() { /** * React when the ball hits a wall + * @param ballX the pixel-x position of the ball + * @param ballY the pixel-y position of the ball */ - public void checkForWalls() { - double[] real = MathLib.toPixel(new double[]{this.ball.getX(), this.ball.getY()}, this.offsets, this.scales); + public void checkForWalls(double ballX, double ballY) { double vx = this.ball.getVelocityX(); double vy = this.ball.getVelocityY(); - if ((real[0] < Ball.RADIUS && vx < 0) || (real[0] > Golf.VIRTUAL_WIDTH - Ball.RADIUS && vx > 0)) { + if ((ballX < Ball.RADIUS && vx < 0) || (ballX > Golf.VIRTUAL_WIDTH - Ball.RADIUS && vx > 0)) { this.ball.setVelocityX(-this.ball.getVelocityX()); + } - if ((real[1] < Ball.RADIUS && vy < 0) || (real[1] > Golf.VIRTUAL_HEIGHT - Ball.RADIUS && vy > 0)) { + if ((ballY < Ball.RADIUS && vy < 0) || (ballY > Golf.VIRTUAL_HEIGHT - Ball.RADIUS && vy > 0)) { this.ball.setVelocityY(-this.ball.getVelocityY()); } } @@ -88,8 +77,8 @@ public void checkForWalls() { public boolean ballInWater() { boolean water = false; - double ballX = this.ball.getX(); - double ballY = this.ball.getY(); + double ballX = this.ball.last()[0]; + double ballY = this.ball.last()[1]; Line2D path = new Line2D(this.lastX, this.lastY, ballX, ballY); // Evaluate the line @@ -152,24 +141,4 @@ public Ball getBall() { public void setBall(Ball ball) { this.ball = ball; } - - public Course getCourse() { - return course; - } - - public double[] getOffsets() { - return offsets; - } - - public void setOffsets(double[] offsets) { - this.offsets = offsets; - } - - public double[] getScales() { - return scales; - } - - public void setScales(double[] scales) { - this.scales = scales; - } } diff --git a/core/src/com/group/golf/Physics/Physics.java b/core/src/com/group/golf/Physics/Physics.java index 2cc2ec8..78273ea 100644 --- a/core/src/com/group/golf/Physics/Physics.java +++ b/core/src/com/group/golf/Physics/Physics.java @@ -56,7 +56,7 @@ public void RK4(double h){ double[] v = updateRKStep(velo[0],accel[i-1],h,i); velo[i] = v; - double[] p = updateRKStep(new double[]{ball.getX(),ball.getY()},v,h,i); + double[] p = updateRKStep(ball.last(),v,h,i); double[] grav = gravForce(p); double[] friction = frictionForce(v[0],v[1]); @@ -74,8 +74,8 @@ public void RK4(double h){ ball.limit(this.course.getVmax()); - double[] coord = new double[]{ball.getX() + h/6 * (velo[0][0] + 2 * velo[1][0] + 2 * velo[2][0] + velo[3][0]), - ball.getY() + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])}; + double[] coord = new double[]{ball.last()[0] + h/6 * (velo[0][0] + 2 * velo[1][0] + 2 * velo[2][0] + velo[3][0]), + ball.last()[1] + h/6 * (velo[0][1] + 2 * velo[1][1] + 2 * velo[2][1] + velo[3][1])}; ball.addCoord(coord); double[] ballCoords = MathLib.toPixel(coord,offsets,scales); @@ -114,6 +114,8 @@ public void hit(double xLength, double yLength) { hitCoord[0] = ball.getX(); hitCoord[1] = ball.getY(); + ball.addCoord(hitCoord); + double frameRate = 0.04; // xLength *= 90; diff --git a/core/src/com/group/golf/ai/GeneticBot.java b/core/src/com/group/golf/ai/GeneticBot.java index 15b1447..73a4a32 100644 --- a/core/src/com/group/golf/ai/GeneticBot.java +++ b/core/src/com/group/golf/ai/GeneticBot.java @@ -81,7 +81,6 @@ public void setPhysics(Physics physics) { public void setCollision(Collision collision) { this.collision = collision; this.virtualCollision = new Collision(collision); - this.virtualCollision.setBall(this.virtualBall); this.startEvolution(); } @@ -224,7 +223,7 @@ public void fillLandings(JVector2[] forces, JVector2[] landings) { this.virtualBall.reset(); this.virtualBall.setPosition(landings[0].getX(), landings[0].getY()); for (int i = 1; i < landings.length; i++) { - this.simulateShot(forces[i-1], landings[i-1]); + this.simulateShot(forces[i-1]); landings[i] = new JVector2(this.virtualBall.getX(), this.virtualBall.getY()); } } @@ -232,15 +231,14 @@ public void fillLandings(JVector2[] forces, JVector2[] landings) { /** * Simulate a shot */ - private void simulateShot(JVector2 force, JVector2 last) { + private void simulateShot(JVector2 force) { this.virtualEngine.hit(force.getX(), force.getY()); - while (this.virtualBall.isMoving()) { - this.virtualCollision.checkForWalls(); - this.virtualEngine.movement(Gdx.graphics.getDeltaTime(), false); - this.virtualBall.limit(this.course.getVmax()); - if (this.virtualCollision.ballInWater()) { - this.virtualBall.reset(); - this.virtualBall.setPosition(last.getX(), last.getY()); + while (this.virtualBall.getSize() != 0) { + this.virtualBall.dequeue(); + if (this.virtualEngine.isWater() && this.virtualBall.getSize() == 0) { + this.virtualBall.clear(); + this.virtualBall.setX(this.engine.getHitCoord()[0]); + this.virtualBall.setY(this.engine.getHitCoord()[1]); } } } diff --git a/core/src/com/group/golf/ai/botMartijn.java b/core/src/com/group/golf/ai/botMartijn.java index cd98b52..e9a8580 100644 --- a/core/src/com/group/golf/ai/botMartijn.java +++ b/core/src/com/group/golf/ai/botMartijn.java @@ -140,27 +140,17 @@ private double[] Point3Dto2D(Point3D point){ return p; } - private void simulateShot(JVector2 force, JVector2 last) { - this.virtualEngine.hit(force.getX(),force.getY()); - while (this.virtualBall.isMoving()) { - this.virtualEngine.movement(Gdx.graphics.getDeltaTime(), false); - this.virtualBall.limit(this.course.getVmax()); - this.virtualCollision.checkForWalls(); - if (this.virtualCollision.ballInWater()) { - this.virtualBall.reset(); - this.virtualBall.setPosition(last.getX(), last.getY()); - } - } - } - private void simulateShot(JVector2 force) { - this.virtualEngine.hit(force.getX(),force.getY()); - while (this.virtualBall.isMoving()) { - this.virtualEngine.movement(Gdx.graphics.getDeltaTime(), false); - this.virtualBall.limit(this.course.getVmax()); - this.virtualCollision.checkForWalls(); + this.virtualEngine.hit(force.getX(), force.getY()); + while (this.virtualBall.getSize() != 0) { + this.virtualBall.dequeue(); + if (this.virtualEngine.isWater() && this.virtualBall.getSize() == 0) { + this.virtualBall.clear(); + this.virtualBall.setX(this.engine.getHitCoord()[0]); + this.virtualBall.setY(this.engine.getHitCoord()[1]); + } + } } - } diff --git a/core/src/com/group/golf/listeners/RandomBotListener.java b/core/src/com/group/golf/listeners/RandomBotListener.java deleted file mode 100644 index e69de29..0000000 diff --git a/core/src/com/group/golf/screens/CourseScreen.java b/core/src/com/group/golf/screens/CourseScreen.java index 3f7fe87..7bb4787 100644 --- a/core/src/com/group/golf/screens/CourseScreen.java +++ b/core/src/com/group/golf/screens/CourseScreen.java @@ -186,8 +186,9 @@ private void setupCommon() { // Setup engine and collision system this.engine = new Physics(this.course, this.ball); - this.collision = new Collision(this.ball, this.course, new double[]{this.xoffset, this.yoffset}, - new double[]{this.scaleX, this.scaleY}); + this.collision = new Collision(this.ball, this.course); + this.engine.setOffsets(new double[]{this.xoffset, this.yoffset}); + this.engine.setScales(new double[]{this.scaleX, this.scaleY}); } @@ -341,6 +342,7 @@ else if (this.moves != null && this.counter < this.moves.size() && this.ball.dequeue(); } + this.computeBallPixels(); // Check for water if (this.engine.isWater() && this.ball.getSize() == 0) {