Skip to content

Commit

Permalink
Merge pull request #2 from JPAKx4/master
Browse files Browse the repository at this point in the history
Added utility methods + documentation to Rollable. Added a smoothing …
  • Loading branch information
landon-wills authored Oct 10, 2024
2 parents b3db00f + 1c74b95 commit 9fc21d4
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 27 deletions.
47 changes: 47 additions & 0 deletions src/client/java/ca/landonjw/math/Smoother.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ca.landonjw.math;

import java.util.LinkedList;
import java.util.List;

public class Smoother {
private final float timeWindow;
private final List<Entry> values = new LinkedList<>();

public Smoother(float timeWindow) {
this.timeWindow = timeWindow;
}

public float smooth(float value, float deltaValue){
values.add(0, new Entry(value, deltaValue));
float totalTime = 0;
float sum = 0;
int index = 0;

while(index < values.size() && totalTime <= timeWindow){
var entry = values.get(index);
totalTime += entry.deltaTime;
sum += entry.value * entry.deltaTime;
index++;
}

while(index < values.size()){
values.remove(index);
}

if(totalTime <= 0){
return value;
}

return sum/totalTime;
}

private static class Entry {
float deltaTime;
float value;

private Entry(float value, float deltaTime){
this.deltaTime = deltaTime;
this.value = value;
}
}
}
10 changes: 4 additions & 6 deletions src/client/java/ca/landonjw/mixin/client/CameraMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import net.minecraft.world.entity.Entity;
import org.joml.Matrix3f;
import org.joml.Quaternionf;
import org.joml.Quaternionfc;
import org.joml.Vector3f;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -17,7 +16,6 @@

import java.util.function.Supplier;

import static com.mojang.text2speech.Narrator.LOGGER;

@Mixin(Camera.class)
public class CameraMixin implements Rollable {
Expand All @@ -29,7 +27,7 @@ public class CameraMixin implements Rollable {
@Shadow @Final private Vector3f left;
@Shadow private float xRot;
@Shadow private float yRot;
@Unique Matrix3f orientation = new Matrix3f();
@Unique Matrix3f orientation = new Matrix3f();

@Override
public Matrix3f getOrientation() {
Expand All @@ -47,9 +45,9 @@ public void updateOrientation(Supplier<Matrix3f> update) {
this.orientation = rollable.getOrientation();

this.rotation.set(this.orientation.normal(new Matrix3f()).getNormalizedRotation(new Quaternionf()));
this.forwards.set(this.orientation.transform(new Vector3f(0, 0, 1)));
this.up.set(this.orientation.transform(new Vector3f(0, 1, 0)));
this.left.set(this.orientation.transform(new Vector3f(1, 0, 0)));
this.forwards.set(getForwardVector());
this.up.set(getUpVector());
this.left.set(getLeftVector());

ci.cancel();
}
Expand Down
18 changes: 11 additions & 7 deletions src/client/java/ca/landonjw/mixin/client/MouseHandlerMixin.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package ca.landonjw.mixin.client;

import ca.landonjw.Rollable;
import ca.landonjw.math.Smoother;
import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
import com.mojang.math.Axis;
import net.minecraft.client.Minecraft;
import net.minecraft.client.MouseHandler;
import net.minecraft.client.player.LocalPlayer;
import org.joml.Vector3f;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(MouseHandler.class)
public class MouseHandlerMixin {

@Shadow @Final private Minecraft minecraft;

@Unique
Smoother pitchSmoother = new Smoother(30);
@Unique
Smoother rollSmoother = new Smoother(30);

@WrapWithCondition(
method = "turnPlayer",
at = @At(
Expand All @@ -28,12 +33,11 @@ public class MouseHandlerMixin {
if (!(player instanceof Rollable rollable)) return true;

rollable.updateOrientation(() -> {
var pitch = - cursorDeltaY * 0.015f;
var roll = - cursorDeltaX * 0.015f;
var pitch = pitchSmoother.smooth((float) (cursorDeltaY * 0.15f), 1);
var roll = rollSmoother.smooth((float) (cursorDeltaX * 0.15f), 1);

return rollable.getOrientation()
.rotate((float) pitch, new Vector3f(1, 0, 0))
.rotate((float) roll, new Vector3f(0, 0, 1));
// This has side effects so either remove return or make a duplicate
return rollable.rotate(0.0F, pitch, roll).getOrientation();
});
return false;
}
Expand Down
17 changes: 4 additions & 13 deletions src/client/java/ca/landonjw/mixin/client/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import org.joml.Matrix3f;
import org.joml.Vector3f;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -33,21 +32,13 @@ protected PlayerMixin(EntityType<? extends LivingEntity> entityType, Level level
public void tick(CallbackInfo ci) {
// Proof of concept for yaw movement
if (this.getMainHandItem().is(Items.STICK)) {
//Axis.YP.rotationDegrees((float)2).mul(this.orientation, this.orientation);
this.rotateYaw(2);
}
else if (this.getMainHandItem().is(Items.BLAZE_ROD)) {
//Axis.YP.rotationDegrees((float)-2).mul(this.orientation, this.orientation);
this.rotateYaw(-2);
}

var forward = new Vector3f(0, 0, 1);
var forwardRotated = this.orientation.transform(new Vector3f(0.0F, 0.0F, 1.0F)).normalize();
var forwardRotatedNoY = new Vector3f(forwardRotated);
forwardRotatedNoY.y = 0.0F;
var angleY = Math.PI - forward.angleSigned(forwardRotatedNoY.normalize(), new Vector3f(0, 1, 0));
setYRot((float) Math.toDegrees(angleY));

var angleX = Math.PI/2 - Math.acos(forwardRotated.y);
setXRot((float) Math.toDegrees(angleX));
setYRot(this.getYaw());
setXRot(this.getPitch());
}

@Override
Expand Down
97 changes: 96 additions & 1 deletion src/main/java/ca/landonjw/Rollable.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,109 @@
package ca.landonjw;

import org.joml.Matrix3f;
import org.joml.Quaternionf;
import org.joml.Vector3f;

import java.util.function.Supplier;

public interface Rollable {

Vector3f FORWARDS = new Vector3f(0, 0, 1);
Vector3f LEFT = new Vector3f(1, 0, 0);
Vector3f UP = new Vector3f(0, 1, 0);


Matrix3f getOrientation();

void updateOrientation(Supplier<Matrix3f> update);

/**
* @return The forward vector (0, 0, 1) after the rotation matrix is applied.
*/
default Vector3f getForwardVector(){
return getOrientation().transform(FORWARDS, new Vector3f());
}

/**
* @return The left vector (1, 0, 0) after the rotation matrix is applied.
*/
default Vector3f getLeftVector(){
return getOrientation().transform(LEFT, new Vector3f());
}

/**
* @return The up vector (0, 1, 0) after the rotation matrix is applied.
*/
default Vector3f getUpVector(){
return getOrientation().transform(UP, new Vector3f());
}

/**
*
* @param yaw change in degrees
* @return The Rollable object this method was called on
*/

default Rollable rotateYaw(float yaw){
getOrientation().rotate((float) - Math.toRadians(yaw), UP);
return this;
}

/**
*
* @param pitch change in degrees
* @return The Rollable object this method was called on
*/

default Rollable rotatePitch(float pitch){
getOrientation().rotate((float) - Math.toRadians(pitch), LEFT);
return this;
}

/**
*
* @param roll change in degrees
* @return The Rollable object this method was called on
*/
default Rollable rotateRoll(float roll){
getOrientation().rotate((float) - Math.toRadians(roll), FORWARDS);
return this;
}


/**
* Order matters when rotating an object. If you need a different order, individually call rotateYaw, rotatePitch, and rotateRoll
*
* @param yaw change in degrees
* @param pitch change in degrees
* @param roll change in degrees
* @return The Rollable object this method was called on
*/
default Rollable rotate(float yaw, float pitch, float roll){
return rotateYaw(yaw).rotatePitch(pitch).rotateRoll(roll);
}


/**
* @return The yaw angle in degrees as a float.
* Value is between [-90, 90] with 0 being straight forward, 90 being straight down, and -90 being straight up
*/
default float getYaw(){
return (float) Math.toDegrees(Math.PI - FORWARDS.angleSigned(getForwardVector(), UP));
}

/**
* Get the pitch angle in degrees as a float.
* Value is between [-180, 180] with 0 being South, 90 being West, 180/-180 being North, and -90 being East
*/
default float getPitch() {
return (float) Math.toDegrees(Math.PI/2 - Math.acos(getForwardVector().y));
}

/**
* Get the roll angle in degrees as a float.
* Value is between [-180, 180] with 0 being no roll (normal), 90 being 90 degrees CW, 180/-180 being 180 degrees CW/CCW, and -90 being 90 degrees CCW
*/
default float getRoll(){
return (float) Math.toDegrees(- LEFT.angleSigned(getLeftVector(), FORWARDS));
}
}

0 comments on commit 9fc21d4

Please sign in to comment.