Jonathan Hager 6 years ago
parent 07d6c433ce
commit 0942dae1de

@ -2,10 +2,13 @@ package com.throwgame.main;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import model.Projectile;
public class ThrowMath { public class ThrowMath {
private double tanA; private double tanA;
private double coefficient; private double coefficient;
private double y0; private double y0;
private int xPos0;
private boolean initialised; private boolean initialised;
public ThrowMath(){ public ThrowMath(){
@ -15,16 +18,17 @@ public class ThrowMath {
this.initialised = false; this.initialised = false;
} }
public void initThrow(double alpha, double g, double v0, double y0){ public void initThrow(double alpha, double g, double v0, double y0, int xPos0){
this.tanA = Math.tan(alpha); this.tanA = Math.tan(alpha);
this.coefficient = g / (2 * Math.pow(v0, 2) * Math.pow(Math.cos(alpha), 2)); this.coefficient = g / (2 * Math.pow(v0, 2) * Math.pow(Math.cos(alpha), 2));
this.y0 = y0; this.y0 = y0;
this.initialised = true; this.initialised = true;
this.xPos0 = xPos0;
} }
public int calculateY(int xPos){ public int calculateY(int xPos){
if(this.initialised){ if(this.initialised){
double res = this.y0 + this.tanA * xPos - this.coefficient * Math.pow(xPos, 2); double res = 1* (this.y0 + this.tanA * (xPos - this.xPos0) - this.coefficient * Math.pow((xPos - this.xPos0), 2));
return (int) res; return (int) res;
} }
else{ else{
@ -33,6 +37,16 @@ public class ThrowMath {
} }
} }
public Vector2 test(Projectile projectile, double g){
Vector2 lol = new Vector2();
lol.x = (int) (projectile.getxPos() + projectile.getvX());
projectile.setvY(projectile.getvY() - g);
lol.y = (int) (projectile.getyPos() + projectile.getvY());
return lol;
}
public Vector2 pivotGetNewPos(double alpha, int xPosPivot, int yPosPivot, int radius){ public Vector2 pivotGetNewPos(double alpha, int xPosPivot, int yPosPivot, int radius){
Vector2 vector = new Vector2(); Vector2 vector = new Vector2();
vector.x = (float) (radius * Math.sin(alpha)); vector.x = (float) (radius * Math.sin(alpha));

@ -14,7 +14,7 @@ import com.throwgame.main.ThrowMath;
* @author Jan * @author Jan
*/ */
public class Level { public class Level {
private final double G = 9.81; private final double G = 0.01;
private final int RADIUS = 100; private final int RADIUS = 100;
private Goal goal; private Goal goal;
@ -43,7 +43,10 @@ public class Level {
public void projectileReleased(){ public void projectileReleased(){
this.isReleased = true; this.isReleased = true;
double v0 = angleSpeed * RADIUS; double v0 = angleSpeed * RADIUS;
this.math.initThrow(angle, G, v0, projectile.getyPos()); this.math.initThrow(Math.PI / 2 + angle, G, v0, projectile.getyPos(), projectile.getxPos());
//double tempAngle = angle + Math.PI / 2;
//projectile.setvX(v0 * Math.sin(tempAngle));
//projectile.setvY(v0 * Math.cos(tempAngle));
} }
public void step(){ public void step(){
@ -59,6 +62,8 @@ public class Level {
angleSpeed += 0.0001; angleSpeed += 0.0001;
angle += angleSpeed; angle += angleSpeed;
System.out.println(Math.toDegrees(angle));
Vector2 newPosVector = math.pivotGetNewPos(this.angle, this.xPosPivot, this.yPosPivot, RADIUS); Vector2 newPosVector = math.pivotGetNewPos(this.angle, this.xPosPivot, this.yPosPivot, RADIUS);
this.projectile.setxPos(xPosPivot + (int) newPosVector.x); this.projectile.setxPos(xPosPivot + (int) newPosVector.x);
this.projectile.setyPos(yPosPivot + (int) newPosVector.y); this.projectile.setyPos(yPosPivot + (int) newPosVector.y);
@ -67,6 +72,11 @@ public class Level {
private void stepAir(){ private void stepAir(){
projectile.setxPos(projectile.getxPos() + 1); projectile.setxPos(projectile.getxPos() + 1);
projectile.setyPos(math.calculateY(projectile.getxPos())); projectile.setyPos(math.calculateY(projectile.getxPos()));
/*Vector2 lol = math.test(projectile, G);
projectile.setxPos((int) lol.x);
projectile.setyPos((int) lol.y);
System.out.println(projectile.getxPos() + " " + projectile.getyPos());
*/
} }
public Goal getGoal() { public Goal getGoal() {

Loading…
Cancel
Save