From e848a242e748ae2199a8d4eba3f2be93c758a7ad Mon Sep 17 00:00:00 2001 From: gdamms Date: Tue, 17 May 2022 11:24:17 +0200 Subject: [PATCH] feat: rotation fonctionnelles --- src/main/java/com/tocard/cam/Camera.java | 30 +++-- .../java/com/tocard/cam/ExecuteTraveling.java | 5 +- .../java/com/tocard/cam/ExtendedLocation.java | 69 ++++++++++ src/main/java/com/tocard/cam/ListPoints.java | 2 +- .../com/tocard/cam/LocationQuaternion.java | 127 ------------------ src/main/java/com/tocard/cam/NewPoint.java | 39 +++--- src/main/java/com/tocard/cam/ShowCurve.java | 8 +- 7 files changed, 115 insertions(+), 165 deletions(-) create mode 100644 src/main/java/com/tocard/cam/ExtendedLocation.java delete mode 100644 src/main/java/com/tocard/cam/LocationQuaternion.java diff --git a/src/main/java/com/tocard/cam/Camera.java b/src/main/java/com/tocard/cam/Camera.java index a3a3549..ea0a727 100644 --- a/src/main/java/com/tocard/cam/Camera.java +++ b/src/main/java/com/tocard/cam/Camera.java @@ -17,8 +17,8 @@ import org.bukkit.inventory.ItemStack; public class Camera extends JavaPlugin implements Listener { - public static ArrayList curve = new ArrayList<>(); - public static ArrayList controlPoints = new ArrayList<>(); + public static ArrayList curve = new ArrayList<>(); + public static ArrayList controlPoints = new ArrayList<>(); public static Logger logger; public static Plugin plugin; private static float dt = 0.01f; @@ -78,7 +78,14 @@ public class Camera extends JavaPlugin implements Listener { } } + public static void patchYaws() { + for (int i = 1; i < controlPoints.size(); i++) { + controlPoints.get(i).patchYaw(controlPoints.get(i - 1)); + } + } + public static void compute() { + patchYaws(); int nControlPoints = controlPoints.size(); curve = new ArrayList<>(); @@ -89,7 +96,7 @@ public class Camera extends JavaPlugin implements Listener { if (nControlPoints <= 4) { for (float t = 0; t < 1; t += dt) { - ArrayList P = new ArrayList<>(controlPoints); + ArrayList P = new ArrayList<>(controlPoints); int N = P.size(); for (int k = N - 1; k > 0; k--) { @@ -107,7 +114,7 @@ public class Camera extends JavaPlugin implements Listener { int nBezier = (nControlPoints - 1) / 3; for (int iBezier = 0; iBezier < nBezier; iBezier++) { for (float t = 0; t < 1; t += dt) { - ArrayList P = new ArrayList<>(); + ArrayList P = new ArrayList<>(); for (int k = 0; k < 4; k++) { P.add(controlPoints.get(3 * iBezier + k).clone()); } @@ -130,13 +137,14 @@ public class Camera extends JavaPlugin implements Listener { point.getPitch(), point.getYaw()); } - public static String prettyLocation(LocationQuaternion point) { - return String.format( - "X=%05.2f, Y=%05.2f, Z=%05.2f, P=%05.2f, Y=%05.2f, R=%05.2f, w=%05.2f, x=%05.2f, y=%05.2f, z=%05.2f", - point.getX(), point.getY(), point.getZ(), - point.getPitch(), point.getYaw(), point.roll, - point.qw, point.qx, point.qy, point.qz); - } + // public static String prettyLocation(ExtendedLocation point) { + // return String.format( + // "X=%05.2f, Y=%05.2f, Z=%05.2f, P=%05.2f, Y=%05.2f, R=%05.2f, w=%05.2f, + // x=%05.2f, y=%05.2f, z=%05.2f", + // point.getX(), point.getY(), point.getZ(), + // point.getPitch(), point.getYaw(), point.roll, + // point.qw, point.qx, point.qy, point.qz); + // } public static void broadlog(String msg) { Camera.logger.log(Level.INFO, msg); diff --git a/src/main/java/com/tocard/cam/ExecuteTraveling.java b/src/main/java/com/tocard/cam/ExecuteTraveling.java index 811d92a..c1f9263 100644 --- a/src/main/java/com/tocard/cam/ExecuteTraveling.java +++ b/src/main/java/com/tocard/cam/ExecuteTraveling.java @@ -10,7 +10,7 @@ import java.util.Iterator; public class ExecuteTraveling implements CommandExecutor { private static int taskID = -1; - private static Iterator curveIterator; + private static Iterator curveIterator; @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { @@ -32,8 +32,7 @@ public class ExecuteTraveling implements CommandExecutor { public void run() { // Teleport to next point if (curveIterator.hasNext()) { - LocationQuaternion loc = curveIterator.next(); - Camera.broadlog(Camera.prettyLocation(loc)); + ExtendedLocation loc = curveIterator.next(); player.teleport(loc); return; } diff --git a/src/main/java/com/tocard/cam/ExtendedLocation.java b/src/main/java/com/tocard/cam/ExtendedLocation.java new file mode 100644 index 0000000..60f720a --- /dev/null +++ b/src/main/java/com/tocard/cam/ExtendedLocation.java @@ -0,0 +1,69 @@ +package com.tocard.cam; + +import org.bukkit.Location; + +public class ExtendedLocation extends Location { + + public ExtendedLocation(Location location) { + super(location.getWorld(), + location.getX(), + location.getY(), + location.getZ(), + location.getYaw(), + location.getPitch()); + } + + public ExtendedLocation patchYaw(ExtendedLocation previousLocation) { + float A = previousLocation.getYaw(); + float B = getYaw(); + float b = B % 360; + int wholePart = 360 * (int) (A / 360); + + float minDist = -1; + float sol = 0; + for (int i = -1; i < 2; i++) { + float val = wholePart + b + i * 360; + float dist = Math.abs(val - A); + if (minDist == -1 || dist < minDist) { + sol = val; + minDist = dist; + } + } + + setYaw(sol); + return this; + } + + public ExtendedLocation add(ExtendedLocation loc) { + super.add(loc); + + setYaw(getYaw() + loc.getYaw()); + setPitch(getPitch() + loc.getPitch()); + + return this; + } + + public ExtendedLocation subtract(ExtendedLocation loc) { + super.subtract(loc); + + setYaw(getYaw() - loc.getYaw()); + setPitch(getPitch() - loc.getPitch()); + + return this; + } + + public ExtendedLocation multiply(double m) { + super.multiply(m); + + setYaw((float) m * getYaw()); + setPitch((float) m * getPitch()); + + return this; + } + + @Override + public ExtendedLocation clone() { + ExtendedLocation cloned = new ExtendedLocation(this); + return cloned; + } +} diff --git a/src/main/java/com/tocard/cam/ListPoints.java b/src/main/java/com/tocard/cam/ListPoints.java index 6eae356..20b4c40 100644 --- a/src/main/java/com/tocard/cam/ListPoints.java +++ b/src/main/java/com/tocard/cam/ListPoints.java @@ -8,7 +8,7 @@ import org.bukkit.entity.Player; public class ListPoints implements CommandExecutor { - private static ListIterator points; + private static ListIterator points; @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { diff --git a/src/main/java/com/tocard/cam/LocationQuaternion.java b/src/main/java/com/tocard/cam/LocationQuaternion.java deleted file mode 100644 index af15da5..0000000 --- a/src/main/java/com/tocard/cam/LocationQuaternion.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.tocard.cam; - -import org.bukkit.Location; - -public class LocationQuaternion extends Location { - - public double qw; - public double qx; - public double qy; - public double qz; - public float roll = 0; - - private static final float deg2rad = (float) Math.PI / 180.0f; - - // https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Source_code - public LocationQuaternion(Location location) { - super(location.getWorld(), - location.getX(), - location.getY(), - location.getZ(), - location.getYaw(), - location.getPitch()); - - this.updateQuaternionAngles(); - } - - public void updateEulerAngles() { - // pitch (y-axis rotation) - double pitch; - double sinp = 2 * (qw * qy - qz * qx); - if (Math.abs(sinp) >= 1) { - pitch = Math.copySign(Math.PI / 2, sinp); // use 90 degrees if out of range - } else { - pitch = Math.asin(sinp); - } - setPitch((float) pitch / deg2rad); - - // yaw (z-axis rotation) - double siny_cosp = 2 * (qw * qz + qx * qy); - double cosy_cosp = 1 - 2 * (qy * qy + qz * qz); - double yaw = Math.atan2(siny_cosp, cosy_cosp); - setYaw((float) yaw / deg2rad); - } - - public void updateQuaternionAngles() { - float yaw = getYaw() * deg2rad; - float pitch = getPitch() * deg2rad; - - double cy = Math.cos(yaw * 0.5); - double sy = Math.sin(yaw * 0.5); - double cp = Math.cos(pitch * 0.5); - double sp = Math.sin(pitch * 0.5); - double cr = Math.cos(roll * 0.5); - double sr = Math.sin(roll * 0.5); - - qw = cr * cp * cy + sr * sp * sy; - qx = sr * cp * cy - cr * sp * sy; - qy = cr * sp * cy + sr * cp * sy; - qz = cr * cp * sy - sr * sp * cy; - } - - public LocationQuaternion patchYaw(LocationQuaternion previousLocation) { - float A = previousLocation.getYaw(); - float B = getYaw(); - float b = B % 360; - int wholePart = 360 * (int) (A / 360); - - float minDist = -1; - float sol = 0; - for (int i = -1; i < 2; i++) { - float val = wholePart + b + i * 360; - float dist = Math.abs(val - A); - if (minDist == -1 || dist < minDist) { - sol = val; - minDist = dist; - } - } - - setYaw(sol); - - updateQuaternionAngles(); - return this; - } - - public LocationQuaternion add(LocationQuaternion loc) { - super.add(loc); - qw += loc.qw; - qx += loc.qx; - qy += loc.qy; - qz += loc.qz; - - updateEulerAngles(); - return this; - } - - public LocationQuaternion subtract(LocationQuaternion loc) { - super.subtract(loc); - qw -= loc.qw; - qx -= loc.qx; - qy -= loc.qy; - qz -= loc.qz; - - updateEulerAngles(); - return this; - } - - public LocationQuaternion multiply(double m) { - super.multiply(m); - qw *= m; - qx *= m; - qy *= m; - qz *= m; - - updateEulerAngles(); - return this; - } - - @Override - public LocationQuaternion clone() { - LocationQuaternion cloned = new LocationQuaternion(this); - return cloned; - } - - public double dot(LocationQuaternion q) { - return qx * q.qx + qy * q.qy + qz * q.qz + qw * q.qw; - } -} diff --git a/src/main/java/com/tocard/cam/NewPoint.java b/src/main/java/com/tocard/cam/NewPoint.java index 7345cec..5b32798 100644 --- a/src/main/java/com/tocard/cam/NewPoint.java +++ b/src/main/java/com/tocard/cam/NewPoint.java @@ -16,13 +16,13 @@ public class NewPoint implements CommandExecutor { Player player = (Player) sender; switch (args[0]) { case "add": - add(args, new LocationQuaternion(player.getLocation()), player.getWorld()); + add(args, new ExtendedLocation(player.getLocation()), player.getWorld()); break; case "rm": rm(args, null, null); break; case "set": - set(args, new LocationQuaternion(player.getLocation()), player.getWorld()); + set(args, new ExtendedLocation(player.getLocation()), player.getWorld()); break; default: break; @@ -38,34 +38,35 @@ public class NewPoint implements CommandExecutor { return true; } - public static void addPoint(LocationQuaternion point, World world) { + public static void addPoint(ExtendedLocation point, World world) { Camera.controlPoints.add(point); ShowCurve.add(point, world); Camera.broadlog("Point added: " + Camera.prettyLocation(point)); } - public static void setPoint(int index, LocationQuaternion point, World world) { + public static void setPoint(int index, ExtendedLocation point, World world) { Camera.controlPoints.set(index, point); ShowCurve.set(index, point, world); Camera.broadlog("Point n°" + index + " set: " + Camera.prettyLocation(point)); } public static void rmPoint(int index) { - LocationQuaternion location = Camera.controlPoints.remove(index); + ExtendedLocation location = Camera.controlPoints.remove(index); ShowCurve.rm(index); Camera.broadlog("Point deleted: " + Camera.prettyLocation(location)); } - public static void add(String[] args, LocationQuaternion location, World world) { + public static void add(String[] args, ExtendedLocation location, World world) { int n = Camera.controlPoints.size(); if (n < 4) { addPoint(location, world); } else { - LocationQuaternion P2 = Camera.controlPoints.get(n - 2); - LocationQuaternion P3 = Camera.controlPoints.get(n - 1); - LocationQuaternion P4 = P3.clone().multiply(2).subtract(P2); - LocationQuaternion P5 = location.clone().add(P4).multiply(0.5); + ExtendedLocation P2 = Camera.controlPoints.get(n - 2); + ExtendedLocation P3 = Camera.controlPoints.get(n - 1); + location.patchYaw(P3); + ExtendedLocation P4 = P3.clone().multiply(2).subtract(P2); + ExtendedLocation P5 = location.clone().add(P4).multiply(0.5); addPoint(P4, world); addPoint(P5, world); @@ -74,7 +75,7 @@ public class NewPoint implements CommandExecutor { } @Deprecated - public static void ins(String[] args, LocationQuaternion location, World world) { + public static void ins(String[] args, ExtendedLocation location, World world) { int index = Integer.parseInt(args[1]); Camera.controlPoints.add(index, location); Camera.broadlog("Point added: " + Camera.prettyLocation(location)); @@ -106,7 +107,7 @@ public class NewPoint implements CommandExecutor { } - public static void set(String[] args, LocationQuaternion location, World world) { + public static void set(String[] args, ExtendedLocation location, World world) { int n = Camera.controlPoints.size(); int index = Integer.parseInt(args[1]); @@ -118,7 +119,7 @@ public class NewPoint implements CommandExecutor { switch (index % 3) { case 0: // Anchor point - LocationQuaternion shift = location.clone().subtract(Camera.controlPoints.get(index)); + ExtendedLocation shift = location.clone().subtract(Camera.controlPoints.get(index)); for (int i = 0; i < 3; i++) { try { setPoint(index + i - 1, Camera.controlPoints.get(index + i - 1).clone().add(shift), world); @@ -131,12 +132,12 @@ public class NewPoint implements CommandExecutor { // Control point after an anchor if (index - 2 >= 0) { // Get anchor-location direction - LocationQuaternion anchor = Camera.controlPoints.get(index - 1); + ExtendedLocation anchor = Camera.controlPoints.get(index - 1); double currentDistance = anchor.distance(location); - LocationQuaternion currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance); + ExtendedLocation currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance); // Get anchor-corresponding distance - LocationQuaternion correspondingControl = Camera.controlPoints.get(index - 2); + ExtendedLocation correspondingControl = Camera.controlPoints.get(index - 2); double correspondingDistance = anchor.distance(correspondingControl); // Keep correcponding at the same distance @@ -150,12 +151,12 @@ public class NewPoint implements CommandExecutor { // Control point before an anchor if (index + 2 < n) { // Get anchor-location direction - LocationQuaternion anchor = Camera.controlPoints.get(index + 1); + ExtendedLocation anchor = Camera.controlPoints.get(index + 1); double currentDistance = anchor.distance(location); - LocationQuaternion currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance); + ExtendedLocation currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance); // Get anchor-corresponding distance - LocationQuaternion correspondingControl = Camera.controlPoints.get(index + 2); + ExtendedLocation correspondingControl = Camera.controlPoints.get(index + 2); double correspondingDistance = anchor.distance(correspondingControl); // Keep correcponding at the same distance diff --git a/src/main/java/com/tocard/cam/ShowCurve.java b/src/main/java/com/tocard/cam/ShowCurve.java index e022b2d..5670f45 100644 --- a/src/main/java/com/tocard/cam/ShowCurve.java +++ b/src/main/java/com/tocard/cam/ShowCurve.java @@ -60,7 +60,7 @@ public class ShowCurve implements CommandExecutor { @Override public void run() { // Get curve iterator - Iterator curveIterator = Camera.curve.iterator(); + Iterator curveIterator = Camera.curve.iterator(); // Draw path while (curveIterator.hasNext()) { @@ -100,7 +100,7 @@ public class ShowCurve implements CommandExecutor { return true; } - public static void add(LocationQuaternion point, World world) { + public static void add(ExtendedLocation point, World world) { ArmorStand as = world.spawn(point, ArmorStand.class); as.setGravity(false); as.setVisible(false); @@ -119,7 +119,7 @@ public class ShowCurve implements CommandExecutor { } @Deprecated - public static void insert(int index, LocationQuaternion point, World world) { + public static void insert(int index, ExtendedLocation point, World world) { ArmorStand as = world.spawn(point, ArmorStand.class); as.setGravity(false); as.setVisible(false); @@ -134,7 +134,7 @@ public class ShowCurve implements CommandExecutor { controlPointsArmorStands.add(index, as); } - public static void set(int index, LocationQuaternion point, World world) { + public static void set(int index, ExtendedLocation point, World world) { controlPointsArmorStands.get(index).setHeadPose(new EulerAngle(point.getPitch() / 180 * Math.PI, 0, 0)); controlPointsArmorStands.get(index).teleport(point); }