fix: a moitié les rotation baybled

Co-authored-by: Laureηt <Laurent2916@users.noreply.github.com>
This commit is contained in:
gdamms 2022-05-16 18:22:40 +02:00
parent f8b608eb0b
commit a36a5be8fb
7 changed files with 190 additions and 38 deletions

View file

@ -18,8 +18,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; public static ArrayList<LocationQuaternion2> curve;
public static List<LocationQuaternion> controlPoints; public static List<LocationQuaternion2> controlPoints;
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;
@ -46,7 +46,15 @@ public class Camera extends JavaPlugin implements Listener {
getServer().getPluginManager().registerEvents(this, this); getServer().getPluginManager().registerEvents(this, this);
} }
public static void reverseQuaternions() {
for (int i = 1; i < controlPoints.size(); i++) {
controlPoints.get(i).invertQuaternion(controlPoints.get(i - 1));
}
}
public static void compute() { public static void compute() {
reverseQuaternions();
int nControlPoints = controlPoints.size(); int nControlPoints = controlPoints.size();
curve = new ArrayList<>(); curve = new ArrayList<>();
@ -56,7 +64,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<LocationQuaternion2> 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--) {
@ -74,7 +82,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<LocationQuaternion2> 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());
} }
@ -97,7 +105,7 @@ public class Camera extends JavaPlugin implements Listener {
Bukkit.broadcastMessage(msg); Bukkit.broadcastMessage(msg);
} }
public static String prettyLocation(LocationQuaternion point) { public static String prettyLocation(LocationQuaternion2 point) {
Location loc = point.toLocation(); Location loc = point.toLocation();
return String.format("X=%05.2f, Y=%05.2f, Z=%05.2f, P=%05.2f, Y=%05.2f", return String.format("X=%05.2f, Y=%05.2f, Z=%05.2f, P=%05.2f, Y=%05.2f",
loc.getX(), loc.getY(), loc.getZ(), loc.getX(), loc.getY(), loc.getZ(),

View file

@ -11,7 +11,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<Location> curveIterator; private static Iterator<LocationQuaternion2> 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) {
@ -33,14 +33,14 @@ 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()) {
player.teleport(curveIterator.next()); player.teleport(curveIterator.next().toLocation());
return; return;
} }
// Regenerate traveling and begin // Regenerate traveling and begin
if (ClosePath.closed) { if (ClosePath.closed) {
curveIterator = Camera.curve.iterator(); curveIterator = Camera.curve.iterator();
player.teleport(curveIterator.next()); player.teleport(curveIterator.next().toLocation());
return; return;
} }

View file

@ -9,7 +9,7 @@ import org.bukkit.entity.Player;
public class ListPoints implements CommandExecutor { public class ListPoints implements CommandExecutor {
private static ListIterator<Location> points; private static ListIterator<LocationQuaternion2> 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

@ -11,8 +11,8 @@ public class LocationQuaternion {
coordinates[1] = location.getY(); coordinates[1] = location.getY();
coordinates[2] = location.getZ(); coordinates[2] = location.getZ();
double yaw = location.getYaw(); double yaw = location.getYaw() * Math.PI / 180;
double pitch = location.getPitch(); double pitch = location.getPitch() * Math.PI / 180;
double roll = 0; double roll = 0;
double cy = Math.cos(yaw * 0.5); double cy = Math.cos(yaw * 0.5);
@ -41,6 +41,13 @@ public class LocationQuaternion {
return this; return this;
} }
public LocationQuaternion subtract(LocationQuaternion lq) {
for (int i = 0; i < coordinates.length; i++) {
coordinates[i] -= lq.coordinates[i];
}
return this;
}
public LocationQuaternion add(Location l) { public LocationQuaternion add(Location l) {
LocationQuaternion lq = new LocationQuaternion(l); LocationQuaternion lq = new LocationQuaternion(l);
for (int i = 0; i < coordinates.length; i++) { for (int i = 0; i < coordinates.length; i++) {
@ -56,6 +63,11 @@ public class LocationQuaternion {
return this; return this;
} }
public double distance(LocationQuaternion loc) {
return Math.sqrt(
coordinates[0] * coordinates[0] + coordinates[1] * coordinates[1] + coordinates[2] * coordinates[2]);
}
public Location toLocation() { public Location toLocation() {
// roll (x-axis rotation) // roll (x-axis rotation)
// double sinr_cosp = 2 * (coordinates[3] * coordinates[4] + coordinates[5] * // double sinr_cosp = 2 * (coordinates[3] * coordinates[4] + coordinates[5] *
@ -80,7 +92,46 @@ public class LocationQuaternion {
return new Location( return new Location(
Bukkit.getWorlds().get(0), Bukkit.getWorlds().get(0),
coordinates[0], coordinates[1], coordinates[2], coordinates[0], coordinates[1], coordinates[2],
(float) yaw, (float) pitch); (float) (yaw * 180 / Math.PI), (float) (pitch * 180 / Math.PI));
}
public Location toLocation(double previousYaw) {
Location loc = toLocation();
double A = previousYaw;
double B = loc.getYaw();
double b = B % 360;
int wholePart = 360 * (int) (A / 360);
double minDist = -1;
double sol = 0;
for (int i = -1; i < 2; i++) {
double val = wholePart + b + i * 360;
double dist = Math.abs(val - A);
if (minDist == -1 || dist < minDist) {
sol = val;
minDist = dist;
}
}
Camera.broadlog("From " + coordinates[3] + " to " + sol);
loc.setYaw((float) sol);
return loc;
}
public double dot(LocationQuaternion loc) {
double sum = 0;
for (int i = 3; i < 7; i++) {
sum += coordinates[i] + loc.coordinates[i];
}
return sum;
}
public LocationQuaternion invertQuaternion(LocationQuaternion previousLocationQuaternion) {
if (dot(previousLocationQuaternion) < 0)
for (int i = 3; i < 7; i++) {
coordinates[i] *= -1;
}
return this;
} }
@Override @Override

View file

@ -0,0 +1,89 @@
package com.tocard.cam;
import org.bukkit.Bukkit;
import org.bukkit.Location;
public class LocationQuaternion2 {
public double[] coordinates = new double[5];
public LocationQuaternion2(Location location) {
coordinates[0] = location.getX();
coordinates[1] = location.getY();
coordinates[2] = location.getZ();
coordinates[3] = location.getYaw();
coordinates[4] = location.getPitch();
}
public LocationQuaternion2(double[] coords) {
for (int i = 0; i < coordinates.length; i++) {
coordinates[i] = coords[i];
}
}
public LocationQuaternion2 add(LocationQuaternion2 lq) {
for (int i = 0; i < coordinates.length; i++) {
coordinates[i] += lq.coordinates[i];
}
return this;
}
public LocationQuaternion2 subtract(LocationQuaternion2 lq) {
for (int i = 0; i < coordinates.length; i++) {
coordinates[i] -= lq.coordinates[i];
}
return this;
}
public LocationQuaternion2 add(Location l) {
LocationQuaternion2 lq = new LocationQuaternion2(l);
for (int i = 0; i < coordinates.length; i++) {
coordinates[i] += lq.coordinates[i];
}
return this;
}
public LocationQuaternion2 multiply(double m) {
for (int i = 0; i < coordinates.length; i++) {
coordinates[i] *= m;
}
return this;
}
public double distance(LocationQuaternion2 loc) {
return Math.sqrt(
coordinates[0] * coordinates[0] + coordinates[1] * coordinates[1] + coordinates[2] * coordinates[2]);
}
public Location toLocation() {
return new Location(
Bukkit.getWorlds().get(0),
coordinates[0], coordinates[1], coordinates[2],
(float) (coordinates[3]), (float) (coordinates[4]));
}
public LocationQuaternion2 invertQuaternion(LocationQuaternion2 previousLocationQuaternion) {
double A = previousLocationQuaternion.coordinates[3];
double B = coordinates[3];
double b = B % 360;
int wholePart = 360 * (int) (A / 360);
double minDist = -1;
double sol = 0;
for (int i = -1; i < 2; i++) {
double val = wholePart + b + i * 360;
double dist = Math.abs(val - A);
if (minDist == -1 || dist < minDist) {
sol = val;
minDist = dist;
}
}
Camera.broadlog("From " + coordinates[3] + " to " + sol);
coordinates[3] = sol;
return this;
}
@Override
public LocationQuaternion2 clone() {
return new LocationQuaternion2(coordinates);
}
}

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, player.getLocation(), player.getWorld()); add(args, new LocationQuaternion2(player.getLocation()), player.getWorld());
break; break;
case "rm": case "rm":
rm(args, player.getLocation(), player.getWorld()); rm(args, null, null);
break; break;
case "set": case "set":
set(args, player.getLocation(), player.getWorld()); set(args, new LocationQuaternion2(player.getLocation()), player.getWorld());
break; break;
default: default:
break; break;
@ -38,34 +38,34 @@ public class NewPoint implements CommandExecutor {
return true; return true;
} }
public static void addPoint(Location point, World world) { public static void addPoint(LocationQuaternion2 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, Location point, World world) { public static void setPoint(int index, LocationQuaternion2 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) {
Location location = Camera.controlPoints.remove(index); LocationQuaternion2 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, Location location, World world) { public static void add(String[] args, LocationQuaternion2 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 {
Location P2 = Camera.controlPoints.get(n - 2); LocationQuaternion2 P2 = Camera.controlPoints.get(n - 2);
Location P3 = Camera.controlPoints.get(n - 1); LocationQuaternion2 P3 = Camera.controlPoints.get(n - 1);
Location P4 = P3.clone().multiply(2).subtract(P2); LocationQuaternion2 P4 = P3.clone().multiply(2).subtract(P2);
Location P5 = location.clone().add(P4).multiply(0.5); LocationQuaternion2 P5 = location.clone().add(P4).multiply(0.5);
addPoint(P4, world); addPoint(P4, world);
addPoint(P5, world); addPoint(P5, world);
@ -74,7 +74,7 @@ public class NewPoint implements CommandExecutor {
} }
@Deprecated @Deprecated
public static void ins(String[] args, Location location, World world) { public static void ins(String[] args, LocationQuaternion2 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 +106,7 @@ public class NewPoint implements CommandExecutor {
} }
public static void set(String[] args, Location location, World world) { public static void set(String[] args, LocationQuaternion2 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 +118,7 @@ public class NewPoint implements CommandExecutor {
switch (index % 3) { switch (index % 3) {
case 0: case 0:
// Anchor point // Anchor point
Location shift = location.clone().subtract(Camera.controlPoints.get(index)); LocationQuaternion2 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 +131,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
Location anchor = Camera.controlPoints.get(index - 1); LocationQuaternion2 anchor = Camera.controlPoints.get(index - 1);
double currentDistance = anchor.distance(location); double currentDistance = anchor.distance(location);
Location currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance); LocationQuaternion2 currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance);
// Get anchor-corresponding distance // Get anchor-corresponding distance
Location correspondingControl = Camera.controlPoints.get(index - 2); LocationQuaternion2 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 +150,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
Location anchor = Camera.controlPoints.get(index + 1); LocationQuaternion2 anchor = Camera.controlPoints.get(index + 1);
double currentDistance = anchor.distance(location); double currentDistance = anchor.distance(location);
Location currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance); LocationQuaternion2 currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance);
// Get anchor-corresponding distance // Get anchor-corresponding distance
Location correspondingControl = Camera.controlPoints.get(index + 2); LocationQuaternion2 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

@ -7,6 +7,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.EulerAngle;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import java.util.ArrayList; import java.util.ArrayList;
@ -60,13 +61,13 @@ 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<LocationQuaternion2> curveIterator = Camera.curve.iterator();
// Draw path // Draw path
while (curveIterator.hasNext()) { while (curveIterator.hasNext()) {
player.getWorld().spawnParticle(Particle.ELECTRIC_SPARK, player.getWorld().spawnParticle(Particle.ELECTRIC_SPARK,
curveIterator.next().clone() curveIterator.next().clone()
.add(new LocationQuaternion(new double[] { 0, 1.8, 0, 0, 0, 0, 0 })) .add(new LocationQuaternion2(new double[] { 0, 1.8, 0, 0, 0, 0, 0 }))
.toLocation(), .toLocation(),
1, 1,
0, 0, 0, 0); 0, 0, 0, 0);
@ -84,7 +85,7 @@ public class ShowCurve implements CommandExecutor {
Camera.controlPoints.get(indMin).clone().multiply(1 - t) Camera.controlPoints.get(indMin).clone().multiply(1 - t)
.add(Camera.controlPoints.get(indMax).clone() .add(Camera.controlPoints.get(indMax).clone()
.multiply(t)) .multiply(t))
.add(new LocationQuaternion( .add(new LocationQuaternion2(
new double[] { 0, 1.8, 0, 0, 0, 0, 0 })) new double[] { 0, 1.8, 0, 0, 0, 0, 0 }))
.toLocation(), .toLocation(),
1, 1,
@ -103,7 +104,7 @@ public class ShowCurve implements CommandExecutor {
return true; return true;
} }
public static void add(LocationQuaternion point, World world) { public static void add(LocationQuaternion2 point, World world) {
ArmorStand as = world.spawn(point.toLocation(), ArmorStand.class); ArmorStand as = world.spawn(point.toLocation(), ArmorStand.class);
as.setGravity(false); as.setGravity(false);
as.setVisible(false); as.setVisible(false);
@ -117,11 +118,12 @@ public class ShowCurve implements CommandExecutor {
else else
as.getEquipment().setHelmet(cameraHeadControl); as.getEquipment().setHelmet(cameraHeadControl);
} }
as.setHeadPose(new EulerAngle(point.coordinates[4] / 180 * Math.PI, 0, 0));
controlPointsArmorStands.add(as); controlPointsArmorStands.add(as);
} }
@Deprecated @Deprecated
public static void insert(int index, LocationQuaternion point, World world) { public static void insert(int index, LocationQuaternion2 point, World world) {
ArmorStand as = world.spawn(point.toLocation(), ArmorStand.class); ArmorStand as = world.spawn(point.toLocation(), ArmorStand.class);
as.setGravity(false); as.setGravity(false);
as.setVisible(false); as.setVisible(false);
@ -132,10 +134,12 @@ public class ShowCurve implements CommandExecutor {
as.getEquipment().setHelmet(cameraHeadAnchor); as.getEquipment().setHelmet(cameraHeadAnchor);
else else
as.getEquipment().setHelmet(cameraHeadControl); as.getEquipment().setHelmet(cameraHeadControl);
as.setHeadPose(new EulerAngle(point.coordinates[4] / 180 * Math.PI, 0, 0));
controlPointsArmorStands.add(index, as); controlPointsArmorStands.add(index, as);
} }
public static void set(int index, LocationQuaternion point, World world) { public static void set(int index, LocationQuaternion2 point, World world) {
controlPointsArmorStands.get(index).setHeadPose(new EulerAngle(point.coordinates[4] / 180 * Math.PI, 0, 0));
controlPointsArmorStands.get(index).teleport(point.toLocation()); controlPointsArmorStands.get(index).teleport(point.toLocation());
} }