Skip to content

Commit

Permalink
replace deprecated math classes with joml
Browse files Browse the repository at this point in the history
remove client Utils
cleanup and prefer common Utils
  • Loading branch information
NicBOMB committed Nov 12, 2023
1 parent 52eb124 commit 9311bec
Show file tree
Hide file tree
Showing 58 changed files with 1,435 additions and 5,013 deletions.
49 changes: 24 additions & 25 deletions common/src/main/java/org/vivecraft/client/VRPlayersClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.vivecraft.client.utils.Utils;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.vivecraft.client_vr.ClientDataHolderVR;
import org.vivecraft.client_vr.VRData;
import org.vivecraft.common.network.VrPlayerState;
import org.vivecraft.common.utils.math.Quaternion;
import org.vivecraft.common.utils.math.Vector3;
import org.vivecraft.common.utils.Utils;

import java.util.*;
import java.util.Map.Entry;
Expand Down Expand Up @@ -54,10 +55,9 @@ public void disableVR(UUID player) {

public void Update(UUID uuid, VrPlayerState vrPlayerState, float worldScale, float heightScale, boolean localPlayer) {
if (localPlayer || !this.mc.player.getUUID().equals(uuid)) {
Vector3 forward = new Vector3(0.0F, 0.0F, -1.0F);
Vector3 hmdDir = vrPlayerState.hmd().orientation().multiply(forward);
Vector3 controller0Dir = vrPlayerState.controller0().orientation().multiply(forward);
Vector3 controller1Dir = vrPlayerState.controller1().orientation().multiply(forward);
Vector3f hmdDir = vrPlayerState.hmd().orientation().transformUnit(Utils.forward, new Vector3f());
Vector3f controller0Dir = vrPlayerState.controller0().orientation().transformUnit(Utils.forward, new Vector3f());
Vector3f controller1Dir = vrPlayerState.controller1().orientation().transformUnit(Utils.forward, new Vector3f());
var rotInfo = new RotInfo();
rotInfo.reverse = vrPlayerState.reverseHands();
rotInfo.seated = vrPlayerState.seated();
Expand All @@ -66,9 +66,9 @@ public void Update(UUID uuid, VrPlayerState vrPlayerState, float worldScale, flo
rotInfo.hmd = this.donors.get(uuid);
}

rotInfo.leftArmRot = new Vec3(controller1Dir.getX(), controller1Dir.getY(), controller1Dir.getZ());
rotInfo.rightArmRot = new Vec3(controller0Dir.getX(), controller0Dir.getY(), controller0Dir.getZ());
rotInfo.headRot = new Vec3(hmdDir.getX(), hmdDir.getY(), hmdDir.getZ());
rotInfo.leftArmRot = new Vec3(controller1Dir.x(), controller1Dir.y(), controller1Dir.z());
rotInfo.rightArmRot = new Vec3(controller0Dir.x(), controller0Dir.y(), controller0Dir.z());
rotInfo.headRot = new Vec3(hmdDir.x(), hmdDir.y(), hmdDir.z());
rotInfo.Headpos = vrPlayerState.hmd().position();
rotInfo.leftArmPos = vrPlayerState.controller1().position();
rotInfo.rightArmPos = vrPlayerState.controller0().position();
Expand Down Expand Up @@ -184,10 +184,10 @@ public RotInfo getRotationsForPlayer(UUID uuid) {
playermodelcontroller$rotinfo2.leftArmQuat = playermodelcontroller$rotinfo.leftArmQuat;
playermodelcontroller$rotinfo2.rightArmQuat = playermodelcontroller$rotinfo.rightArmQuat;
playermodelcontroller$rotinfo2.headQuat = playermodelcontroller$rotinfo.headQuat;
Vector3 vector3 = new Vector3(0.0F, 0.0F, -1.0F);
playermodelcontroller$rotinfo2.leftArmRot = Utils.vecLerp(playermodelcontroller$rotinfo1.leftArmRot, Utils.convertToVector3d(playermodelcontroller$rotinfo2.leftArmQuat.multiply(vector3)), f);
playermodelcontroller$rotinfo2.rightArmRot = Utils.vecLerp(playermodelcontroller$rotinfo1.rightArmRot, Utils.convertToVector3d(playermodelcontroller$rotinfo2.rightArmQuat.multiply(vector3)), f);
playermodelcontroller$rotinfo2.headRot = Utils.vecLerp(playermodelcontroller$rotinfo1.headRot, Utils.convertToVector3d(playermodelcontroller$rotinfo2.headQuat.multiply(vector3)), f);
Vector3f vector3 = new Vector3f(0.0F, 0.0F, -1.0F);
playermodelcontroller$rotinfo2.leftArmRot = Utils.vecLerp(playermodelcontroller$rotinfo1.leftArmRot, Utils.convertToVec3(playermodelcontroller$rotinfo2.leftArmQuat.transformUnit(vector3, new Vector3f())), f);
playermodelcontroller$rotinfo2.rightArmRot = Utils.vecLerp(playermodelcontroller$rotinfo1.rightArmRot, Utils.convertToVec3(playermodelcontroller$rotinfo2.rightArmQuat.transformUnit(vector3, new Vector3f())), f);
playermodelcontroller$rotinfo2.headRot = Utils.vecLerp(playermodelcontroller$rotinfo1.headRot, Utils.convertToVec3(playermodelcontroller$rotinfo2.headQuat.transformUnit(vector3, new Vector3f())), f);
playermodelcontroller$rotinfo2.heightScale = playermodelcontroller$rotinfo.heightScale;
playermodelcontroller$rotinfo2.worldScale = playermodelcontroller$rotinfo.worldScale;
return playermodelcontroller$rotinfo2;
Expand All @@ -198,12 +198,9 @@ public RotInfo getRotationsForPlayer(UUID uuid) {

public static RotInfo getMainPlayerRotInfo(VRData data) {
RotInfo playermodelcontroller$rotinfo = new RotInfo();
Quaternion quaternion = new Quaternion(data.getController(1).getMatrix());
Quaternion quaternion1 = new Quaternion(data.getController(0).getMatrix());
Quaternion quaternion2 = new Quaternion(data.hmd.getMatrix());
playermodelcontroller$rotinfo.headQuat = quaternion2;
playermodelcontroller$rotinfo.leftArmQuat = quaternion;
playermodelcontroller$rotinfo.rightArmQuat = quaternion1;
playermodelcontroller$rotinfo.headQuat = new Quaternionf().setFromNormalized(data.hmd.getMatrix(new Matrix4f())).invert();
playermodelcontroller$rotinfo.leftArmQuat = new Quaternionf().setFromNormalized(data.getController(1).getMatrix(new Matrix4f())).invert();
playermodelcontroller$rotinfo.rightArmQuat = new Quaternionf().setFromNormalized(data.getController(0).getMatrix(new Matrix4f())).invert();
playermodelcontroller$rotinfo.seated = ClientDataHolderVR.getInstance().vrSettings.seated;
playermodelcontroller$rotinfo.leftArmPos = data.getController(1).getPosition();
playermodelcontroller$rotinfo.rightArmPos = data.getController(0).getPosition();
Expand All @@ -221,18 +218,20 @@ public static float getFacingYaw(RotInfo rotInfo) {
return (float) Math.toDegrees(Math.atan2(vec3.x, vec3.z));
}

public static Vec3 getOrientVec(Quaternion quat) {
Vec3 vec3 = quat.multiply(new Vec3(0.0D, 0.0D, -1.0D)).cross(quat.multiply(new Vec3(0.0D, 1.0D, 0.0D))).normalize();
public static Vec3 getOrientVec(Quaternionf quat) {
Vec3 vec = new Vec3(0.0D, 1.0D, 0.0D);
Vec3 vec1 = new Vec3(0.0D, 0.0D, -1.0D);
Vec3 vec3 = Utils.convertToVec3(quat.transformUnit(new Vector3f((float) vec1.x, (float) vec1.y, (float) vec1.z), new Vector3f())).cross(Utils.convertToVec3(quat.transform(new Vector3f((float) vec.x, (float) vec.y, (float) vec.z), new Vector3f()))).normalize();
return (new Vec3(0.0D, 1.0D, 0.0D)).cross(vec3).normalize();
}

public static class RotInfo {
public boolean seated;
public boolean reverse;
public int hmd = 0;
public Quaternion leftArmQuat;
public Quaternion rightArmQuat;
public Quaternion headQuat;
public Quaternionf leftArmQuat;
public Quaternionf rightArmQuat;
public Quaternionf headQuat;
public Vec3 leftArmRot;
public Vec3 rightArmRot;
public Vec3 headRot;
Expand Down
48 changes: 33 additions & 15 deletions common/src/main/java/org/vivecraft/client/utils/Debug.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,36 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.vivecraft.common.utils.math.Quaternion;
import org.vivecraft.common.utils.math.Vector3;
import org.joml.Math;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.vivecraft.common.utils.Utils;

import java.awt.*;
import java.util.ArrayList;

import static java.lang.Math.pow;

public class Debug {
Vec3 root;
Quaternion rotation;
Quaternionf rotation;
public static boolean isEnabled = true;
static Polygon cross = new Polygon(6);
static Polygon arrowHead = new Polygon(8);
private static final DebugRendererManual renderer = new DebugRendererManual();

public Debug(Vec3 root) {
this.root = root;
this.rotation = new Quaternion();
this.rotation = new Quaternionf();
}

public Debug(Vec3 root, Quaternion rotation) {
public Debug(Vec3 root, Quaternionf rotation) {
this.root = root;
this.rotation = rotation;
}

public void drawPoint(Vec3 point, Color color) {
point = this.rotation.multiply(point);
point = Utils.convertToVec3(this.rotation.transformUnit(new Vector3f((float) point.x, (float) point.y, (float) point.z), new Vector3f()));
Vec3 vec3 = this.root.add(point);
Polygon debug$polygon = cross.offset(vec3);

Expand All @@ -50,13 +54,27 @@ public void drawPoint(Vec3 point, Color color) {

public void drawVector(Vec3 start, Vec3 direction, Color color) {
Polygon debug$polygon = new Polygon(2);
start = this.rotation.multiply(start);
direction = this.rotation.multiply(direction);
start = Utils.convertToVec3(this.rotation.transformUnit(new Vector3f((float) start.x, (float) start.y, (float) start.z), new Vector3f()));
direction = Utils.convertToVec3(this.rotation.transformUnit(new Vector3f((float) direction.x, (float) direction.y, (float) direction.z), new Vector3f()));
debug$polygon.vertices[0] = this.root.add(start);
debug$polygon.colors[0] = new Color(0, 0, 0, 0);
debug$polygon.vertices[1] = this.root.add(start).add(direction);
debug$polygon.colors[1] = color;
Quaternion quaternion = Quaternion.createFromToVector(new Vector3(0.0F, 1.0F, 0.0F), new Vector3(direction.normalize()));
Vec3 normalized = direction.normalize();
Vector3f to = new Vector3f((float) normalized.x(), (float) normalized.y(), (float) normalized.z());
Vector3f vector3 = Utils.up.cross(to, new Vector3f());
float f = (float) (Math.sqrt(pow(Utils.up.length(), 2.0D) * pow(to.length(), 2.0D)) + (double) Utils.up.dot(to));
Quaternionf quaternion1 = (new Quaternionf(vector3.x, vector3.y, vector3.z, f));
Quaternionf dest = new Quaternionf();
float f4 = Math.sqrt(quaternion1.lengthSquared());

if (f4 > 0.0F) {
dest.normalize();
} else {
dest.identity();
}

Quaternionf quaternion = dest;
Polygon debug$polygon1 = arrowHead.rotated(quaternion).offset(this.root.add(start).add(direction));

for (int i = 0; i < debug$polygon1.colors.length; ++i) {
Expand All @@ -70,8 +88,8 @@ public void drawVector(Vec3 start, Vec3 direction, Color color) {
}

public void drawLine(Vec3 start, Vec3 end, Color color) {
start = this.rotation.multiply(start);
end = this.rotation.multiply(end);
start = Utils.convertToVec3(this.rotation.transformUnit(new Vector3f((float) start.x, (float) start.y, (float) start.z), new Vector3f()));
end = Utils.convertToVec3(this.rotation.transformUnit(new Vector3f((float) end.x, (float) end.y, (float) end.z), new Vector3f()));
Polygon debug$polygon = new Polygon(2);
debug$polygon.vertices[0] = this.root.add(start);
debug$polygon.colors[0] = new Color(0, 0, 0, 0);
Expand All @@ -95,8 +113,8 @@ public void drawBoundingBox(AABB box, Color color) {
avec31[3] = new Vec3(box.maxX, box.maxY, box.minZ);

for (int j = 0; j < 4; ++j) {
avec3[j] = this.root.add(this.rotation.multiply(avec3[j]));
avec31[j] = this.root.add(this.rotation.multiply(avec31[j]));
avec3[j] = this.root.add(Utils.convertToVec3(this.rotation.transformUnit(new Vector3f((float) avec3[j].x, (float) avec3[j].y, (float) avec3[j].z), new Vector3f())));
avec31[j] = this.root.add(Utils.convertToVec3(this.rotation.transformUnit(new Vector3f((float) avec31[j].x, (float) avec31[j].y, (float) avec31[j].z), new Vector3f())));
}

for (int k = 0; k < 5; ++k) {
Expand Down Expand Up @@ -221,11 +239,11 @@ public Polygon offset(Vec3 offset) {
return debug$polygon;
}

public Polygon rotated(Quaternion quat) {
public Polygon rotated(Quaternionf quat) {
Polygon debug$polygon = new Polygon(this.vertices.length);

for (int i = 0; i < this.vertices.length; ++i) {
debug$polygon.vertices[i] = quat.multiply(new Vector3(this.vertices[i])).toVector3d();
debug$polygon.vertices[i] = Utils.convertToVec3(quat.transformUnit(new Vector3f((float) this.vertices[i].x, (float) this.vertices[i].y, (float) this.vertices[i].z), new Vector3f()));
debug$polygon.colors[i] = this.colors[i];
}

Expand Down
Loading

0 comments on commit 9311bec

Please sign in to comment.