feat: rotation fonctionnelles

This commit is contained in:
gdamms 2022-05-17 11:24:17 +02:00
parent 6f31f76ee4
commit e848a242e7
7 changed files with 115 additions and 165 deletions

View file

@ -17,8 +17,8 @@ import org.bukkit.inventory.ItemStack;
public class Camera extends JavaPlugin implements Listener { public class Camera extends JavaPlugin implements Listener {
public static ArrayList<LocationQuaternion> curve = new ArrayList<>(); public static ArrayList<ExtendedLocation> curve = new ArrayList<>();
public static ArrayList<LocationQuaternion> controlPoints = new ArrayList<>(); public static ArrayList<ExtendedLocation> controlPoints = new ArrayList<>();
public static Logger logger; public static Logger logger;
public static Plugin plugin; public static Plugin plugin;
private static float dt = 0.01f; 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() { public static void compute() {
patchYaws();
int nControlPoints = controlPoints.size(); int nControlPoints = controlPoints.size();
curve = new ArrayList<>(); curve = new ArrayList<>();
@ -89,7 +96,7 @@ public class Camera extends JavaPlugin implements Listener {
if (nControlPoints <= 4) { if (nControlPoints <= 4) {
for (float t = 0; t < 1; t += dt) { for (float t = 0; t < 1; t += dt) {
ArrayList<LocationQuaternion> P = new ArrayList<>(controlPoints); ArrayList<ExtendedLocation> P = new ArrayList<>(controlPoints);
int N = P.size(); int N = P.size();
for (int k = N - 1; k > 0; k--) { for (int k = N - 1; k > 0; k--) {
@ -107,7 +114,7 @@ public class Camera extends JavaPlugin implements Listener {
int nBezier = (nControlPoints - 1) / 3; int nBezier = (nControlPoints - 1) / 3;
for (int iBezier = 0; iBezier < nBezier; iBezier++) { for (int iBezier = 0; iBezier < nBezier; iBezier++) {
for (float t = 0; t < 1; t += dt) { for (float t = 0; t < 1; t += dt) {
ArrayList<LocationQuaternion> P = new ArrayList<>(); ArrayList<ExtendedLocation> P = new ArrayList<>();
for (int k = 0; k < 4; k++) { for (int k = 0; k < 4; k++) {
P.add(controlPoints.get(3 * iBezier + k).clone()); P.add(controlPoints.get(3 * iBezier + k).clone());
} }
@ -130,13 +137,14 @@ public class Camera extends JavaPlugin implements Listener {
point.getPitch(), point.getYaw()); point.getPitch(), point.getYaw());
} }
public static String prettyLocation(LocationQuaternion point) { // public static String prettyLocation(ExtendedLocation point) {
return String.format( // 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", // "X=%05.2f, Y=%05.2f, Z=%05.2f, P=%05.2f, Y=%05.2f, R=%05.2f, w=%05.2f,
point.getX(), point.getY(), point.getZ(), // x=%05.2f, y=%05.2f, z=%05.2f",
point.getPitch(), point.getYaw(), point.roll, // point.getX(), point.getY(), point.getZ(),
point.qw, point.qx, point.qy, point.qz); // point.getPitch(), point.getYaw(), point.roll,
} // point.qw, point.qx, point.qy, point.qz);
// }
public static void broadlog(String msg) { public static void broadlog(String msg) {
Camera.logger.log(Level.INFO, msg); Camera.logger.log(Level.INFO, msg);

View file

@ -10,7 +10,7 @@ import java.util.Iterator;
public class ExecuteTraveling implements CommandExecutor { public class ExecuteTraveling implements CommandExecutor {
private static int taskID = -1; private static int taskID = -1;
private static Iterator<LocationQuaternion> curveIterator; private static Iterator<ExtendedLocation> curveIterator;
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
@ -32,8 +32,7 @@ public class ExecuteTraveling implements CommandExecutor {
public void run() { public void run() {
// Teleport to next point // Teleport to next point
if (curveIterator.hasNext()) { if (curveIterator.hasNext()) {
LocationQuaternion loc = curveIterator.next(); ExtendedLocation loc = curveIterator.next();
Camera.broadlog(Camera.prettyLocation(loc));
player.teleport(loc); player.teleport(loc);
return; return;
} }

View file

@ -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;
}
}

View file

@ -8,7 +8,7 @@ import org.bukkit.entity.Player;
public class ListPoints implements CommandExecutor { public class ListPoints implements CommandExecutor {
private static ListIterator<LocationQuaternion> points; private static ListIterator<ExtendedLocation> points;
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

View file

@ -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;
}
}

View file

@ -16,13 +16,13 @@ public class NewPoint implements CommandExecutor {
Player player = (Player) sender; Player player = (Player) sender;
switch (args[0]) { switch (args[0]) {
case "add": case "add":
add(args, new LocationQuaternion(player.getLocation()), player.getWorld()); add(args, new ExtendedLocation(player.getLocation()), player.getWorld());
break; break;
case "rm": case "rm":
rm(args, null, null); rm(args, null, null);
break; break;
case "set": case "set":
set(args, new LocationQuaternion(player.getLocation()), player.getWorld()); set(args, new ExtendedLocation(player.getLocation()), player.getWorld());
break; break;
default: default:
break; break;
@ -38,34 +38,35 @@ public class NewPoint implements CommandExecutor {
return true; return true;
} }
public static void addPoint(LocationQuaternion point, World world) { public static void addPoint(ExtendedLocation point, World world) {
Camera.controlPoints.add(point); Camera.controlPoints.add(point);
ShowCurve.add(point, world); ShowCurve.add(point, world);
Camera.broadlog("Point added: " + Camera.prettyLocation(point)); 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); Camera.controlPoints.set(index, point);
ShowCurve.set(index, point, world); ShowCurve.set(index, point, world);
Camera.broadlog("Point n°" + index + " set: " + Camera.prettyLocation(point)); Camera.broadlog("Point n°" + index + " set: " + Camera.prettyLocation(point));
} }
public static void rmPoint(int index) { public static void rmPoint(int index) {
LocationQuaternion location = Camera.controlPoints.remove(index); ExtendedLocation location = Camera.controlPoints.remove(index);
ShowCurve.rm(index); ShowCurve.rm(index);
Camera.broadlog("Point deleted: " + Camera.prettyLocation(location)); 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(); int n = Camera.controlPoints.size();
if (n < 4) { if (n < 4) {
addPoint(location, world); addPoint(location, world);
} else { } else {
LocationQuaternion P2 = Camera.controlPoints.get(n - 2); ExtendedLocation P2 = Camera.controlPoints.get(n - 2);
LocationQuaternion P3 = Camera.controlPoints.get(n - 1); ExtendedLocation P3 = Camera.controlPoints.get(n - 1);
LocationQuaternion P4 = P3.clone().multiply(2).subtract(P2); location.patchYaw(P3);
LocationQuaternion P5 = location.clone().add(P4).multiply(0.5); ExtendedLocation P4 = P3.clone().multiply(2).subtract(P2);
ExtendedLocation P5 = location.clone().add(P4).multiply(0.5);
addPoint(P4, world); addPoint(P4, world);
addPoint(P5, world); addPoint(P5, world);
@ -74,7 +75,7 @@ public class NewPoint implements CommandExecutor {
} }
@Deprecated @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]); int index = Integer.parseInt(args[1]);
Camera.controlPoints.add(index, location); Camera.controlPoints.add(index, location);
Camera.broadlog("Point added: " + Camera.prettyLocation(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 n = Camera.controlPoints.size();
int index = Integer.parseInt(args[1]); int index = Integer.parseInt(args[1]);
@ -118,7 +119,7 @@ public class NewPoint implements CommandExecutor {
switch (index % 3) { switch (index % 3) {
case 0: case 0:
// Anchor point // 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++) { for (int i = 0; i < 3; i++) {
try { try {
setPoint(index + i - 1, Camera.controlPoints.get(index + i - 1).clone().add(shift), world); 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 // Control point after an anchor
if (index - 2 >= 0) { if (index - 2 >= 0) {
// Get anchor-location direction // Get anchor-location direction
LocationQuaternion anchor = Camera.controlPoints.get(index - 1); ExtendedLocation anchor = Camera.controlPoints.get(index - 1);
double currentDistance = anchor.distance(location); 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 // Get anchor-corresponding distance
LocationQuaternion correspondingControl = Camera.controlPoints.get(index - 2); ExtendedLocation correspondingControl = Camera.controlPoints.get(index - 2);
double correspondingDistance = anchor.distance(correspondingControl); double correspondingDistance = anchor.distance(correspondingControl);
// Keep correcponding at the same distance // Keep correcponding at the same distance
@ -150,12 +151,12 @@ public class NewPoint implements CommandExecutor {
// Control point before an anchor // Control point before an anchor
if (index + 2 < n) { if (index + 2 < n) {
// Get anchor-location direction // Get anchor-location direction
LocationQuaternion anchor = Camera.controlPoints.get(index + 1); ExtendedLocation anchor = Camera.controlPoints.get(index + 1);
double currentDistance = anchor.distance(location); 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 // Get anchor-corresponding distance
LocationQuaternion correspondingControl = Camera.controlPoints.get(index + 2); ExtendedLocation correspondingControl = Camera.controlPoints.get(index + 2);
double correspondingDistance = anchor.distance(correspondingControl); double correspondingDistance = anchor.distance(correspondingControl);
// Keep correcponding at the same distance // Keep correcponding at the same distance

View file

@ -60,7 +60,7 @@ public class ShowCurve implements CommandExecutor {
@Override @Override
public void run() { public void run() {
// Get curve iterator // Get curve iterator
Iterator<LocationQuaternion> curveIterator = Camera.curve.iterator(); Iterator<ExtendedLocation> curveIterator = Camera.curve.iterator();
// Draw path // Draw path
while (curveIterator.hasNext()) { while (curveIterator.hasNext()) {
@ -100,7 +100,7 @@ public class ShowCurve implements CommandExecutor {
return true; 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); ArmorStand as = world.spawn(point, ArmorStand.class);
as.setGravity(false); as.setGravity(false);
as.setVisible(false); as.setVisible(false);
@ -119,7 +119,7 @@ public class ShowCurve implements CommandExecutor {
} }
@Deprecated @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); ArmorStand as = world.spawn(point, ArmorStand.class);
as.setGravity(false); as.setGravity(false);
as.setVisible(false); as.setVisible(false);
@ -134,7 +134,7 @@ public class ShowCurve implements CommandExecutor {
controlPointsArmorStands.add(index, as); 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).setHeadPose(new EulerAngle(point.getPitch() / 180 * Math.PI, 0, 0));
controlPointsArmorStands.get(index).teleport(point); controlPointsArmorStands.get(index).teleport(point);
} }