Ca a empiré mais le code est mieux ?

This commit is contained in:
gdamms 2022-05-16 20:46:06 +02:00
parent 85423171cf
commit 9b2f56ac83
4 changed files with 106 additions and 61 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<LocationQuaternion2> curve; public static ArrayList<LocationQuaternion2> curve = new ArrayList<>();
public static List<LocationQuaternion2> controlPoints; public static ArrayList<LocationQuaternion2> 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;
@ -48,12 +48,12 @@ public class Camera extends JavaPlugin implements Listener {
public static void reverseQuaternions() { public static void reverseQuaternions() {
for (int i = 1; i < controlPoints.size(); i++) { for (int i = 1; i < controlPoints.size(); i++) {
controlPoints.get(i).invertQuaternion(controlPoints.get(i - 1)); controlPoints.get(i).patchYaw(controlPoints.get(i - 1));
} }
} }
public static void compute() { public static void compute() {
reverseQuaternions(); // reverseQuaternions();
int nControlPoints = controlPoints.size(); int nControlPoints = controlPoints.size();
curve = new ArrayList<>(); curve = new ArrayList<>();
@ -106,10 +106,10 @@ public class Camera extends JavaPlugin implements Listener {
} }
public static String prettyLocation(LocationQuaternion2 point) { public static String prettyLocation(LocationQuaternion2 point) {
Location loc = point.toLocation(); point.updateEulerAngles();
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(), point.getX(), point.getY(), point.getZ(),
loc.getPitch(), loc.getYaw()); point.getPitch(), point.getYaw());
} }
@Override @Override

View file

@ -33,14 +33,16 @@ 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().toLocation()); LocationQuaternion2 loc = curveIterator.next();
Camera.broadlog(Camera.prettyLocation(loc));
player.teleport(loc);
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().toLocation()); player.teleport(curveIterator.next());
return; return;
} }

View file

@ -1,24 +1,7 @@
// 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;
package com.tocard.cam; package com.tocard.cam;
import javax.swing.DefaultBoundedRangeModel;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
@ -28,8 +11,10 @@ public class LocationQuaternion2 extends Location {
private double qx; private double qx;
private double qy; private double qy;
private double qz; private double qz;
private float roll = 0;
private static final float deg2grad = (float) Math.PI / 180.0f; private static final float deg2grad = (float) Math.PI / 180.0f;
private static final float grad2deg = 180.0f / (float) Math.PI;
// https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Source_code // https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles#Source_code
public LocationQuaternion2(Location location) { public LocationQuaternion2(Location location) {
@ -37,8 +22,8 @@ public class LocationQuaternion2 extends Location {
location.getX(), location.getX(),
location.getY(), location.getY(),
location.getZ(), location.getZ(),
location.getYaw() * deg2grad, location.getYaw(),
location.getPitch() * deg2grad); location.getPitch());
this.updateQuaternionAngles(); this.updateQuaternionAngles();
} }
@ -47,44 +32,105 @@ public class LocationQuaternion2 extends Location {
// roll (x-axis rotation) // roll (x-axis rotation)
double sinr_cosp = 2 * (qw * qx + qy * qz); double sinr_cosp = 2 * (qw * qx + qy * qz);
double cosr_cosp = 1 - 2 * (qx * qx + qy * qy); double cosr_cosp = 1 - 2 * (qx * qx + qy * qy);
this.roll = (float) Math.atan2(sinr_cosp, cosr_cosp); roll = (float) Math.atan2(sinr_cosp, cosr_cosp) * grad2deg;
// pitch (y-axis rotation) // pitch (y-axis rotation)
double pitch;
double sinp = 2 * (qw * qy - qz * qx); double sinp = 2 * (qw * qy - qz * qx);
if (Math.abs(sinp) >= 1) { if (Math.abs(sinp) >= 1) {
this.setPitch((float) Math.copySign(Math.PI / 2, sinp)); // use 90 degrees if out of range pitch = Math.copySign(Math.PI / 2, sinp); // use 90 degrees if out of range
} else { } else {
this.setPitch((float) Math.asin(sinp)); pitch = Math.asin(sinp);
} }
setPitch((float) pitch * grad2deg);
// yaw (z-axis rotation) // yaw (z-axis rotation)
double siny_cosp = 2 * (qw * qz + qx * qy); double siny_cosp = 2 * (qw * qz + qx * qy);
double cosy_cosp = 1 - 2 * (qy * qy + qz * qz); double cosy_cosp = 1 - 2 * (qy * qy + qz * qz);
this.setYaw((float) Math.atan2(siny_cosp, cosy_cosp)); double yaw = Math.atan2(siny_cosp, cosy_cosp);
setYaw((float) yaw * grad2deg);
} }
public void updateQuaternionAngles() { public void updateQuaternionAngles() {
double cy = Math.cos(getYaw() * 0.5); float yaw = getYaw() * deg2grad;
double sy = Math.sin(getYaw() * 0.5); float pitch = getPitch() * deg2grad;
double cp = Math.cos(getPitch() * 0.5);
double sp = Math.sin(getPitch() * 0.5);
double cr = Math.cos(0 * 0.5);
double sr = Math.sin(0 * 0.5);
this.qw = cr * cp * cy + sr * sp * sy; double cy = Math.cos(yaw * 0.5);
this.qx = sr * cp * cy - cr * sp * sy; double sy = Math.sin(yaw * 0.5);
this.qy = cr * sp * cy + sr * cp * sy; double cp = Math.cos(pitch * 0.5);
this.qz = cr * cp * sy - sr * sp * cy; 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 LocationQuaternion2 patchYaw(LocationQuaternion2 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 LocationQuaternion2 add(LocationQuaternion2 loc) {
super.add(loc);
qw += loc.qw;
qx += loc.qx;
qy += loc.qy;
qz += loc.qz;
updateEulerAngles();
return this;
}
public LocationQuaternion2 subtract(LocationQuaternion2 loc) {
super.subtract(loc);
qw -= loc.qw;
qx -= loc.qx;
qy -= loc.qy;
qz -= loc.qz;
updateEulerAngles();
return this;
}
public LocationQuaternion2 multiply(double m) {
super.multiply(m);
qw *= m;
qx += m;
qy += m;
qz += m;
updateEulerAngles();
return this;
} }
@Override @Override
public LocationQuaternion2 clone() { public LocationQuaternion2 clone() {
LocationQuaternion2 cloned = (LocationQuaternion2) super.clone(); LocationQuaternion2 cloned = new LocationQuaternion2(this);
cloned.qw = this.qw;
cloned.qx = this.qx;
cloned.qy = this.qy;
cloned.qz = this.qz;
return cloned; return cloned;
} }

View file

@ -67,8 +67,7 @@ public class ShowCurve implements CommandExecutor {
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 LocationQuaternion2(new double[] { 0, 1.8, 0, 0, 0, 0, 0 })) .add(0, 1.8, 0),
.toLocation(),
1, 1,
0, 0, 0, 0); 0, 0, 0, 0);
} }
@ -85,9 +84,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 LocationQuaternion2( .add(0, 1.8, 0),
new double[] { 0, 1.8, 0, 0, 0, 0, 0 }))
.toLocation(),
1, 1,
0, 0, 0, 0); 0, 0, 0, 0);
} }
@ -105,7 +102,7 @@ public class ShowCurve implements CommandExecutor {
} }
public static void add(LocationQuaternion2 point, World world) { public static void add(LocationQuaternion2 point, World world) {
ArmorStand as = world.spawn(point.toLocation(), ArmorStand.class); ArmorStand as = world.spawn(point, ArmorStand.class);
as.setGravity(false); as.setGravity(false);
as.setVisible(false); as.setVisible(false);
as.setMarker(true); as.setMarker(true);
@ -118,13 +115,13 @@ 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)); as.setHeadPose(new EulerAngle(point.getPitch() / 180 * Math.PI, 0, 0));
controlPointsArmorStands.add(as); controlPointsArmorStands.add(as);
} }
@Deprecated @Deprecated
public static void insert(int index, LocationQuaternion2 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, ArmorStand.class);
as.setGravity(false); as.setGravity(false);
as.setVisible(false); as.setVisible(false);
as.setMarker(true); as.setMarker(true);
@ -134,13 +131,13 @@ 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)); as.setHeadPose(new EulerAngle(point.getPitch() / 180 * Math.PI, 0, 0));
controlPointsArmorStands.add(index, as); controlPointsArmorStands.add(index, as);
} }
public static void set(int index, LocationQuaternion2 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).setHeadPose(new EulerAngle(point.getPitch() / 180 * Math.PI, 0, 0));
controlPointsArmorStands.get(index).teleport(point.toLocation()); controlPointsArmorStands.get(index).teleport(point);
} }
public static void rm(int index) { public static void rm(int index) {