Skip to content

Commit

Permalink
Player model roll, fix fall damage
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavid04 committed Feb 17, 2024
1 parent 5d164b6 commit 5b30765
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 31 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
## Zero gravity space
-[x] no gravity
-[x] swim movement
-[ ] no swim up/down based on pitch
-[ ] no swim up/down based on pitch
-[ ] correct on ground to use all surface orientations
-[x] no fall damage
-[ ] bump into wall damage regardless of direction
-[ ] make mob AI less helpless in zero gravity

## 6DOF
-[x] camera
-[ ] model pose
-[x] model pose
-[ ] look around
-[ ] controls for roll
-[ ] camera relative movement
-[ ] camera relative movement (hook Entity.movementInputToVelocity)
-[ ] sync orientation
-[ ] animate transition back to upright (roll)
9 changes: 1 addition & 8 deletions src/main/java/gdavid/sixdoftest/ClientMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.RotationAxis;
import net.minecraft.util.math.Vec3d;
import org.joml.Quaternionf;

public class ClientMod implements ClientModInitializer {

Expand All @@ -14,11 +11,7 @@ public void onInitializeClient() {
WorldRenderEvents.AFTER_SETUP.register((ctx) -> {
Entity focus = ctx.camera().getFocusedEntity();
if (focus == null) return;
float roll = (float) focus.getVelocity().dotProduct(focus.getRotationVecClient().crossProduct(new Vec3d(0, 1, 0)));
Quaternionf q = RotationAxis.POSITIVE_Z.rotation(roll);
var mats = ctx.matrixStack().peek();
mats.getPositionMatrix().rotateLocal(q);
mats.getNormalMatrix().rotateLocal(q);
RollManager.rollTransform(ctx.matrixStack(), focus);
});
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/gdavid/sixdoftest/RollManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gdavid.sixdoftest;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.RotationAxis;
import net.minecraft.util.math.Vec3d;
import org.joml.Quaternionf;

public class RollManager {

public static float getRoll(Entity entity) {
return (float) entity.getVelocity().dotProduct(entity.getRotationVecClient().crossProduct(new Vec3d(0, 1, 0)));
}

@Environment(EnvType.CLIENT)
public static void rollTransform(MatrixStack ms, Entity entity) {
rollTransform(ms, getRoll(entity));
}

@Environment(EnvType.CLIENT)
public static void rollTransform(MatrixStack ms, float roll) {
Quaternionf q = RotationAxis.POSITIVE_Z.rotation(roll);
var mats = ms.peek();
mats.getPositionMatrix().rotateLocal(q);
mats.getNormalMatrix().rotateLocal(q);
}

}
9 changes: 1 addition & 8 deletions src/main/java/gdavid/sixdoftest/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import gdavid.sixdoftest.SpaceManager;
import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
Expand Down Expand Up @@ -36,11 +35,5 @@ private void noCrawlIn6DOFSpace(CallbackInfoReturnable<Boolean> callback) {
if (!SpaceManager.isIn6dof(self())) return;
callback.setReturnValue(false);
}

@Inject(method = "handleFallDamage", at = @At("HEAD"), cancellable = true)
private void noFallDamageIn6DOFSpace(float fallDistance, float damageMultiplier, DamageSource damageSource, CallbackInfoReturnable<Boolean> callback) {
if (!SpaceManager.isIn6dof(self())) return;
callback.setReturnValue(false);
}


}
25 changes: 25 additions & 0 deletions src/main/java/gdavid/sixdoftest/mixin/LivingEntityMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gdavid.sixdoftest.mixin;

import gdavid.sixdoftest.SpaceManager;
import net.minecraft.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(LivingEntity.class)
public class LivingEntityMixin {

@Unique
private LivingEntity self() {
return (LivingEntity) (Object) this;
}

@Inject(method = "computeFallDamage", at = @At("HEAD"), cancellable = true)
private void noFallDamageIn6DOFSpace(float fallDistance, float damageMultiplier, CallbackInfoReturnable<Integer> callback) {
if (!SpaceManager.isIn6dof(self())) return;
callback.setReturnValue(0);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package gdavid.sixdoftest.mixin;

import gdavid.sixdoftest.RollManager;
import gdavid.sixdoftest.SpaceManager;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.render.entity.PlayerEntityRenderer;
import net.minecraft.client.util.math.MatrixStack;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(PlayerEntityRenderer.class)
public class PlayerEntityRendererMixin {
Expand All @@ -15,5 +19,11 @@ public class PlayerEntityRendererMixin {
private boolean fix6DOFSwimPose(AbstractClientPlayerEntity instance) {
return SpaceManager.isIn6dof(instance) || instance.isTouchingWater();
}


@Inject(method = "setupTransforms(Lnet/minecraft/client/network/AbstractClientPlayerEntity;Lnet/minecraft/client/util/math/MatrixStack;FFF)V", at = @At("TAIL"))
private void setup6DOFTranform(AbstractClientPlayerEntity player, MatrixStack matrixStack, float f, float g, float h, CallbackInfo callback) {
if (!SpaceManager.isIn6dof(player)) return;
RollManager.rollTransform(matrixStack, player);
}

}
24 changes: 13 additions & 11 deletions src/main/resources/sixdoftest.mixins.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"required": true,
"minVersion": "0.8",
"package": "gdavid.sixdoftest.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"EntityMixin"
],
"client": [
"PlayerEntityRendererMixin"
],
"injectors": { "defaultRequire": 1 }
"required": true,
"minVersion": "0.8",
"package": "gdavid.sixdoftest.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"EntityMixin",
"LivingEntityMixin"
],
"client": [
"PlayerEntityRendererMixin"
],
"injectors": {
"defaultRequire": 1 }
}

0 comments on commit 5b30765

Please sign in to comment.