Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions core/src/com/group/golf/Ball.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
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;


/**
* A class to save the data of the ball
*/
public class Ball {
public class Ball extends Queue<double[]>{

public static final float RADIUS = 10;


private double mass;
private double x;
private double y;
private double velocityX;
private double velocityY;
private double accelerationX;
private double accelerationY;


private Texture texture;
private CourseScreen courseScreen;
Expand Down Expand Up @@ -51,6 +56,17 @@ public Ball(double mass) {
this.collisionCircle = new Circle((float) this.x, (float) this.y, RADIUS);
}

public double[] dequeue() {
double[] coord = super.dequeue();
setX(coord[0]);
setY(coord[1]);
return coord;
}

public void addCoord(double[] coord) {
super.enqueue(coord);
}

/**
* Create a new Ball using another one as a template
* @param other the template ball
Expand Down Expand Up @@ -137,8 +153,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;
}

/**
Expand Down Expand Up @@ -186,10 +201,14 @@ public void setMass(double mass) {
* @return the x-coordinate for position
*/
public double getX() {
return x;
// if (super.tail == null)
return x;
// return super.last()[0];
}




/**
* Set a new x-position
* @param x the new x-position
Expand All @@ -204,7 +223,9 @@ public void setX(double x) {
* @return the y-coordinate for position
*/
public double getY() {
return y;
// if (super.tail == null)
return y;
// return super.last()[1];
}

/**
Expand All @@ -226,4 +247,20 @@ public void setPosition(double x, double y) {
this.y = y;
this.updateCollisionCircle();
}

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;
}
}
66 changes: 21 additions & 45 deletions core/src/com/group/golf/Physics/Collision.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,28 @@ public class Collision {
private Ball ball;
private final Course course;

private double[] offsets;
private double[] scales;

private double lastX;
private double lastY;



/**
* 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;
}

/**
Expand All @@ -66,16 +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());
}
}
Expand All @@ -87,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
Expand All @@ -106,9 +96,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;
}
Expand Down Expand Up @@ -145,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;
}
}
Loading