fix: Bow & Arrow partially working

This commit is contained in:
Laureηt 2021-04-09 16:30:24 +02:00
parent c70d2beace
commit 6a95fe7d3a
5 changed files with 84 additions and 92 deletions

View file

@ -8,7 +8,8 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
import sagittarius.Sagittarius; import sagittarius.SagittariusGame;
import sagittarius.view.GameScreen;
public class Arrow extends Actor { public class Arrow extends Actor {
@ -19,11 +20,8 @@ public class Arrow extends Actor {
private Vector2 force = new Vector2(); private Vector2 force = new Vector2();
private float TTL = 20; private float TTL = 20;
private final float length = 100;
private boolean active = true; private BitmapFont fontDebug = new BitmapFont();
private Planet crash;
private Vector2 offset = new Vector2();
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
@ -51,55 +49,29 @@ public class Arrow extends Actor {
*/ */
private Vector2 computeForce() { private Vector2 computeForce() {
Vector2 force = new Vector2(); Vector2 force = new Vector2();
for (Planet attractor : Sagittarius.planetList) { for (Actor actor : GameScreen.planets.getChildren()) {
Vector2 diff = attractor.position.cpy().sub(this.position);
Vector2 attraction = diff.scl( Sagittarius.G * attractor.mass / diff.len2() ); float dx = actor.getX() - this.getX();
force.add(attraction); float dy = actor.getX() - this.getX();
}
for (Moon attractor : Sagittarius.moonList) { float len2 = dx*dx + dy*dy;
Vector2 diff = attractor.position.cpy().sub(this.position); float coeff = SagittariusGame.G * ((Planet) actor).getMass() * (float) Math.pow(len2, -3/2);
Vector2 attraction = diff.scl( Sagittarius.G * attractor.mass / diff.len2() );
force.add(attraction); float gravityX = coeff * dx;
float gravityY = coeff * dy;
force.add(gravityX, gravityY);
} }
return force; return force;
} }
/** @Override
* Verify whether or not the Arrow contacts an Entity. public void act(float dt) {
*/
private void verifyActivity() {
for (Planet planet : Sagittarius.planetList) {
if (this.distanceTo(planet) < planet.getRadius()) {
this.active = false;
this.crash = planet;
this.offset = this.position.cpy().sub(planet.position);
}
}
for (Moon moon : Sagittarius.moonList) {
if (this.distanceTo(moon) < moon.getRadius()) {
this.active = false;
this.crash = moon;
this.offset = this.position.cpy().sub(moon.position);
}
}
}
/** integrationVerlet(dt);
* Updates the physical attributes of the Arrow, this.TTL -= dt;
* must be called in the main update loop. this.setRotation(this.velocity.angleDeg());
*
* @param dt time elapsed between 2 frames.
*/
public void update(float dt) {
if (this.active) {
verifyActivity();
integrationVerlet(dt);
this.TTL -= dt;
this.angle = this.velocity.angleDeg();
} else {
this.position = crash.position.cpy().add(offset);
}
} }
/** /**
@ -114,8 +86,8 @@ public class Arrow extends Actor {
this.acceleration = this.force.cpy(); this.acceleration = this.force.cpy();
this.position.x += dt * ( this.velocity.x + dt * this.acceleration.x / 2 ); this.moveBy(dt * ( this.velocity.x + dt * this.acceleration.x / 2 ),
this.position.y += dt * ( this.velocity.y + dt * this.acceleration.y / 2 ); dt * ( this.velocity.y + dt * this.acceleration.y / 2 ));
this.force = computeForce(); this.force = computeForce();
@ -126,21 +98,19 @@ public class Arrow extends Actor {
// ---------- GRAPHICAL METHODs ---------- // ---------- GRAPHICAL METHODs ----------
public void render(ShapeRenderer shapeRenderer) { public void render(ShapeRenderer shapeRenderer) {
Vector2 tail = new Vector2(-this.length, 0).rotateDeg(this.angle).add(this.position); // Vector2 tail = new Vector2(-this.length, 0).rotateDeg(this.getRotation()).add(this.position);
shapeRenderer.line(this.position, tail); // shapeRenderer.line(this.getX(), this.getY(), tail.x, tail.y);
} }
public void renderDebug(Batch batch, BitmapFont font) { @Override
public void draw(Batch batch, float parentAlpha) {
// TODO : dirty, do it in an other way ? super.draw(batch, parentAlpha);
if (active) { fontDebug.draw(batch, "TTL=" + this.TTL, getX(), getY() + fontDebug.getCapHeight()*5);
font.draw(batch, "TTL = " + this.TTL, this.position.x, this.position.y + font.getCapHeight()*5); fontDebug.draw(batch, "pos=" + getX() + "," + getY(), getX(), getY() + fontDebug.getCapHeight()*4);
font.draw(batch, "pos = " + this.position, this.position.x, this.position.y + font.getCapHeight()*4); fontDebug.draw(batch, "speed=" + this.velocity, getX(), getY() + fontDebug.getCapHeight()*3);
font.draw(batch, "speed = " + this.velocity, this.position.x, this.position.y + font.getCapHeight()*3); fontDebug.draw(batch, "accel=" + this.acceleration, getX(), getY() + fontDebug.getCapHeight()*2);
font.draw(batch, "accel = " + this.acceleration, this.position.x, this.position.y + font.getCapHeight()*2); fontDebug.draw(batch, "force=" + this.force, getX(), getY() + fontDebug.getCapHeight()*1);
font.draw(batch, "force = " + this.force, this.position.x, this.position.y + font.getCapHeight()*1); fontDebug.draw(batch, "angle=" + getRotation(), getX(), getY());
font.draw(batch, "angle = " + this.angle, this.position.x, this.position.y);
}
} }
// ---------- STATIC METHODs ---------- // ---------- STATIC METHODs ----------
@ -157,11 +127,9 @@ public class Arrow extends Actor {
ArrayList<Float> path = new ArrayList<Float>(); ArrayList<Float> path = new ArrayList<Float>();
Arrow dummyArrow = new Arrow(angle, power, shooter); Arrow dummyArrow = new Arrow(angle, power, shooter);
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
dummyArrow.verifyActivity();
if (!dummyArrow.active) { break; }
dummyArrow.integrationVerlet(timeStep); dummyArrow.integrationVerlet(timeStep);
path.add(dummyArrow.position.x); path.add(dummyArrow.getX());
path.add(dummyArrow.position.y); path.add(dummyArrow.getY());
} }
// TODO : optimize, lots of useless stuff + change name // TODO : optimize, lots of useless stuff + change name
@ -176,4 +144,8 @@ public class Arrow extends Actor {
return arr; return arr;
} }
public Vector2 getPosition() {
return new Vector2( getX(), getY() );
}
} }

View file

@ -7,7 +7,7 @@ import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
import sagittarius.Sagittarius; import sagittarius.view.GameScreen;
class Bow extends Actor { class Bow extends Actor {
@ -18,8 +18,8 @@ class Bow extends Actor {
private boolean pressed = false; private boolean pressed = false;
private float angle; private float angle;
private Vector2 anchor; private Vector2 anchor = new Vector2();
private Vector2 aim; private Vector2 aim = new Vector2();
private float power; private float power;
@ -36,47 +36,55 @@ class Bow extends Actor {
@Override @Override
public void act(float dt) { public void act(float dt) {
// TODO: probably can do better with an eventListener
if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) { if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) {
this.anchor = Sagittarius.worldCursor.cpy(); this.anchor = GameScreen.worldCursor.cpy();
pressed = true; pressed = true;
} else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) { } else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) {
computeArrow(); computeArrow();
} else if (pressed) { } else if (pressed) {
Sagittarius.arrowList.add(getArrow()); // Sagittarius.arrowList.add(getArrow());
pressed = false; pressed = false;
} }
} }
/** // /**
* Generates an Arrow according to the Bow's attributes. // * Generates an Arrow according to the Bow's attributes.
* // *
* @return an Arrow. // * @return an Arrow.
*/ // */
private Arrow getArrow() { // private Arrow getArrow() {
return new Arrow(angle, power, shooter); // return new Arrow(angle, power, shooter);
} // }
/** /**
* Converts the Bow's attributes to apply it * Converts the Bow's attributes to apply it
* to the Arrow's constructor. * to the Arrow's constructor.
*/ */
private void computeArrow() { private void computeArrow() {
aim = this.anchor.cpy().sub(Sagittarius.worldCursor); aim = this.anchor.cpy().sub(GameScreen.worldCursor);
angle = aim.angleDeg(); angle = aim.angleDeg();
power = MathUtils.clamp(aim.len(), 0, 1000); power = MathUtils.clamp(aim.len(), 0, 1000);
} }
void render(ShapeRenderer shapeRenderer) { @Override
public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes);
if (pressed) { if (pressed) {
shapeRenderer.line(this.anchor, Sagittarius.worldCursor); shapes.line(this.anchor, GameScreen.worldCursor);
if (aimAssist) {
float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f);
if (traj.length > 2) {
shapeRenderer.polyline(traj);
}
}
} }
} }
// void render(ShapeRenderer shapeRenderer) {
// if (pressed) {
// if (aimAssist) {
// float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f);
// if (traj.length > 2) {
// shapeRenderer.polyline(traj);
// }
// }
// }
// })
} }

View file

@ -55,4 +55,12 @@ public class Planet extends Group {
this.addActor(moon); this.addActor(moon);
} }
public float getMass() {
return mass;
}
public Vector2 getPosition() {
return new Vector2( getX(), getY() );
}
} }

View file

@ -15,7 +15,7 @@ public class Player extends Actor {
// ---------- ATTRIBUTEs ---------- // ---------- ATTRIBUTEs ----------
private Planet home; private Planet home;
// private Bow bow; private Bow bow;
private BitmapFont fontDebug = new BitmapFont(); private BitmapFont fontDebug = new BitmapFont();
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
@ -27,7 +27,7 @@ public class Player extends Actor {
this.setColor(color); this.setColor(color);
this.setOrigin(0, getHeight()/2); this.setOrigin(0, getHeight()/2);
this.home = home; this.home = home;
//this.bow = new Bow(this, true); this.bow = new Bow(this, true);
} }
// ---------- METHODs ---------- // ---------- METHODs ----------
@ -55,6 +55,7 @@ public class Player extends Actor {
getScaleX(), getScaleY(), getScaleX(), getScaleY(),
getRotation()); getRotation());
shapes.line(home.getX(), home.getY(), getX(), getY()); shapes.line(home.getX(), home.getY(), getX(), getY());
bow.drawDebug(shapes);
} }
@Override @Override
@ -70,6 +71,8 @@ public class Player extends Actor {
setX(home.getX() + home.getRadius()*MathUtils.cosDeg(getRotation())); setX(home.getX() + home.getRadius()*MathUtils.cosDeg(getRotation()));
setY(home.getY() + home.getRadius()*MathUtils.sinDeg(getRotation())); setY(home.getY() + home.getRadius()*MathUtils.sinDeg(getRotation()));
bow.act(dt);
} }
/** /**

View file

@ -55,4 +55,5 @@ public abstract class BaseScreen implements Screen {
public Stage getUIStage() { public Stage getUIStage() {
return uiStage; return uiStage;
} }
} }