From 5c5f6bd621c27e556339dc614403c21ceba14c6d Mon Sep 17 00:00:00 2001 From: Awlex Date: Sat, 19 Nov 2016 00:06:55 +0100 Subject: [PATCH] Basic Implementation of a Mover with more than 2 points (GameObjects) and added Jama (Java Matrix) library to the project --- pom.xml | 5 ++ src/awlex/ospu/polymover/Arc.java | 88 +++++++++++++++++++ src/awlex/ospu/polymover/PolyMover.java | 12 +++ .../ospu/polymover/factory/ArcFactory.java | 54 ++++++++++++ .../polymover/factory/MultiMoverFactory.java | 27 ++++++ 5 files changed, 186 insertions(+) create mode 100644 src/awlex/ospu/polymover/Arc.java create mode 100644 src/awlex/ospu/polymover/PolyMover.java create mode 100644 src/awlex/ospu/polymover/factory/ArcFactory.java create mode 100644 src/awlex/ospu/polymover/factory/MultiMoverFactory.java diff --git a/pom.xml b/pom.xml index 60952e38..57346244 100644 --- a/pom.xml +++ b/pom.xml @@ -209,5 +209,10 @@ lzma-java 1.3 + + gov.nist.math + jama + 1.0.3 + \ No newline at end of file diff --git a/src/awlex/ospu/polymover/Arc.java b/src/awlex/ospu/polymover/Arc.java new file mode 100644 index 00000000..ee7e7a77 --- /dev/null +++ b/src/awlex/ospu/polymover/Arc.java @@ -0,0 +1,88 @@ +package awlex.ospu.polymover; + +import Jama.Matrix; +import itdelatrisu.opsu.objects.GameObject; + +import static java.lang.Math.*; + +/** + * Created by Awlex on 13.11.2016. + */ +public class Arc extends PolyMover { + + private GameObject p1, middle, p2; + private double xm, ym, r, alpha, beta, gamma; + + public Arc(GameObject p1, GameObject middle, GameObject p2) { + this.p1 = p1; + this.middle = middle; + this.p2 = p2; + init(); + } + + public Arc(PolyMover mover, GameObject p) { + GameObject[] items = mover.getItems(); + p1 = items[items.length - 2]; + middle = items[items.length - 1]; + p2 = p; + init(); + } + + private void init() { + Matrix m = prepareMatrix(p1, middle, p2); + xm = m.get(2, 1); + ym = m.get(3, 1); + r = pow(xm, 2) + pow(ym, 2) - m.get(1, 1); + alpha = atan2(p1.end.y - ym, p1.end.x - xm); + beta = atan2(middle.end.y - ym, middle.end.x - xm); + gamma = atan2(p2.start.y - ym, p2.start.x - xm); + } + + @Override + public double[] getPoint(int time) { + double percent; + double angle; + if (time < middle.getTime()) { + time -= p1.getEndTime(); + percent = ((double) time) / ((middle.getTime() - p1.getEndTime())); + angle = alpha + beta * percent; + } + else { + time -= middle.getTime(); + percent = ((double) time) / (p2.getTime() - middle.getTime()); + angle = beta + gamma * percent; + } + return new double[]{ + xm + r * cos(angle), + ym + r * sin(angle) + }; + } + + @Override + public GameObject[] getItems() { + return new GameObject[]{ + p1, + middle, + p2 + }; + } + + private Matrix prepareMatrix(GameObject p1, GameObject middle, GameObject p2) { + Matrix a = new Matrix(new double[][]{ + {1, -p1.end.x, -p1.end.y}, + {1, -middle.end.x, -middle.end.y}, + {1, -p2.start.x, -p2.start.y} + }); + Matrix b = new Matrix(new double[][]{ + {-(pow(p1.end.x, 2) + pow(p1.end.y, 2))}, + {-(pow(middle.end.x, 2) + pow(middle.end.y, 2))}, + {-(pow(p2.start.x, 2) + pow(p2.start.y, 2))}, + }); + + return a.solve(b); + } + + public static boolean canCricleExistBetweenItems(GameObject p1, GameObject p2, GameObject p3) { + return !((p1.end.x == p2.start.x && p1.end.x == p3.start.x) || (p1.end.y == p2.start.y && p1.end.y == p3.start.y)); + } +} diff --git a/src/awlex/ospu/polymover/PolyMover.java b/src/awlex/ospu/polymover/PolyMover.java new file mode 100644 index 00000000..32f71a20 --- /dev/null +++ b/src/awlex/ospu/polymover/PolyMover.java @@ -0,0 +1,12 @@ +package awlex.ospu.polymover; + +import itdelatrisu.opsu.objects.GameObject; + +/** + * Created by Awlex on 18.11.2016. + */ +public abstract class PolyMover { + public abstract double[] getPoint(int time); + + public abstract GameObject[] getItems(); +} \ No newline at end of file diff --git a/src/awlex/ospu/polymover/factory/ArcFactory.java b/src/awlex/ospu/polymover/factory/ArcFactory.java new file mode 100644 index 00000000..41ec329a --- /dev/null +++ b/src/awlex/ospu/polymover/factory/ArcFactory.java @@ -0,0 +1,54 @@ +package awlex.ospu.polymover.factory; + +import awlex.ospu.polymover.Arc; +import awlex.ospu.polymover.PolyMover; +import itdelatrisu.opsu.objects.GameObject; + +/** + * Created by Awlex on 18.11.2016. + */ +public class ArcFactory implements MultiMoverFactory { + + private static final int PREFFERED_BUFFER_SIZE = 3; + private PolyMover arc1, arc2; + + @Override + public double[] getPoint(int time) { + if (arc2 == null) { + return arc1.getPoint(time); + } + + double[] point1 = arc1.getPoint(time); + double[] point2 = arc2.getPoint(time); + + return new double[]{ + (point1[0] + point2[0]) * 0.5, + (point1[1] + point2[1]) * 0.5 + }; + } + + + public void init(GameObject[] objects, int startIndex) { + if (objects == null) + throw new NullPointerException("Objects musn't be null"); + if (objects.length < startIndex + 2) + arc1 = new Arc(objects[startIndex], objects[startIndex + 1], objects[startIndex + 2]); + arc2 = null; + } + + public void update(GameObject p) { + if (arc2 != null) + arc1 = arc2; + arc2 = new Arc(arc1, p); + } + + @Override + public int getPrefferedBufferSize() { + return PREFFERED_BUFFER_SIZE; + } + + @Override + public String toString() { + return "Arcs"; + } +} diff --git a/src/awlex/ospu/polymover/factory/MultiMoverFactory.java b/src/awlex/ospu/polymover/factory/MultiMoverFactory.java new file mode 100644 index 00000000..9e750c1f --- /dev/null +++ b/src/awlex/ospu/polymover/factory/MultiMoverFactory.java @@ -0,0 +1,27 @@ +package awlex.ospu.polymover.factory; + +import itdelatrisu.opsu.objects.GameObject; + +/** + * Created by Awlex on 18.11.2016. + */ +public interface MultiMoverFactory { + + + /** + * @param time point in time whose cursor position has to be calculated + * @return [x, y] + */ + double[] getPoint(int time); + + void init(GameObject[] objects, int startIndex); + + void update(GameObject g); + + /** + * How many items the Factory would like to look in the future + * + * @return + */ + int getPrefferedBufferSize(); +}