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 static ArrayList<LocationQuaternion2> curve;
public static List<LocationQuaternion2> controlPoints;
public static ArrayList<LocationQuaternion2> curve = new ArrayList<>();
public static ArrayList<LocationQuaternion2> controlPoints = new ArrayList<>();
public static Logger logger;
public static Plugin plugin;
private static float dt = 0.01f;
@ -48,12 +48,12 @@ public class Camera extends JavaPlugin implements Listener {
public static void reverseQuaternions() {
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() {
reverseQuaternions();
// reverseQuaternions();
int nControlPoints = controlPoints.size();
curve = new ArrayList<>();
@ -106,10 +106,10 @@ public class Camera extends JavaPlugin implements Listener {
}
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",
loc.getX(), loc.getY(), loc.getZ(),
loc.getPitch(), loc.getYaw());
point.getX(), point.getY(), point.getZ(),
point.getPitch(), point.getYaw());
}
@Override

View file

@ -33,14 +33,16 @@ public class ExecuteTraveling implements CommandExecutor {
public void run() {
// Teleport to next point
if (curveIterator.hasNext()) {
player.teleport(curveIterator.next().toLocation());
LocationQuaternion2 loc = curveIterator.next();
Camera.broadlog(Camera.prettyLocation(loc));
player.teleport(loc);
return;
}
// Regenerate traveling and begin
if (ClosePath.closed) {
curveIterator = Camera.curve.iterator();
player.teleport(curveIterator.next().toLocation());
player.teleport(curveIterator.next());
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;
import javax.swing.DefaultBoundedRangeModel;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -28,8 +11,10 @@ public class LocationQuaternion2 extends Location {
private double qx;
private double qy;
private double qz;
private float roll = 0;
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
public LocationQuaternion2(Location location) {
@ -37,8 +22,8 @@ public class LocationQuaternion2 extends Location {
location.getX(),
location.getY(),
location.getZ(),
location.getYaw() * deg2grad,
location.getPitch() * deg2grad);
location.getYaw(),
location.getPitch());
this.updateQuaternionAngles();
}
@ -47,44 +32,105 @@ public class LocationQuaternion2 extends Location {
// roll (x-axis rotation)
double sinr_cosp = 2 * (qw * qx + qy * qz);
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)
double pitch;
double sinp = 2 * (qw * qy - qz * qx);
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 {
this.setPitch((float) Math.asin(sinp));
pitch = Math.asin(sinp);
}
setPitch((float) pitch * grad2deg);
// yaw (z-axis rotation)
double siny_cosp = 2 * (qw * qz + qx * qy);
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() {
double cy = Math.cos(getYaw() * 0.5);
double sy = Math.sin(getYaw() * 0.5);
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);
float yaw = getYaw() * deg2grad;
float pitch = getPitch() * deg2grad;
this.qw = cr * cp * cy + sr * sp * sy;
this.qx = sr * cp * cy - cr * sp * sy;
this.qy = cr * sp * cy + sr * cp * sy;
this.qz = cr * cp * sy - sr * sp * cy;
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 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
public LocationQuaternion2 clone() {
LocationQuaternion2 cloned = (LocationQuaternion2) super.clone();
cloned.qw = this.qw;
cloned.qx = this.qx;
cloned.qy = this.qy;
cloned.qz = this.qz;
LocationQuaternion2 cloned = new LocationQuaternion2(this);
return cloned;
}

View file

@ -67,8 +67,7 @@ public class ShowCurve implements CommandExecutor {
while (curveIterator.hasNext()) {
player.getWorld().spawnParticle(Particle.ELECTRIC_SPARK,
curveIterator.next().clone()
.add(new LocationQuaternion2(new double[] { 0, 1.8, 0, 0, 0, 0, 0 }))
.toLocation(),
.add(0, 1.8, 0),
1,
0, 0, 0, 0);
}
@ -85,9 +84,7 @@ public class ShowCurve implements CommandExecutor {
Camera.controlPoints.get(indMin).clone().multiply(1 - t)
.add(Camera.controlPoints.get(indMax).clone()
.multiply(t))
.add(new LocationQuaternion2(
new double[] { 0, 1.8, 0, 0, 0, 0, 0 }))
.toLocation(),
.add(0, 1.8, 0),
1,
0, 0, 0, 0);
}
@ -105,7 +102,7 @@ public class ShowCurve implements CommandExecutor {
}
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.setVisible(false);
as.setMarker(true);
@ -118,13 +115,13 @@ public class ShowCurve implements CommandExecutor {
else
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);
}
@Deprecated
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.setVisible(false);
as.setMarker(true);
@ -134,13 +131,13 @@ public class ShowCurve implements CommandExecutor {
as.getEquipment().setHelmet(cameraHeadAnchor);
else
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);
}
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).setHeadPose(new EulerAngle(point.getPitch() / 180 * Math.PI, 0, 0));
controlPointsArmorStands.get(index).teleport(point);
}
public static void rm(int index) {