Skip to content

Commit

Permalink
Add animated roll return to 0 when exiting rolling state.
Browse files Browse the repository at this point in the history
  • Loading branch information
JPAKx4 committed Oct 12, 2024
1 parent 39d310f commit 7380f59
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
45 changes: 39 additions & 6 deletions src/client/java/ca/landonjw/mixin/client/CameraMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Camera.class)
public class CameraMixin {
public abstract class CameraMixin {

@Shadow private Entity entity;
@Shadow @Final private Quaternionf rotation;
Expand All @@ -25,17 +25,53 @@ public class CameraMixin {
@Shadow @Final private Vector3f left;
@Shadow private float xRot;
@Shadow private float yRot;

@Shadow public abstract float getXRot();

@Shadow public abstract float getYRot();

@Unique private float returnTimer = 0;
@Unique private float rollAngleStart = 0;
@Unique Minecraft minecraft = Minecraft.getInstance();

@Inject(method = "setRotation", at = @At("HEAD"), cancellable = true)
public void open_camera$setRotation(float f, float g, CallbackInfo ci) {
if (!(this.entity instanceof Rollable rollable)) return;
if (!rollable.shouldRoll()) {
rollable.clearRotation();
if (!rollable.shouldRoll() && rollable.getOrientation() != null) {
if(this.returnTimer < 1) {
//Rotation is taken from entity since we no longer handle mouse ourselves
//Stops a period of time when you can't input anything.
rollable.getOrientation().set(
new Matrix3f()
.rotateY((float) Math.toRadians(180 - this.entity.getYRot()))
.rotateX((float) Math.toRadians(-this.entity.getXRot()))
);
if(rollAngleStart == 0){
this.returnTimer = 1;
rollable.clearRotation();
return;
}
rollable.getOrientation().rotateZ((float) Math.toRadians(-rollAngleStart*(1-returnTimer)));
applyRotation();
this.returnTimer += .05F;
ci.cancel();
} else {
this.returnTimer = 1;
rollable.clearRotation();
}
return;
}
if (rollable.getOrientation() == null) return;
applyRotation();

this.returnTimer = 0;
this.rollAngleStart = rollable.getRoll();
ci.cancel();
}

@Unique
private void applyRotation(){
if (!(this.entity instanceof Rollable rollable) || rollable.getOrientation() == null) return;
var newRotation = rollable.getOrientation().normal(new Matrix3f()).getNormalizedRotation(new Quaternionf());
if (this.minecraft.options.getCameraType().isMirrored()) {
newRotation.rotateY((float)Math.toRadians(180));
Expand All @@ -46,8 +82,5 @@ public class CameraMixin {
this.forwards.set(rollable.getForwardVector());
this.up.set(rollable.getUpVector());
this.left.set(rollable.getLeftVector());

ci.cancel();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public class MouseHandlerMixin {
)
public boolean open_camera$modifyRotation(LocalPlayer player, double cursorDeltaX, double cursorDeltaY, @Local(argsOnly = true) double d) {
if (!(player instanceof Rollable rollable)) return true;
if (!rollable.shouldRoll()) return true;
if (!rollable.shouldRoll()) {
pitchSmoother.reset();
rollSmoother.reset();
return true;
}

var pitch = pitchSmoother.getNewDeltaValue(cursorDeltaY * 0.15f, d);
var roll = rollSmoother.getNewDeltaValue(cursorDeltaX * 0.15f, d);
Expand Down
4 changes: 1 addition & 3 deletions src/client/java/ca/landonjw/mixin/client/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

@Mixin(Player.class)
public abstract class PlayerMixin extends LivingEntity implements Rollable {

@Shadow @Final private static Logger LOGGER;
@Unique private Matrix3f orientation = new Matrix3f();
@Unique private Matrix3f orientation = null;

protected PlayerMixin(EntityType<? extends LivingEntity> entityType, Level level) {
super(entityType, level);
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/ca/landonjw/Rollable.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ default Vector3f getUpVector() {
*/

default Rollable rotateYaw(float yaw) {
return updateOrientation(prev -> prev.rotate((float) Math.toRadians(-yaw), UP));
return updateOrientation(prev -> prev.rotateY((float) Math.toRadians(-yaw)));
}

/**
Expand All @@ -57,7 +57,7 @@ default Rollable rotateYaw(float yaw) {
*/

default Rollable rotatePitch(float pitch) {
return updateOrientation(prev -> prev.rotate((float) Math.toRadians(-pitch), LEFT));
return updateOrientation(prev -> prev.rotateX((float) Math.toRadians(-pitch)));
}

/**
Expand All @@ -66,7 +66,7 @@ default Rollable rotatePitch(float pitch) {
* @return The Rollable object this method was called on
*/
default Rollable rotateRoll(float roll) {
return updateOrientation(prev -> prev.rotate((float) Math.toRadians(-roll), FORWARDS));
return updateOrientation(prev -> prev.rotateZ((float) Math.toRadians(-roll)));
}


Expand All @@ -88,23 +88,25 @@ default Rollable rotate(float yaw, float pitch, float roll) {
* 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));
return (float) (180F - Math.toDegrees(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));
return (float) Math.toDegrees(Math.asin(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));
var upVector = getUpVector();
var forwardVector = getForwardVector();
return (float) Math.toDegrees(upVector.angleSigned(UP, forwardVector));
}

boolean shouldRoll();
Expand Down

0 comments on commit 7380f59

Please sign in to comment.