From 71ea2f646afd29cd5ddd843b7fcd0e7933caac5b Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 25 Sep 2020 12:39:10 +0200 Subject: [PATCH] More work on the new flight mechanics --- .../player/AeronauticalPlayerPhysics.java | 50 +++++++++++++++++++ .../unicopia/entity/player/Aeronautics.java | 37 +++++++++++--- 2 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/entity/player/AeronauticalPlayerPhysics.java diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/AeronauticalPlayerPhysics.java b/src/main/java/com/minelittlepony/unicopia/entity/player/AeronauticalPlayerPhysics.java new file mode 100644 index 00000000..9db796ee --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/AeronauticalPlayerPhysics.java @@ -0,0 +1,50 @@ +package com.minelittlepony.unicopia.entity.player; + +import com.minelittlepony.unicopia.util.MutableVector; + +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.util.math.Vec3d; + +public class AeronauticalPlayerPhysics extends PlayerPhysics { + + private final Aeronautics auronautics = new Aeronautics(); + + private double gravity; + + private int thrustCountdown; + + public AeronauticalPlayerPhysics(Pony pony) { + super(pony); + } + + @Override + public double calcGravity(double worldConstant) { + return gravity = super.calcGravity(worldConstant); + } + + @Override + protected void moveFlying(Entity player, MutableVector velocity) { + + PlayerEntity ply = (PlayerEntity)player; + + auronautics.pitchAngle = 0; + auronautics.rollAngle = 0; + + float yaw = player.getYaw(1); + + Vec3d motion = auronautics.calcGravitationalAccelleration(gravity); + + if (ply.forwardSpeed != 0 && thrustCountdown-- <= 0) { + thrustCountdown = 20; + player.playSound(getWingSound(), 0.4F, 1); + motion = motion.add(auronautics.calcThrustVelocity(-100)); + } + + motion = motion.rotateY(yaw).add(velocity.toImmutable()).multiply(1 - auronautics.getDrag()); + + velocity.x = motion.x; + velocity.y = motion.y; + velocity.z = motion.z; + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/Aeronautics.java b/src/main/java/com/minelittlepony/unicopia/entity/player/Aeronautics.java index 494171f5..8f37d15e 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/Aeronautics.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/Aeronautics.java @@ -14,20 +14,41 @@ public class Aeronautics { * Gets the normalized direction vector */ private Vec3d getMomentVector() { - Vec3d bankVector = new Vec3d(0, Math.sin(rollAngle), Math.cos(rollAngle)); + // sine(angle) = y/h + // cos(angle) = x/h + Vec3d climbVector = new Vec3d(Math.cos(pitchAngle), Math.sin(pitchAngle), 0); + Vec3d bankVector = new Vec3d(0, Math.sin(rollAngle), Math.cos(rollAngle)); + + return bankVector.add(climbVector).normalize(); + } + + /** + * Gets the normalized perpendicular vector. + */ + private Vec3d getNormal() { + // sine(angle) = y/h + // cos(angle) = x/h + + Vec3d climbVector = new Vec3d(Math.cos(pitchAngle), Math.sin(pitchAngle), 0); + Vec3d bankVector = new Vec3d(0, Math.sin(rollAngle), Math.cos(rollAngle)); return bankVector.crossProduct(climbVector).normalize(); } /** - * Returns the acceleration vector due to gravity in the direction of - * the incline described by the roll and pitch components. + * Returns the acceleration vector due to gravity + * parallel to the slope of the incline described + * by the roll and pitch components. * * @param gravity The global gravitation constant C */ public Vec3d calcGravitationalAccelleration(double gravity) { - return getMomentVector().multiply(-gravity); + return getMomentVector().multiply( + -gravity * Math.signum(pitchAngle), + -gravity, + -gravity * Math.signum(rollAngle) + ); } /** @@ -37,10 +58,10 @@ public class Aeronautics { * @param forwards The forwards thrust speed * @param velocity The current motion vector */ - public Vec3d calcThrustVelocity(double forwards, Vec3d velocity) { - velocity = velocity.normalize().multiply(forwards); - - return getMomentVector().multiply(velocity.x, velocity.y, 0).multiply(getDrag()); + public Vec3d calcThrustVelocity(double forwards) { + return getNormal().add(getMomentVector()) + .normalize() + .multiply(forwards * (1 - getDrag())); } /**