feat: optimized computeTrajectory

This commit is contained in:
Laureηt 2021-05-12 22:09:38 +02:00
parent e047063e7e
commit d664987ff8
4 changed files with 32 additions and 19 deletions

View file

@ -9,7 +9,7 @@ public class SagittariusGame extends Game {
// Constants // Constants
public static float G = 100; public static float G = 100;
public static boolean debugMode = false; public static boolean debugMode = true;
private static Game game; private static Game game;

View file

@ -1,5 +1,6 @@
package sagittarius.model; package sagittarius.model;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
@ -215,26 +216,16 @@ public class Arrow extends EntityQuad {
* @param timeStep time period used for the integration. * @param timeStep time period used for the integration.
* @return an array of vertices describing the trajectory of the Arrow. * @return an array of vertices describing the trajectory of the Arrow.
*/ */
static float[] traj(float angle, float power, Player shooter, int iterations, float timeStep) { static Trajectory computeTrajectory(float angle, float power, Player shooter, int iterations, float timeStep) {
ArrayList<Float> path = new ArrayList<Float>(); Trajectory traj = new Trajectory(iterations);
Arrow dummyArrow = new Arrow(angle, power, shooter, true); Arrow dummyArrow = new Arrow(angle, power, shooter, true);
for (int i = 0; i < iterations; i++) { for (int i = 0; i < iterations; i++) {
dummyArrow.integrationVerlet(timeStep); dummyArrow.integrationVerlet(timeStep);
path.add(dummyArrow.getX()); traj.add(dummyArrow.getPosition());
path.add(dummyArrow.getY());
if ( dummyArrow.hasLanded() || dummyArrow.hasHit() ) { break; } if ( dummyArrow.hasLanded() || dummyArrow.hasHit() ) { break; }
} }
// TODO : optimize, lots of useless stuff + change name return traj;
// or ignore and use adapters
final float[] arr = new float[path.size()]; // utiliser le count de la fonction, faire tab size iteration*2, et draw avec count
int index = 0; // TODO: créer objet Trajectory, avec un float[iteration*2] et un actualSize;
for (final Float value: path) {
arr[index++] = value;
}
return arr;
} }
@Override @Override

View file

@ -60,10 +60,8 @@ public class Bow extends Actor {
if (power > 50) shapes.setColor(Color.RED); if (power > 50) shapes.setColor(Color.RED);
shapes.line(this.anchor, GameScreen.worldCursor); shapes.line(this.anchor, GameScreen.worldCursor);
if (aimAssist) { if (aimAssist) {
float[] traj = Arrow.traj(angle, power, GameScreen.playerCurrent, 500, 0.03f); Trajectory traj = Arrow.computeTrajectory(angle, power, GameScreen.playerCurrent, 500, 0.03f);
if (traj.length > 2) { shapes.polyline(traj.path, 0, traj.actualSize);
shapes.polyline(traj);
}
} }
} }
} }

View file

@ -0,0 +1,24 @@
package sagittarius.model;
import com.badlogic.gdx.math.Vector2;
public class Trajectory {
float[] path;
int actualSize;
public Trajectory(int iterations) {
path = new float[2*iterations];
}
public void add(float x, float y) {
path[actualSize++] = x;
path[actualSize++] = y;
}
public void add(Vector2 vec) {
path[actualSize++] = vec.x;
path[actualSize++] = vec.y;
}
}