Author SHA1 Message Date
richard bc4b5bcc92 Add sweep effects and direct cursor aim until the wings lift (0.4.8)
- Read wing sweep from the block layout and lower the lift slope, move the transonic drag rise, and stall the tips of aft swept wings first (or the root of forward swept ones), behind the new sweepEffects config option
- Treat blocks without a horizontal facing as facing the craft's dominant axis, and join diagonal same-level blocks into one wing
- Hold cursor aim in direct surface control until the wings can lift about 1.1g, with hysteresis back to 0.9g, and bank the ailerons toward the cursor while doing so
- Learn the roll sign from the response, like pitch and yaw
- Animate the surfaces from their real deflection and drive the flight computer's sweep-aware stall angle from the same per-block data
- Bump mod_version to 0.4.8
2026-11-21 09:25:53 +01:00
richardandClaude Opus 5.5 dec28c5a01 Let cursor aim turn as hard as the keys (0.4.5)
- Rate loop gets an integral term with anti-windup, so held turns reach
  the commanded rate instead of settling below it (rateIntegralGain)
- Cursor past full deflection asks for more pull than planned; a measured
  angle of attack limiter caps it at the wings' stall limit, like holding
  the key without stalling
- Lift margin is now configurable (liftMargin, default 0.95, was a fixed
  0.85) and also sets the angle of attack limit
- fullDeflectionDegrees defaults to 20 (was 30)
- Cursor aim options are settable in game with /aero
- /aero cursor_debug shows commanded vs actual pitch rate, planned pull,
  lift limit and stall ratio above the hotbar

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 15:56:44 +01:00
richard 80bb63d44f Add LiveInputs so nozzles and Futurism afterburners follow cursor aim
- Cursor aim reverses below the nose and eases off in stalls, with pitch and yaw signs learned from the measured responses
- Cursor aim takes over at low speed and flies the surfaces directly while taxiing
- Nozzles and Futurism vectored afterburners follow the live flight inputs instead of the raw keys
- Warframes vertical surfaces and Futurism vectored afterburners are animated from their real deflection
2026-11-13 12:29:23 +01:00
richard 3478a04885 Rework cursor aim: cursor is world-fixed, pilot turns until reticle meets it
- Move cursor state to a world direction so the offset updates as the craft turns, not on mouse input alone
- Move cursor aim tunables from separate TOML files into Config, under cursor_aim sections
- Pilot banks by a level-wings cursor angle and pulls a level-turn rate, capped by live lift and measured control rate
- Measure each cursor-flown node's steady pitch rate from control torque over pitch damping
- Accumulate maximum stall-limited lift per tick for the pilot's turn-rate and bank limits
- Bump mod version to 0.4.2
2026-11-11 19:54:24 +01:00
Claude 23f0f91ede Animate control surfaces and flaps from the real deflection
A GeckoLib GeoModel mixin sets the Fin bone of Warium's control surfaces to the
deflection the flight physics uses, synced to nearby clients per ship.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YVREx3uo7nz1FEhzveWTUh
2026-11-11 18:44:58 +01:00
30 changed files with 1399 additions and 216 deletions

No files matched your search

+4
View File
@@ -84,6 +84,10 @@ dependencies {
compileOnly fg.deobf("blank:WariumVS:1.1.7")
compileOnly fg.deobf("blank:futurism:0.0.4")
// GeckoLib 4.8.4, which Warium VS animates its control surfaces with (optional dependency, remapped, the surface animation mixin
// only applies when it and Warium VS are present)
compileOnly fg.deobf("maven.modrinth:geckolib:aC5KMoNg")
// AzureLib 3.2.7 for the animated block models (optional dependency, the common jar uses obfuscated names, so it is remapped)
compileOnly fg.deobf("mod.azure.azurelib:azurelib-common-${minecraft_version}:${azurelib_version}")
}
+1 -1
View File
@@ -12,7 +12,7 @@ mapping_version=2023.09.03-1.20.1
mod_id=improved_aerodynamics
mod_name=VS: Improved Aerodynamics
mod_license=MIT
mod_version=0.4.0
mod_version=0.4.8
azurelib_version=3.2.7
mod_group_id=dev.lumareth.improvedaerodynamics
mod_authors=Lumareth
View File
Whitespace-only changes.
View File
Whitespace-only changes.
@@ -123,6 +123,12 @@ public final class Config {
.comment("Improved flight model: multiplier for the extra lift the vortices make at high angles of attack")
.defineInRange("vortexLiftStrength", 1.0D, 0.0D, 3.0D);
public static final ForgeConfigSpec.BooleanValue SWEEP_EFFECTS = BUILDER
.comment("Improved flight model: swept wings behave like swept wings. Sweep lowers the lift slope, moves the drag rise",
"near the speed of sound to higher speeds, and decides where the stall starts: at the tips of an aft swept wing,",
"at the root of a forward swept one, which keeps its ailerons working when the rest of the wing lets go")
.define("sweepEffects", true);
public static final ForgeConfigSpec.DoubleValue FBW_AUTHORITY = BUILDER
.comment("Flight computer (auto trim): largest total pitch input, as a fraction of full deflection, the computer may use.",
"A craft with strongly relaxed stability needs close to 1")
@@ -204,6 +210,61 @@ public final class Config {
.comment("The nozzle gives off light while its afterburner is lit")
.define("lightEnabled", true);
static {
BUILDER.pop();
BUILDER.comment("Cursor aim: the mouse moves a cursor on screen, a virtual pilot flies the control node's craft by it").push("cursor_aim");
}
public static final ForgeConfigSpec.BooleanValue CURSOR_AIM_ENABLED = BUILDER.comment("Whether pilots may use cursor aim on this server")
.define("enabled", true);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_FULL_DEFLECTION_DEGREES = BUILDER.comment("Angle of the cursor from the gun reticle at which the virtual pilot turns as hard as it can: the maximum bank",
"sideways, the full pitch rate up or down. Closer than that it turns more gently, so the reticle settles onto",
"the cursor. How fast the craft can turn is worked out from the craft itself")
.defineInRange("fullDeflectionDegrees", 20.0, 5.0, 90.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_DEADZONE_DEGREES = BUILDER.comment("Radius around the gun reticle in degrees inside which the pilot stops steering towards the cursor. The reticle",
"then settles this far from the cursor. Outside it the command grows from zero, there is no jump at the edge")
.defineInRange("deadzoneDegrees", 0.0, 0.0, 15.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_MAX_ROLL_RATE_DEGREES = BUILDER.comment("Fastest roll in degrees per second the pilot asks for when it rolls to a bank angle")
.defineInRange("maxRollRateDegrees", 150.0, 10.0, 720.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_MAX_BANK_DEGREES = BUILDER.comment("Bank angle the pilot rolls to when the cursor is at full sideways deflection, at most. Craft whose wings",
"cannot hold that bank in a level turn get less")
.defineInRange("maxBankDegrees", 70.0, 10.0, 90.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_BANK_GAIN = BUILDER.comment("Roll rate in rad/s per radian of bank angle error, how eagerly the pilot rolls to the wanted bank and levels the wings")
.defineInRange("bankGain", 3.0, 0.2, 20.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_RATE_GAIN = BUILDER.comment("How hard the pilot corrects its turn rate, in angular acceleration per rad/s of rate error. Lower is smoother,",
"higher follows the cursor tighter and can start to oscillate")
.defineInRange("rateGain", 5.0, 0.5, 40.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_RATE_INTEGRAL_GAIN = BUILDER.comment("How fast the pilot builds up extra deflection when the craft keeps turning slower than asked, so a held turn",
"reaches the rate it asks for. 0 turns it off, high values can make the nose overshoot")
.defineInRange("rateIntegralGain", 4.0, 0.0, 40.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_LIFT_MARGIN = BUILDER.comment("Share of the wings' stall limit the pilot uses: of the lift they can make before stalling when it plans a turn,",
"and of the stall angle of attack, which it never pulls past. 1 flies right at the edge of the stall")
.defineInRange("liftMargin", 0.95, 0.5, 1.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_PUSH_FACTOR = BUILDER.comment("A cursor below the gun reticle pushes the nose down. This is how much of the pull rate that may be, since",
"negative g is harder on the craft and the pilot")
.defineInRange("pushFactor", 0.4, 0.0, 1.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_REVERSE_TURN_DEGREES = BUILDER.comment("How far below the nose (wings level) the cursor has to be before the pilot rolls over and pulls towards it",
"instead of pushing the nose down. Less than that is flown with the elevators alone, like for a landing")
.defineInRange("reverseTurnDegrees", 40.0, 10.0, 135.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_STALLED_PITCH_FACTOR = BUILDER.comment("Multiplier for the pitch rate the pilot asks for while the wings are stalled, so it does not keep pulling")
.defineInRange("stalledPitchFactor", 0.3, 0.0, 1.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_MIN_SPEED = BUILDER.comment("Below this speed in blocks per second the pilot does not fly the craft: the cursor moves the elevators and",
"rudder directly instead, so they follow it while taxiing and rotating for takeoff. With the improved flight",
"model that also holds above it until the wings can lift the craft's weight")
.defineInRange("minSpeed", 15.0, 0.0, 200.0);
static {
BUILDER.pop();
}
@@ -220,6 +281,45 @@ public final class Config {
.comment("Vectored afterburner nozzle: afterburner ignition and heavy afterburner sound")
.define("soundEnabled", true);
public static final ForgeConfigSpec.BooleanValue ANIMATE_SURFACES = CLIENT_BUILDER
.comment("Control surfaces and flaps visibly deflect by as much as the flight physics deflects them, instead of Warium's",
"fixed 20 degree steps. Needs physicsRateAerodynamics on the server")
.define("animateControlSurfaces", true);
static {
CLIENT_BUILDER.comment("Cursor aim, client side").push("cursor_aim");
}
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_LOOK_AHEAD = CLIENT_BUILDER.comment("How strongly the camera looks into the direction of the turn. 0 never moves it, 1 (100%) turns it quickly",
"to look where the cursor points. In between it follows more slowly")
.defineInRange("lookAhead", 0.0, 0.0, 1.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_MOUSE_SENSITIVITY = CLIENT_BUILDER.comment("Multiplier for how far the mouse moves the cursor (or the camera while the free look key is held)")
.defineInRange("mouseSensitivity", 1.0, 0.05, 10.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_SPRING_BACK_SECONDS = CLIENT_BUILDER.comment("The cursor drifts to the gun reticle with this time constant in seconds, which ends the turn by itself.",
"0, the default, keeps it where you put it in the world, so the craft turns until the reticle is on it")
.defineInRange("springBackSeconds", 0.0, 0.0, 10.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_MAX_ANGLE_DEGREES = CLIENT_BUILDER.comment("Furthest the cursor can go from the gun reticle, 135 is a 270 degree cone")
.defineInRange("maxCursorAngleDegrees", 135.0, 10.0, 180.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_ZOOM_STEP = CLIENT_BUILDER.comment("Blocks the camera moves per mouse wheel notch")
.defineInRange("zoomStep", 2.0, 0.1, 20.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_ZOOM_MAX = CLIENT_BUILDER.comment("Furthest the camera can be pulled back from its normal distance, in blocks")
.defineInRange("zoomMax", 80.0, 0.0, 500.0);
public static final ForgeConfigSpec.DoubleValue CURSOR_AIM_MARKER_SIZE = CLIENT_BUILDER.comment("Radius of the cursor ring on screen")
.defineInRange("markerSize", 0.04, 0.01, 0.3);
public static final ForgeConfigSpec.BooleanValue CURSOR_AIM_SHOW_RETICLE = CLIENT_BUILDER.comment("Draw a small cross where the gun reticle points, the zero of the cursor, so you can see it when the camera looks away")
.define("showReticle", true);
static {
CLIENT_BUILDER.pop();
}
static final ForgeConfigSpec CLIENT_SPEC = CLIENT_BUILDER.build();
private Config() {
@@ -8,6 +8,7 @@ import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import dev.lumareth.improvedAerodynamics.exhaust.NozzleCommands;
import dev.lumareth.improvedAerodynamics.flight.ControlMode;
import dev.lumareth.improvedAerodynamics.flight.CursorAimDebug;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.SharedSuggestionProvider;
@@ -23,7 +24,8 @@ import java.util.List;
* /improvedaerodynamics (or /aero) to read and change the most important server config values in game.
* {@code /aero} lists them, {@code /aero <option>} shows one, {@code /aero <option> <value>} sets and saves it.
* The config options need operator rights, except for the owner of a singleplayer world, who may use them without cheats.
* {@code /aero controls keyboard|cursor_aim} is open to every player and picks how they fly.
* {@code /aero controls keyboard|cursor_aim} is open to every player and picks how they fly, and
* {@code /aero cursor_debug} shows them what cursor aim is doing.
* All of them are read live, so changes apply immediately.
*/
public final class ConfigCommands {
@@ -48,6 +50,7 @@ public final class ConfigCommands {
new Option<>("transonic_peak_drag", Config.TRANSONIC_PEAK_DRAG, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("supersonic_drag", Config.SUPERSONIC_DRAG, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("vortex_lift", Config.VORTEX_LIFT, BoolArgumentType.bool(), Boolean.class),
new Option<>("sweep_effects", Config.SWEEP_EFFECTS, BoolArgumentType.bool(), Boolean.class),
new Option<>("vortex_stall_bonus", Config.VORTEX_STALL_BONUS, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("vortex_lift_strength", Config.VORTEX_LIFT_STRENGTH, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("flight_computer_authority", Config.FBW_AUTHORITY, DoubleArgumentType.doubleArg(), Double.class),
@@ -68,7 +71,20 @@ public final class ConfigCommands {
new Option<>("flap_slew_degrees", Config.FLAP_SLEW_DEGREES, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("flap_takeoff_max_speed", Config.FLAP_TAKEOFF_MAX_SPEED, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("flap_landing_max_speed", Config.FLAP_LANDING_MAX_SPEED, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("lookahead_seconds", Config.LOOKAHEAD_SECONDS, DoubleArgumentType.doubleArg(), Double.class));
new Option<>("lookahead_seconds", Config.LOOKAHEAD_SECONDS, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_enabled", Config.CURSOR_AIM_ENABLED, BoolArgumentType.bool(), Boolean.class),
new Option<>("cursor_aim_full_deflection_degrees", Config.CURSOR_AIM_FULL_DEFLECTION_DEGREES, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_deadzone_degrees", Config.CURSOR_AIM_DEADZONE_DEGREES, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_max_roll_rate_degrees", Config.CURSOR_AIM_MAX_ROLL_RATE_DEGREES, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_max_bank_degrees", Config.CURSOR_AIM_MAX_BANK_DEGREES, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_bank_gain", Config.CURSOR_AIM_BANK_GAIN, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_rate_gain", Config.CURSOR_AIM_RATE_GAIN, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_rate_integral_gain", Config.CURSOR_AIM_RATE_INTEGRAL_GAIN, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_lift_margin", Config.CURSOR_AIM_LIFT_MARGIN, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_push_factor", Config.CURSOR_AIM_PUSH_FACTOR, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_reverse_turn_degrees", Config.CURSOR_AIM_REVERSE_TURN_DEGREES, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_stalled_pitch_factor", Config.CURSOR_AIM_STALLED_PITCH_FACTOR, DoubleArgumentType.doubleArg(), Double.class),
new Option<>("cursor_aim_min_speed", Config.CURSOR_AIM_MIN_SPEED, DoubleArgumentType.doubleArg(), Double.class));
private ConfigCommands() {
}
@@ -93,6 +109,7 @@ public final class ConfigCommands {
root.then(SeedgenCommands.node().requires(ConfigCommands::mayUse));
root.then(NozzleCommands.node().requires(ConfigCommands::mayUse));
root.then(controlsNode());
root.then(CursorAimDebug.node());
LiteralCommandNode<CommandSourceStack> node = event.getDispatcher().register(root);
event.getDispatcher().register(Commands.literal("aero")
.executes(context -> mayUse(context.getSource()) ? list(context.getSource()) : denied(context.getSource()))
@@ -1,11 +1,9 @@
package dev.lumareth.improvedAerodynamics;
import com.mojang.logging.LogUtils;
import dev.lumareth.improvedAerodynamics.client.aim.CursorAimClientConfig;
import dev.lumareth.improvedAerodynamics.client.pipper.PipperConfig;
import dev.lumareth.improvedAerodynamics.exhaust.ExhaustSync;
import dev.lumareth.improvedAerodynamics.exhaust.NozzleContent;
import dev.lumareth.improvedAerodynamics.flight.CursorAimConfig;
import dev.lumareth.improvedAerodynamics.flight.PhysicsBridge;
import dev.lumareth.improvedAerodynamics.network.Network;
import dev.lumareth.improvedAerodynamics.terrain.LookaheadLoader;
@@ -29,12 +27,12 @@ public class ImprovedAerodynamics {
MinecraftForge.EVENT_BUS.register(ExhaustSync.class);
MinecraftForge.EVENT_BUS.register(dev.lumareth.improvedAerodynamics.flight.ControlMode.class);
MinecraftForge.EVENT_BUS.register(dev.lumareth.improvedAerodynamics.flight.FlapInteraction.class);
MinecraftForge.EVENT_BUS.register(dev.lumareth.improvedAerodynamics.flight.DeflectionSync.class);
MinecraftForge.EVENT_BUS.register(dev.lumareth.improvedAerodynamics.flight.CursorAimDebug.class);
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, Config.SPEC);
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, Config.CLIENT_SPEC);
// Own file name: the default one is taken by Config.CLIENT_SPEC
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, PipperConfig.SPEC, "improved_aerodynamics-pipper-client.toml");
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, CursorAimConfig.SPEC, "improved_aerodynamics-cursor-aim.toml");
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, CursorAimClientConfig.SPEC, "improved_aerodynamics-cursor-aim-client.toml");
NozzleContent.register(FMLJavaModLoadingContext.get().getModEventBus());
Network.register();
}
@@ -1,5 +1,6 @@
package dev.lumareth.improvedAerodynamics.client.aim;
import dev.lumareth.improvedAerodynamics.Config;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferUploader;
@@ -32,6 +33,7 @@ import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.util.ObfuscationReflectionHelper;
import org.joml.Matrix4f;
import org.joml.primitives.AABBdc;
import org.joml.Quaterniondc;
import org.joml.Vector3d;
import org.joml.Vector3f;
@@ -44,9 +46,10 @@ import java.lang.reflect.Method;
/**
* Cursor aim, the client side. While it is on:
* <ul>
* <li>the mouse moves a cursor instead of the camera. The cursor is a direction relative to the gun reticle (the
* craft's boresight), at most {@code maxCursorAngleDegrees} away from it, and springs back to it when the mouse
* rests. It is sent to the server every tick, where a virtual pilot flies the craft by it;</li>
* <li>the mouse moves a cursor instead of the camera. The cursor is a fixed direction in the world: the craft
* turning does not drag it along, so the virtual pilot on the server steers until the gun reticle (the craft's
* boresight) sits on the cursor again. It is kept at most {@code maxCursorAngleDegrees} from the reticle and, if
* spring back is set, drifts back to it. Every tick it is sent to the server as an offset from the reticle;</li>
* <li>the camera is a chase view, looking along the reticle, or, with look ahead, towards the cursor;</li>
* <li>holding the free look key moves the camera instead of the cursor, it eases back when released;</li>
* <li>the mouse wheel changes how far the camera is behind the craft.</li>
@@ -62,16 +65,23 @@ public final class CursorAimClient {
private static final float MARKER_DISTANCE = 2.0F;
private static final double LOOK_AHEAD_RATE = 6.0;
private static final double FREE_LOOK_RETURN_RATE = 10.0;
// Blocks under the craft's bounding box that count as still on the ground, and how long it has to be off it
private static final double GROUND_CLEARANCE = 1.5;
private static final int AIRBORNE_TICKS = 20;
private static boolean active;
// Chosen with /aero controls, told by the server
private static boolean cursorAimMode;
// Cursor as components along the craft's forward (gun reticle), right and up axes
// Cursor as a world direction, null until the first frame puts it on the reticle
private static Vector3d cursorWorld;
// The same cursor as components along the craft's forward (gun reticle), right and up axes, as of the last update
private static double cf = 1.0;
private static double cr;
private static double cu;
// Camera direction in ship space and the free look offset on top of it, degrees
private static double[] cameraDirection;
// Client ticks the craft has been off the ground, up to AIRBORNE_TICKS
private static int airborneTicks;
private static double offsetYaw;
private static double offsetPitch;
// Rotation the player was given last frame, to see what the mouse did since
@@ -109,9 +119,91 @@ public final class CursorAimClient {
deactivate(minecraft);
return;
}
// The craft has turned since the last frame: the cursor stays put in the world, its offset changes
Entity seat = AimSeat.seatOf(player);
AimBasis basis = minecraft.level != null ? AimBasis.of(minecraft.level, seat.blockPosition()) : null;
Quaterniondc rotation = basis != null ? shipRotation(minecraft.level, seat, false) : null;
if (rotation != null) {
cursorFromWorld(basis, rotation);
}
updateAirborne(minecraft.level, seat);
send(minecraft, true);
}
/**
* Whether the craft is off the ground, for look ahead: on the ground (and for a moment after leaving it) the camera
* stays behind the reticle, so pulling the nose up to take off does not swing it down under the craft.
*/
private static void updateAirborne(ClientLevel level, Entity seat) {
Ship ship = VSGameUtilsKt.getLoadedShipManagingPos(level, BlockPos.containing(seat.getX(), seat.getY(), seat.getZ()));
if (ship == null || onGround(level, ship.getWorldAABB())) {
airborneTicks = 0;
} else if (airborneTicks < AIRBORNE_TICKS) {
airborneTicks++;
}
}
/**
* Whether there is solid ground right under the craft: any block with a collision shape in a layer just below the
* bottom of its world bounding box. The craft's own blocks are not in the world, so they never count.
*/
private static boolean onGround(ClientLevel level, AABBdc box) {
int minX = Mth.floor(box.minX());
int maxX = Mth.floor(box.maxX());
int minZ = Mth.floor(box.minZ());
int maxZ = Mth.floor(box.maxZ());
int fromY = Mth.floor(box.minY() - GROUND_CLEARANCE);
int toY = Mth.floor(box.minY() + 0.5);
// At most about 16 x 16 columns, a big craft is sampled on a coarser grid
int stepX = Math.max(1, (maxX - minX) / 16);
int stepZ = Math.max(1, (maxZ - minZ) / 16);
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
for (int x = minX; x <= maxX; x += stepX) {
for (int z = minZ; z <= maxZ; z += stepZ) {
for (int y = fromY; y <= toY; y++) {
pos.set(x, y, z);
if (!level.getBlockState(pos).getCollisionShape(level, pos).isEmpty()) {
return true;
}
}
}
}
return false;
}
/** Rotation of the ship the seat is on, its render transform when drawing, null if it is not on a ship. */
private static Quaterniondc shipRotation(ClientLevel level, Entity seat, boolean render) {
Ship ship = VSGameUtilsKt.getLoadedShipManagingPos(level, BlockPos.containing(seat.getX(), seat.getY(), seat.getZ()));
if (ship == null) {
return null;
}
return render && ship instanceof ClientShip clientShip
? clientShip.getRenderTransform().getShipToWorldRotation() : ship.getTransform().getShipToWorldRotation();
}
/** Sets the cursor's components from its world direction, putting it on the reticle the first time. */
private static void cursorFromWorld(AimBasis basis, Quaterniondc rotation) {
if (cursorWorld == null) {
double[] forward = basis.forward();
cursorWorld = rotation.transform(new Vector3d(forward[0], forward[1], forward[2]));
}
Vector3d ship = rotation.transformInverse(cursorWorld, new Vector3d());
double[] f = basis.forward();
double[] r = basis.right();
double[] u = basis.up();
cf = ship.x * f[0] + ship.y * f[1] + ship.z * f[2];
cr = ship.x * r[0] + ship.y * r[1] + ship.z * r[2];
cu = ship.x * u[0] + ship.y * u[1] + ship.z * u[2];
normalizeCursor();
limitCursor();
}
/** Stores the cursor's components back as its world direction. */
private static void cursorToWorld(AimBasis basis, Quaterniondc rotation) {
double[] ship = basis.toShip(cf, cr, cu);
cursorWorld = rotation.transform(new Vector3d(ship[0], ship[1], ship[2]));
}
/** Called when the server tells the client how its player flies. */
public static void setMode(boolean cursorAim) {
cursorAimMode = cursorAim;
@@ -135,10 +227,12 @@ public final class CursorAimClient {
return;
}
active = true;
cursorWorld = null;
cf = 1.0;
cr = 0.0;
cu = 0.0;
cameraDirection = null;
airborneTicks = 0;
offsetYaw = 0.0;
offsetPitch = 0.0;
haveLast = false;
@@ -184,7 +278,8 @@ public final class CursorAimClient {
}
Entity seat = AimSeat.seatOf(player);
AimBasis basis = AimBasis.of(level, seat.blockPosition());
if (basis == null) {
Quaterniondc rotation = basis != null ? shipRotation(level, seat, true) : null;
if (basis == null || rotation == null) {
return;
}
if (minecraft.options.getCameraType() != CameraType.THIRD_PERSON_BACK) {
@@ -207,10 +302,11 @@ public final class CursorAimClient {
deltaPitch = 0.0;
}
}
double sensitivity = CursorAimClientConfig.MOUSE_SENSITIVITY.get();
double sensitivity = Config.CURSOR_AIM_MOUSE_SENSITIVITY.get();
boolean freeLook = minecraft.screen == null && AimKeys.FREE_LOOK.isDown();
double spring = CursorAimClientConfig.SPRING_BACK_SECONDS.get();
cursorFromWorld(basis, rotation);
double spring = Config.CURSOR_AIM_SPRING_BACK_SECONDS.get();
if (spring > 0.0) {
shrinkCursor(Math.exp(-dt / spring));
}
@@ -223,14 +319,16 @@ public final class CursorAimClient {
offsetYaw *= decay;
offsetPitch *= decay;
}
cursorToWorld(basis, rotation);
// Camera: along the reticle, or turned towards the cursor by the look ahead setting
double lookAhead = CursorAimClientConfig.LOOK_AHEAD.get();
// Camera: along the reticle, or turned towards the cursor by the look ahead setting once the craft is flying
double lookAhead = Config.CURSOR_AIM_LOOK_AHEAD.get();
double[] forward = basis.forward();
if (cameraDirection == null || lookAhead <= 0.0) {
cameraDirection = forward.clone();
} else {
double[] target = basis.toShip(cf, cr, cu);
// On the ground it eases back behind the reticle rather than snapping there
double[] target = airborneTicks >= AIRBORNE_TICKS ? basis.toShip(cf, cr, cu) : forward;
cameraDirection = turnTowards(cameraDirection, target, 1.0 - Math.exp(-lookAhead * LOOK_AHEAD_RATE * dt));
}
double[] direction = rotate(cameraDirection, basis.up(), -Math.toRadians(offsetYaw));
@@ -295,7 +393,7 @@ public final class CursorAimClient {
}
private static void limitCursor() {
double limit = Math.toRadians(CursorAimClientConfig.MAX_CURSOR_ANGLE_DEGREES.get());
double limit = Math.toRadians(Config.CURSOR_AIM_MAX_ANGLE_DEGREES.get());
double angle = Math.acos(Mth.clamp(cf, -1.0, 1.0));
if (angle <= limit) {
return;
@@ -318,7 +416,7 @@ public final class CursorAimClient {
if (!active || minecraft.screen != null) {
return;
}
zoom = Mth.clamp(zoom - event.getScrollDelta() * CursorAimClientConfig.ZOOM_STEP.get(), -3.0, CursorAimClientConfig.ZOOM_MAX.get());
zoom = Mth.clamp(zoom - event.getScrollDelta() * Config.CURSOR_AIM_ZOOM_STEP.get(), -3.0, Config.CURSOR_AIM_ZOOM_MAX.get());
event.setCanceled(true);
}
@@ -354,14 +452,10 @@ public final class CursorAimClient {
}
Entity seat = AimSeat.seatOf(player);
AimBasis basis = AimBasis.of(level, seat.blockPosition());
Ship ship = VSGameUtilsKt.getLoadedShipManagingPos(level, BlockPos.containing(seat.getX(), seat.getY(), seat.getZ()));
if (basis == null || ship == null) {
Quaterniondc rotation = basis != null ? shipRotation(level, seat, true) : null;
if (rotation == null || cursorWorld == null) {
return;
}
Quaterniondc rotation = ship instanceof ClientShip clientShip
? clientShip.getRenderTransform().getShipToWorldRotation() : ship.getTransform().getShipToWorldRotation();
double[] cursorShip = basis.toShip(cf, cr, cu);
Vector3d cursorWorld = rotation.transform(new Vector3d(cursorShip[0], cursorShip[1], cursorShip[2]));
double[] forward = basis.forward();
Vector3d reticleWorld = rotation.transform(new Vector3d(forward[0], forward[1], forward[2]));
@@ -375,8 +469,8 @@ public final class CursorAimClient {
RenderSystem.setShader(GameRenderer::getPositionColorShader);
builder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
float size = (float) (CursorAimClientConfig.MARKER_SIZE.get() * MARKER_DISTANCE);
if (CursorAimClientConfig.SHOW_RETICLE.get()) {
float size = (float) (Config.CURSOR_AIM_MARKER_SIZE.get() * MARKER_DISTANCE);
if (Config.CURSOR_AIM_SHOW_RETICLE.get()) {
plus(builder, matrix, camera, reticleWorld, size * 0.35F, RETICLE_COLOR);
}
ring(builder, matrix, camera, cursorWorld, size, CURSOR_COLOR);
@@ -1,45 +0,0 @@
package dev.lumareth.improvedAerodynamics.client.aim;
import net.minecraftforge.common.ForgeConfigSpec;
/** Client side settings of cursor aim. Lives in improved_aerodynamics-cursor-aim-client.toml. */
public final class CursorAimClientConfig {
public static final ForgeConfigSpec SPEC;
public static final ForgeConfigSpec.DoubleValue LOOK_AHEAD;
public static final ForgeConfigSpec.DoubleValue MOUSE_SENSITIVITY;
public static final ForgeConfigSpec.DoubleValue SPRING_BACK_SECONDS;
public static final ForgeConfigSpec.DoubleValue MAX_CURSOR_ANGLE_DEGREES;
public static final ForgeConfigSpec.DoubleValue ZOOM_STEP;
public static final ForgeConfigSpec.DoubleValue ZOOM_MAX;
public static final ForgeConfigSpec.DoubleValue MARKER_SIZE;
public static final ForgeConfigSpec.BooleanValue SHOW_RETICLE;
static {
ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
builder.comment("Cursor aim, client side").push("cursor_aim");
LOOK_AHEAD = builder.comment("How strongly the camera looks into the direction of the turn. 0 never moves it, 1 (100%) turns it quickly",
"to look where the cursor points. In between it follows more slowly")
.defineInRange("lookAhead", 0.0, 0.0, 1.0);
MOUSE_SENSITIVITY = builder.comment("Multiplier for how far the mouse moves the cursor (or the camera while the free look key is held)")
.defineInRange("mouseSensitivity", 1.0, 0.05, 10.0);
SPRING_BACK_SECONDS = builder.comment("The cursor returns to the gun reticle with this time constant in seconds when you stop moving the mouse.",
"0 makes it stay where you left it")
.defineInRange("springBackSeconds", 1.0, 0.0, 10.0);
MAX_CURSOR_ANGLE_DEGREES = builder.comment("Furthest the cursor can go from the gun reticle, 135 is a 270 degree cone")
.defineInRange("maxCursorAngleDegrees", 135.0, 10.0, 180.0);
ZOOM_STEP = builder.comment("Blocks the camera moves per mouse wheel notch")
.defineInRange("zoomStep", 2.0, 0.1, 20.0);
ZOOM_MAX = builder.comment("Furthest the camera can be pulled back from its normal distance, in blocks")
.defineInRange("zoomMax", 80.0, 0.0, 500.0);
MARKER_SIZE = builder.comment("Radius of the cursor ring on screen")
.defineInRange("markerSize", 0.04, 0.01, 0.3);
SHOW_RETICLE = builder.comment("Draw a small cross where the gun reticle points, the zero of the cursor, so you can see it when the camera looks away")
.define("showReticle", true);
builder.pop();
SPEC = builder.build();
}
private CursorAimClientConfig() {
}
}
@@ -0,0 +1,138 @@
package dev.lumareth.improvedAerodynamics.client.animation;
import dev.lumareth.improvedAerodynamics.Config;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.ClientPlayerNetworkEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import software.bernie.geckolib.core.animatable.model.CoreGeoBone;
import software.bernie.geckolib.model.GeoModel;
/**
* Client side of the control surface animation. The server tells the clients how far each control surface is deflected
* (see {@code DeflectionSync}); this class keeps those values, eases towards them, and sets the rotation of the "Fin"
* bone of Warium's control surface models to them after GeckoLib has played Warium's own fixed animation.
* <p>
* Warium's animations tilt the Fin bone by 20 degrees about X for a positive input, which GeckoLib turns into a
* rotation of -20 degrees (it negates X and Y for rotations), and the same sign is used here, so a surface moves the
* way Warium's own animation moves it, only by the amount the physics uses. Warframes' vertical control surfaces are
* modelled differently: their Fin turns about Y, by -20 degrees in the animation file (+20 after GeckoLib) for the same
* input.
* <p>
* Futurism's vectored afterburner is shown the same way: its own animation only swings the Nozzle bone all the way (20
* degrees about X) up or down, here it follows the tilt the block actually has, which Futurism keeps in the block's
* synced data as "Vectorpitch" (minus a third of the input).
*/
@Mod.EventBusSubscriber(modid = dev.lumareth.improvedAerodynamics.ImprovedAerodynamics.MOD_ID, value = Dist.CLIENT)
public final class SurfaceAnimation {
private static final String BONE = "Fin";
private static final String WARFRAMES_VERTICAL = "net.evo_mc.warframes.block.entity.WarframeVerticalControlSurfaceBlockEntity";
private static final String FUTURISM_VECTORED = "net.mcreator.futurism.block.entity.VectoredAfterburnerTileEntity";
private static final String NOZZLE_BONE = "Nozzle";
// Futurism's animation swing at full input, degrees
private static final float FUTURISM_FULL_DEGREES = 20.0F;
// A value that has not been refreshed for this long is dropped, Warium's own animation takes over again
private static final long STALE_NANOS = 3_000_000_000L;
// Time in seconds the visible deflection takes to close most of the way to the latest value
private static final double EASE_SECONDS = 0.07;
private static final class Entry {
float target;
float shown;
long updated;
long drawn;
boolean placed;
}
private static final Long2ObjectOpenHashMap<Entry> ENTRIES = new Long2ObjectOpenHashMap<>();
// Shown angle (degrees) of each Futurism vectored afterburner
private static final Long2ObjectOpenHashMap<Entry> VECTORED = new Long2ObjectOpenHashMap<>();
private SurfaceAnimation() {
}
/** New deflection of a control surface in degrees, in Warium's animation sign (positive is its "input up" tilt). */
public static void set(long pos, float degrees) {
Entry entry = ENTRIES.computeIfAbsent(pos, key -> new Entry());
entry.target = degrees;
entry.updated = System.nanoTime();
if (!entry.placed) {
// First value of a surface: show it right away, it may have been there before the player came near
entry.shown = degrees;
entry.placed = true;
}
}
public static void apply(BlockEntity blockEntity, GeoModel<?> model) {
if (!Config.ANIMATE_SURFACES.get()) {
return;
}
if (blockEntity.getClass().getName().equals(FUTURISM_VECTORED)) {
applyVectored(blockEntity, model);
return;
}
if (ENTRIES.isEmpty()) {
return;
}
Entry entry = ENTRIES.get(blockEntity.getBlockPos().asLong());
if (entry == null) {
return;
}
long now = System.nanoTime();
if (now - entry.updated > STALE_NANOS) {
ENTRIES.remove(blockEntity.getBlockPos().asLong());
return;
}
ease(entry, now);
CoreGeoBone bone = model.getAnimationProcessor().getBone(BONE);
if (bone == null) {
return;
}
float radians = (float) Math.toRadians(entry.shown);
if (blockEntity.getClass().getName().equals(WARFRAMES_VERTICAL)) {
bone.setRotX(0.0F);
bone.setRotY(radians);
} else {
bone.setRotX(-radians);
}
// Setting a rotation flags the bone as animated, and GeckoLib cleared its flags before this runs. Left set, the
// flag would make GeckoLib skip resetting this bone for the next block drawn with the same model (all blocks of
// a model share its bones), so that block would keep this block's deflection
bone.resetStateChanges();
}
/** Futurism's vectored afterburner: the Nozzle bone at the block's real tilt, Futurism's sign. */
private static void applyVectored(BlockEntity blockEntity, GeoModel<?> model) {
float input = (float) (-3.0 * blockEntity.getPersistentData().getDouble("Vectorpitch"));
float degrees = FUTURISM_FULL_DEGREES * Math.max(-1.0F, Math.min(1.0F, input));
Entry entry = VECTORED.computeIfAbsent(blockEntity.getBlockPos().asLong(), key -> new Entry());
entry.target = degrees;
if (!entry.placed) {
entry.shown = degrees;
entry.placed = true;
}
ease(entry, System.nanoTime());
CoreGeoBone bone = model.getAnimationProcessor().getBone(NOZZLE_BONE);
if (bone != null) {
bone.setRotX((float) -Math.toRadians(entry.shown));
bone.resetStateChanges();
}
}
/** Moves the shown value towards the target. The same block can be drawn more than once a frame (shadows, other passes), the easing steps once per frame. */
private static void ease(Entry entry, long now) {
if (entry.drawn != 0L && now != entry.drawn) {
double dt = (now - entry.drawn) / 1.0E9;
entry.shown += (entry.target - entry.shown) * (float) (1.0 - Math.exp(-dt / EASE_SECONDS));
}
entry.drawn = now;
}
@SubscribeEvent
public static void onLoggingOut(ClientPlayerNetworkEvent.LoggingOut event) {
ENTRIES.clear();
VECTORED.clear();
}
}
@@ -1,5 +1,7 @@
package dev.lumareth.improvedAerodynamics.exhaust;
import dev.lumareth.improvedAerodynamics.flight.ControlAxis;
import dev.lumareth.improvedAerodynamics.flight.LiveInputs;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
@@ -13,7 +15,8 @@ import org.jetbrains.annotations.Nullable;
* with Warium's two key configuration screen, are read. Either key can name any axis: Pitch+ means W raises the nose and
* Pitch- means S does, Roll+ or Roll- says which roll input rolls the plane right, Yaw+ or Yaw- which yaw input yaws it
* right. An axis no key names is off, except that a nozzle with no key at all pitches with W, and yaw follows D without
* a key.
* a key. While the ship flies, the inputs are the ones the flight physics uses ({@link LiveInputs}), so the nozzles follow
* cursor aim and the flight computer like the control surfaces do.
*/
final class NozzleControl {
private static final ResourceLocation NODE_BLOCK = new ResourceLocation("valkyrien_warium", "vehicle_control_node");
@@ -43,9 +46,10 @@ final class NozzleControl {
String key = own.getString("Key");
String key2 = own.getString("Key2");
boolean unset = (key.isEmpty() || key.equals("Null")) && (key2.isEmpty() || key2.equals("Null"));
double pitch = clamp(tag.getDouble("Pitch")) * sign(key, key2, "Pitch", unset ? 1.0 : 0.0);
double roll = clamp(tag.getDouble("Roll")) * sign(key, key2, "Roll", 0.0);
double yaw = clamp(tag.getDouble("Yaw")) * sign(key, key2, "Yaw", 1.0);
long nodeKey = nodePos.asLong();
double pitch = clamp(LiveInputs.value(nodeKey, tag, ControlAxis.PITCH)) * sign(key, key2, "Pitch", unset ? 1.0 : 0.0);
double roll = clamp(LiveInputs.value(nodeKey, tag, ControlAxis.ROLL)) * sign(key, key2, "Roll", 0.0);
double yaw = clamp(LiveInputs.value(nodeKey, tag, ControlAxis.YAW)) * sign(key, key2, "Yaw", 1.0);
return new Inputs(node, pitch, roll, yaw, Math.max(0.0, tag.getDouble("Throttle")), tag.getBoolean(AfterburnerToggle.TAG));
}
@@ -27,6 +27,8 @@ public final class AeroState implements PhysicsBridge.Publishable {
private static final double INPUT_RATE_PER_TICK = 0.2;
private static final double VELOCITY_EPSILON = 0.05;
private static final double ANGULAR_EPSILON = 0.005;
// Time constant of the smoothing of measured control rates
private static final double RATE_SMOOTHING_SECONDS = 0.3;
private static final ControlAxis[] MEASURED_AXES = {ControlAxis.PITCH, ControlAxis.YAW, ControlAxis.ROLL};
private final SubstepClock clock;
@@ -41,7 +43,15 @@ public final class AeroState implements PhysicsBridge.Publishable {
private Frame current;
private boolean[] stalled = new boolean[0];
private boolean wingStalled;
// Lift the wings could make at the edge of the stall in the airflow of the last substep, NaN without the improved
// flight model, which has no stall
private double maxLift = Double.NaN;
// Highest stall ratio (angle of attack over stall angle) of any wing in the last substep, NaN without the improved
// flight model
private double wingStallRatio = Double.NaN;
private final Map<Long, NodeState> nodeStates = new ConcurrentHashMap<>();
// Every ship's node states by node position, for the control surface animation on the server thread
private static final Map<Long, NodeState> ALL_NODE_STATES = new ConcurrentHashMap<>();
private final Wrench wrench = new Wrench();
private final Wrench probe = new Wrench();
@@ -57,6 +67,8 @@ public final class AeroState implements PhysicsBridge.Publishable {
double trimOutput;
// Deflection of this node's flaps in radians, moves towards what the flap lever and airspeed allow
double flapAngle;
// Wall clock time of the last physics substep that ran this node, to tell whether its ship is flying
volatile long updatedMillis;
// Total pitch input the flight computer applied last substep, pilot and trim together
double computerOutput;
// Torque (ship axes) one unit of pitch input on this node's pitch surfaces makes, measured every substep
@@ -67,6 +79,30 @@ public final class AeroState implements PhysicsBridge.Publishable {
final Vector3d yawResponseBuilding = new Vector3d();
final Vector3d rollResponse = new Vector3d();
final Vector3d rollResponseBuilding = new Vector3d();
// Fastest steady pitch rate (rad/s) the pitch surfaces can hold against the craft's pitch damping, smoothed,
// measured while cursor aim flies the node. NaN until measured
double controlRate = Double.NaN;
// Cursor aim is rolling the craft over to reach a cursor far below the nose
boolean reversing;
// Cursor aim moves the surfaces straight from the cursor because the craft is not flying yet (or any more)
boolean directMode = true;
// Cursor aim's integral of the turn rate error (angular acceleration, ship axes), so it reaches the rate it asks for
final Vector3d rateIntegral = new Vector3d();
// What cursor aim did last, for /aero cursor_debug: rates in rad/s, lift as load factor (g)
volatile double debugCommandedPitch;
volatile double debugActualPitch;
volatile double debugMaxPull;
volatile double debugLoadFactor = Double.NaN;
volatile double debugStallRatio = Double.NaN;
volatile boolean debugActive;
// Share of the pull cursor aim keeps, eased down while the wings are stalled
double stallEase = 1.0;
// Which way pitch and yaw input turn the craft (+1 or -1 for nose up and nose right), learned from the measured
// responses. Until then Warium's keys: W (Pitch+) is usually nose down, D (Yaw+) nose right
double pitchSign = -1.0;
double yawSign = 1.0;
// The same for roll, +1 when Roll+ rolls the craft right (Warium's C key)
double rollSign = 1.0;
final AutoTrim.Targets trimTargets = new AutoTrim.Targets();
volatile double savedTrim;
@@ -151,9 +187,14 @@ public final class AeroState implements PhysicsBridge.Publishable {
double dt = clock.substepSeconds();
boolean improved = Config.IMPROVED_FLIGHT_MODEL.get();
Matrix3dc inertia = ship.getMomentOfInertia();
// Same frames and scaling as Warium applies its forces in
double scale = transform.getShipToWorldScaling().x();
double forceScale = autoScale ? scale * scale * scale : 1.0;
double liftAcceleration = maxLift * forceScale / ship.getMass();
updateNodes(frame, worldVelocity, transform.getPositionInWorld().y(), rotation, inertia, w, dt, improved);
updateNodes(frame, worldVelocity, transform.getPositionInWorld().y(), rotation, inertia, w, dt, improved, liftAcceleration);
evaluate(frame, centerOfMass, v, w, improved, true, wrench);
measureControlRates(frame, centerOfMass, v, w, improved, dt);
if (Config.IMPLICIT_DAMPING.get()) {
// Implicit Euler for the damping part: scale each axis by 1 / (1 + dt * damping / inertia), using the
@@ -180,9 +221,6 @@ public final class AeroState implements PhysicsBridge.Publishable {
}
}
// Same frames and scaling as Warium applies its forces in
double scale = transform.getShipToWorldScaling().x();
double forceScale = autoScale ? scale * scale * scale : 1.0;
Vector3d force = new Vector3d(wrench.force[0], wrench.force[1], wrench.force[2]).mul(forceScale);
Vector3d forceTorque = new Vector3d(wrench.forceTorque[0], wrench.forceTorque[1], wrench.forceTorque[2]).mul(forceScale);
if (force.isFinite() && forceTorque.isFinite()) {
@@ -209,9 +247,15 @@ public final class AeroState implements PhysicsBridge.Publishable {
for (int i = 0; i < stalled.length; i++) {
stalled[i] = previous.get(frame.surfaces[i].key());
}
nodeStates.keySet().removeIf(key -> !frame.nodes.containsKey((long) key));
nodeStates.keySet().removeIf(key -> {
boolean gone = !frame.nodes.containsKey((long) key);
if (gone) {
ALL_NODE_STATES.remove(key);
}
return gone;
});
for (NodeData node : frame.nodes.values()) {
nodeStates.computeIfAbsent(node.key(), key -> new NodeState(node));
ALL_NODE_STATES.put(node.key(), nodeStates.computeIfAbsent(node.key(), key -> new NodeState(node)));
}
current = frame;
}
@@ -229,6 +273,8 @@ public final class AeroState implements PhysicsBridge.Publishable {
double[] r = new double[3];
if (commit) {
wingStalled = false;
maxLift = improved ? 0.0 : Double.NaN;
wingStallRatio = Double.NaN;
for (NodeState state : nodeStates.values()) {
state.pitchResponseBuilding.zero();
state.yawResponseBuilding.zero();
@@ -251,6 +297,15 @@ public final class AeroState implements PhysicsBridge.Publishable {
if (commit) {
stalled[i] = nowStalled;
wingStalled |= nowStalled && s.type() == SurfaceType.WING;
if (s.type() == SurfaceType.WING || s.type() == SurfaceType.CONTROL_SURFACE && s.wingGroup()) {
maxLift += ImprovedAero.maxLift(s, v, w, r, flap, weight, tuning);
}
if (s.type() == SurfaceType.WING) {
double ratio = ImprovedAero.stallRatio(s, v, w, r, flap, tuning);
if (!Double.isNaN(ratio) && !(ratio <= wingStallRatio)) {
wingStallRatio = ratio;
}
}
}
} else {
WariumAero.evaluate(s, v, w, r, input, weight, out);
@@ -296,6 +351,37 @@ public final class AeroState implements PhysicsBridge.Publishable {
}
}
/**
* For every node cursor aim flies: the steady pitch rate its pitch surfaces can hold, full deflection torque over
* the craft's pitch damping, both measured about the axis the pitch surfaces turn the craft about.
*/
private void measureControlRates(Frame frame, Vector3dc centerOfMass, double[] v, double[] w, boolean improved, double dt) {
for (NodeData node : frame.nodes.values()) {
NodeState state = nodeStates.get(node.key());
if (state == null || !CursorAimRegistry.isActive(node.key())) {
continue;
}
double response = state.pitchResponse.length();
if (response < 1.0E-9) {
continue;
}
Vector3d axis = state.pitchResponse.div(response, new Vector3d());
double[] turned = {w[0] + ANGULAR_EPSILON * axis.x, w[1] + ANGULAR_EPSILON * axis.y, w[2] + ANGULAR_EPSILON * axis.z};
evaluate(frame, centerOfMass, v, turned, improved, false, probe);
double damping = 0.0;
for (int i = 0; i < 3; i++) {
damping -= (probe.totalTorque(i) - wrench.totalTorque(i)) * axis.get(i);
}
damping /= ANGULAR_EPSILON;
if (!(damping > 1.0E-9)) {
continue;
}
double rate = response / damping;
state.controlRate = Double.isNaN(state.controlRate) ? rate
: state.controlRate + (rate - state.controlRate) * Math.min(1.0, dt / RATE_SMOOTHING_SECONDS);
}
}
/** A control surface's input, combined from its two keys and clamped like Warium does. */
private static double input(AeroSurface s, NodeData node, NodeState state, ControlAxis nudged, double extra) {
if (node == null || state == null) {
@@ -318,7 +404,7 @@ public final class AeroState implements PhysicsBridge.Publishable {
}
private void updateNodes(Frame frame, Vector3dc worldVelocity, double altitude, Quaterniondc rotation, Matrix3dc inertia,
double[] w, double dt, boolean improved) {
double[] w, double dt, boolean improved, double liftAcceleration) {
double step = Config.CONTROL_SMOOTHING.get() ? INPUT_RATE_PER_TICK * clock.weight() : Double.POSITIVE_INFINITY;
// Torque of the last substep, in ship axes, which the flight computer separates from its own surfaces' share
double[] torque = {wrench.totalTorque(0), wrench.totalTorque(1), wrench.totalTorque(2)};
@@ -328,11 +414,13 @@ public final class AeroState implements PhysicsBridge.Publishable {
continue;
}
// Cursor aim, if the pilot has it on, supplies the inputs the pilot is not giving with the keys
NodeData effective = CursorPilot.apply(state, node, rotation, inertia, w, worldVelocity.length(), wingStalled);
NodeData effective = CursorPilot.apply(state, node, rotation, inertia, w, worldVelocity.length(), wingStalled,
liftAcceleration, wingStallRatio, dt);
state.pitch = moveTowards(state.pitch, effective.pitch(), step);
state.yaw = moveTowards(state.yaw, effective.yaw(), step);
state.roll = moveTowards(state.roll, effective.roll(), step);
updateFlaps(state, node, worldVelocity.length(), dt);
state.updatedMillis = System.currentTimeMillis();
AutoTrim.update(state, effective, worldVelocity, altitude, rotation, inertia, w, torque, dt, improved && wingStalled);
}
}
@@ -351,6 +439,11 @@ public final class AeroState implements PhysicsBridge.Publishable {
state.flapAngle = moveTowards(state.flapAngle, target, Math.toRadians(Config.FLAP_SLEW_DEGREES.get()) * dt);
}
/** The physics state of a control node, null if no physics runs for it. */
static NodeState nodeState(long nodeKey) {
return ALL_NODE_STATES.get(nodeKey);
}
static double moveTowards(double value, double target, double maxStep) {
if (Math.abs(target - value) <= maxStep) {
return target;
@@ -7,6 +7,8 @@ package dev.lumareth.improvedAerodynamics.flight;
* @param key BlockPos.asLong of the block
* @param x block center
* @param facingX Warium's "facing" of the block, the chord direction of the surface
* @param facingKnown whether the block says its direction; if not, the facing is a stand in until WingShape gives it
* the direction the craft's other surfaces agree on
* @param pressure Warium's air pressure factor at the block's altitude
* @param fluidFactor Warium's multiplier for blocks under water (1 in air)
* @param speedMultiplier Warium's aerodynamicSpeedMultiplier game rule / 100, at least 1
@@ -15,16 +17,25 @@ package dev.lumareth.improvedAerodynamics.flight;
* @param aspectRatio span squared over area of the wing the block belongs to, 0 if not known yet
* @param wingGroup whether the wing the block belongs to contains WING blocks, not only control surfaces or stabilizers
* @param flap whether the block was made a flap with shift-right-click, it then also follows its node's flap setting
* @param sweep sweep of the half wing the block belongs to, radians: positive when its tip lies further towards
* +facing than its root. Whether that is forward or aft sweep depends on which way the air comes
* @param spanFraction where along its half wing the block sits, 0 at the root and 1 at the tip
*/
public record AeroSurface(long key, SurfaceType type, boolean slab, double x, double y, double z, int facingX, int facingZ,
double pressure, double fluidFactor, double speedMultiplier,
public record AeroSurface(long key, SurfaceType type, double x, double y, double z, int facingX, int facingZ,
boolean facingKnown, double pressure, double fluidFactor, double speedMultiplier,
long node, ControlAxis axis1, double multiplier1, ControlAxis axis2, double multiplier2,
double brakeInput, double aspectRatio, boolean wingGroup, boolean flap) {
double brakeInput, double aspectRatio, boolean wingGroup, boolean flap, double sweep, double spanFraction) {
public static final long NO_NODE = Long.MIN_VALUE;
AeroSurface withWingShape(double aspectRatio, boolean wingGroup) {
return new AeroSurface(key, type, slab, x, y, z, facingX, facingZ, pressure, fluidFactor, speedMultiplier,
node, axis1, multiplier1, axis2, multiplier2, brakeInput, aspectRatio, wingGroup, flap);
AeroSurface withWingShape(double aspectRatio, boolean wingGroup, double sweep, double spanFraction) {
return new AeroSurface(key, type, x, y, z, facingX, facingZ, facingKnown, pressure, fluidFactor, speedMultiplier,
node, axis1, multiplier1, axis2, multiplier2, brakeInput, aspectRatio, wingGroup, flap, sweep, spanFraction);
}
/** The same surface facing along the x axis (true) or the z axis, the direction now counting as known. */
AeroSurface withFacingAxis(boolean alongX) {
return new AeroSurface(key, type, x, y, z, alongX ? 1 : 0, alongX ? 0 : 1, true, pressure, fluidFactor, speedMultiplier,
node, axis1, multiplier1, axis2, multiplier2, brakeInput, aspectRatio, wingGroup, flap, sweep, spanFraction);
}
/** Warium's "Direction.getClockWise(Axis.Y)" of the facing, the span direction of a horizontal surface. */
@@ -1,56 +0,0 @@
package dev.lumareth.improvedAerodynamics.flight;
import net.minecraftforge.common.ForgeConfigSpec;
/** Server side settings of the cursor aim virtual pilot. Lives in improved_aerodynamics-cursor-aim.toml. */
public final class CursorAimConfig {
public static final ForgeConfigSpec SPEC;
public static final ForgeConfigSpec.BooleanValue ENABLED;
public static final ForgeConfigSpec.DoubleValue FULL_DEFLECTION_DEGREES;
public static final ForgeConfigSpec.DoubleValue DEADZONE_DEGREES;
public static final ForgeConfigSpec.DoubleValue MAX_TURN_RATE_DEGREES;
public static final ForgeConfigSpec.DoubleValue MAX_ROLL_RATE_DEGREES;
public static final ForgeConfigSpec.DoubleValue MAX_BANK_DEGREES;
public static final ForgeConfigSpec.DoubleValue BANK_GAIN;
public static final ForgeConfigSpec.DoubleValue RATE_GAIN;
public static final ForgeConfigSpec.DoubleValue PUSH_FACTOR;
public static final ForgeConfigSpec.DoubleValue STALLED_PITCH_FACTOR;
public static final ForgeConfigSpec.DoubleValue MIN_SPEED;
static {
ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
builder.comment("Cursor aim: the mouse moves a cursor on screen, a virtual pilot flies the control node's craft by it").push("cursor_aim");
ENABLED = builder.comment("Whether pilots may use cursor aim on this server")
.define("enabled", true);
FULL_DEFLECTION_DEGREES = builder.comment("Angle of the cursor from the gun reticle at which the virtual pilot asks for full deflection: the maximum bank",
"sideways, the full pitch rate up or down")
.defineInRange("fullDeflectionDegrees", 30.0, 5.0, 90.0);
DEADZONE_DEGREES = builder.comment("Radius around the gun reticle in degrees inside which the cursor commands nothing, so the craft flies straight.",
"Outside it the command grows from zero, there is no jump at the edge")
.defineInRange("deadzoneDegrees", 3.0, 0.0, 15.0);
MAX_TURN_RATE_DEGREES = builder.comment("Turn rate in degrees per second the pilot asks for at full deflection, both for the turn at maximum bank and for pitching")
.defineInRange("maxTurnRateDegrees", 40.0, 5.0, 360.0);
MAX_ROLL_RATE_DEGREES = builder.comment("Fastest roll in degrees per second the pilot asks for when it rolls to a bank angle")
.defineInRange("maxRollRateDegrees", 150.0, 10.0, 720.0);
MAX_BANK_DEGREES = builder.comment("Bank angle the pilot rolls to when the cursor is at full sideways deflection")
.defineInRange("maxBankDegrees", 70.0, 10.0, 90.0);
BANK_GAIN = builder.comment("Roll rate in rad/s per radian of bank angle error, how eagerly the pilot rolls to the wanted bank and levels the wings")
.defineInRange("bankGain", 3.0, 0.2, 20.0);
RATE_GAIN = builder.comment("How hard the pilot corrects its turn rate, in angular acceleration per rad/s of rate error. Lower is smoother,",
"higher follows the cursor tighter and can start to oscillate")
.defineInRange("rateGain", 5.0, 0.5, 40.0);
PUSH_FACTOR = builder.comment("A cursor below the gun reticle pushes the nose down. This is how much of the pull rate that may be, since",
"negative g is harder on the craft and the pilot")
.defineInRange("pushFactor", 0.4, 0.0, 1.0);
STALLED_PITCH_FACTOR = builder.comment("Multiplier for the pitch rate the pilot asks for while the wings are stalled, so it does not keep pulling")
.defineInRange("stalledPitchFactor", 0.3, 0.0, 1.0);
MIN_SPEED = builder.comment("Below this speed in blocks per second the pilot does nothing, the surfaces have no effect anyway")
.defineInRange("minSpeed", 15.0, 0.0, 200.0);
builder.pop();
SPEC = builder.build();
}
private CursorAimConfig() {
}
}
@@ -0,0 +1,81 @@
package dev.lumareth.improvedAerodynamics.flight;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.mcreator.valkyrienwarium.entity.SeatEntityEntity;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModList;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;
/**
* {@code /aero cursor_debug} toggles a line above the hotbar that shows what cursor aim is doing for the player's control
* node: the pitch rate it asks for and the one the craft makes, the pull it plans with, the most lift the wings can make
* (as load factor) and how close they fly to the stall (1 is the stall). Open to every player, like /aero controls.
*/
public final class CursorAimDebug {
private static final int INTERVAL_TICKS = 4;
private static final Set<UUID> PLAYERS = new HashSet<>();
private CursorAimDebug() {
}
public static LiteralArgumentBuilder<CommandSourceStack> node() {
return Commands.literal("cursor_debug")
.requires(source -> source.getEntity() instanceof ServerPlayer)
.executes(context -> {
ServerPlayer player = context.getSource().getPlayerOrException();
boolean on = PLAYERS.add(player.getUUID());
if (!on) {
PLAYERS.remove(player.getUUID());
}
context.getSource().sendSuccess(() -> Component.literal("Cursor aim debug " + (on ? "on" : "off")), false);
return 1;
});
}
@SubscribeEvent
public static void onServerTick(TickEvent.ServerTickEvent event) {
if (event.phase != TickEvent.Phase.END || PLAYERS.isEmpty()) {
return;
}
MinecraftServer server = event.getServer();
if (server.getTickCount() % INTERVAL_TICKS != 0 || !ModList.get().isLoaded("valkyrien_warium")) {
return;
}
for (UUID id : PLAYERS) {
ServerPlayer player = server.getPlayerList().getPlayer(id);
if (player != null) {
player.displayClientMessage(Component.literal(line(player)), true);
}
}
}
private static String line(ServerPlayer player) {
Entity vehicle = player.getVehicle();
if (!(vehicle instanceof SeatEntityEntity)) {
return "Cursor aim: not in a seat";
}
AimBasis basis = AimBasis.of(vehicle.level(), vehicle.blockPosition());
AeroState.NodeState state = basis != null ? AeroState.nodeState(basis.nodePos().asLong()) : null;
if (state == null) {
return "Cursor aim: no physics for this node (not moving)";
}
if (!state.debugActive) {
return state.directMode ? String.format(Locale.ROOT, "Cursor aim: direct control, wings lift %4.1fg of the 1.1g it waits for",
state.debugLoadFactor) : "Cursor aim: off";
}
return String.format(Locale.ROOT, "pitch %5.1f/%5.1f deg/s plan %5.1f lift %4.1fg stall %4.2f",
Math.toDegrees(state.debugActualPitch), Math.toDegrees(state.debugCommandedPitch),
Math.toDegrees(state.debugMaxPull), state.debugLoadFactor, state.debugStallRatio);
}
}
@@ -1,5 +1,6 @@
package dev.lumareth.improvedAerodynamics.flight;
import dev.lumareth.improvedAerodynamics.Config;
import net.mcreator.valkyrienwarium.entity.SeatEntityEntity;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
@@ -24,7 +25,7 @@ public final class CursorAimServer {
}
long nodeKey = basis.nodePos().asLong();
double length = Math.sqrt(cf * cf + cr * cr + cu * cu);
if (!active || !CursorAimConfig.ENABLED.get() || !(length > 1.0E-6) || !Double.isFinite(length)) {
if (!active || !Config.CURSOR_AIM_ENABLED.get() || !(length > 1.0E-6) || !Double.isFinite(length)) {
CursorAimRegistry.clear(nodeKey);
return;
}
@@ -1,29 +1,68 @@
package dev.lumareth.improvedAerodynamics.flight;
import dev.lumareth.improvedAerodynamics.Config;
import org.joml.Matrix3d;
import org.joml.Matrix3dc;
import org.joml.Quaterniondc;
import org.joml.Vector3d;
/**
* The virtual pilot of cursor aim. The cursor is a stick that springs back to the gun reticle:
* The virtual pilot of cursor aim. The cursor is a direction fixed in the world, and the pilot turns the craft until
* the gun reticle sits on it, so the offset it gets shrinks to nothing as the turn completes:
* <ul>
* <li>sideways it commands a bank angle and turn: the further out, the steeper the bank and the harder the pull
* that comes with it. Centered it levels the wings, so a centered cursor is straight flight;</li>
* <li>up and down it commands a pitch rate;</li>
* <li>close to the reticle it steers with yaw and pitch directly instead of banking, so aiming does not rock the
* wings.</li>
* <li>it banks towards the cursor, judged with the wings level so the bank does not hide where the cursor is: the
* further the cursor is to the side, the steeper the bank. While banked it pulls as much as a level turn at that
* bank needs, so the craft turns without losing height;</li>
* <li>it pitches towards the cursor as seen from the cockpit, so once banked the cursor is above the nose and the
* pull brings the nose round onto it;</li>
* <li>a cursor far below the nose (more than {@code reverseTurnDegrees}, judged with the wings level) is not reached
* by pushing: the pilot rolls until the cursor is above the canopy and pulls, a reversal or split-S. It keeps at it
* until the reticle is close to the cursor, or the cursor is moved above the nose again. A cursor only a little below
* the nose is reached with the elevators alone, gently, so the craft can still be flown down to a landing;</li>
* <li>until the wings can carry the craft (and always below {@code minSpeed}) there is nothing to fly by: the cursor
* moves elevators and rudder directly, for taxiing and rotating on the takeoff run;</li>
* <li>close to the reticle it levels the wings and puts the reticle on the cursor with rudder and elevator, so
* aiming does not rock the wings.</li>
* </ul>
* The wanted rotation is turned into stick inputs by the measured effect of each input on the craft (torque per unit
* of input, see {@link AeroState}), so it works for any craft however its surfaces are mixed or oriented: angular
* acceleration wanted times inertia, solved for the three inputs with a little regularization for axes that have no
* surfaces. Keys the pilot holds win over it, per axis.
* <p>
* How hard the craft can turn is worked out live, not configured: the lift its wings can make at the edge of the stall
* at the current speed and altitude, over its mass and speed, and the steady pitch rate its pitch surfaces can hold
* against its pitch damping, whichever is lower. The same lift limit caps the bank to what the craft can hold level, so
* a heavy bomber banks gently and turns wide while a light fighter racks into a steep, tight turn.
*/
final class CursorPilot {
private static final double FINE_START = Math.toRadians(4.0);
private static final double FINE_END = Math.toRadians(16.0);
private static final double MANUAL_OVERRIDE = 0.05;
private static final double MAX_PULL_BANK = Math.toRadians(90.0);
// Gravity of VS
private static final double GRAVITY = 10.0;
// Cursor beyond full deflection: the pull command grows to this many times the planned rate by 1.5 times the full
// deflection angle, and the angle of attack limit decides how much of it the craft gets
private static final double OVERDRIVE = 2.5;
private static final double OVERDRIVE_END = 1.5;
// Pitch rate (rad/s) the angle of attack limiter takes off per unit of stall ratio over the limit
private static final double ALPHA_GAIN = 4.0;
// The rate integral may make up for this much rate error (rad/s) worth of acceleration
private static final double MAX_ERROR_FOR_INTEGRAL = 0.5;
// Bank the pilot rolls to at full sideways deflection even when the wings cannot hold the turn level
private static final double MIN_BANK = Math.toRadians(20.0);
// Load factor the wings must be able to make before the rate loop takes over from flying the surfaces directly, and
// below which it hands back, apart so it does not switch back and forth
private static final double FLYING_LOAD_ENTER = 1.1;
private static final double FLYING_LOAD_EXIT = 0.9;
// Aileron input per radian of bank error, and taken off per rad/s of roll rate, when flying the surfaces directly
private static final double DIRECT_BANK_GAIN = 1.5;
private static final double DIRECT_ROLL_DAMPING = 0.5;
// Smallest pull allowed, as a share of the turn rate, when gravity takes most of the lift
private static final double MIN_PULL_SHARE = 0.2;
// Time constant of easing the pull back in and out of a stall
private static final double STALL_EASE_SECONDS = 0.4;
// Turn rate when neither limit can be measured, like the first moments of a flight
private static final double FALLBACK_TURN_RATE = Math.toRadians(30.0);
private CursorPilot() {
}
@@ -32,57 +71,163 @@ final class CursorPilot {
* @return the node with the pilot's wanted inputs in place of the keys, or the node itself if cursor aim is off
*/
static NodeData apply(AeroState.NodeState state, NodeData node, Quaterniondc rotation, Matrix3dc inertia, double[] w,
double speed, boolean wingStalled) {
if (!CursorAimConfig.ENABLED.get()) {
double speed, boolean wingStalled, double liftAcceleration, double stallRatio, double dt) {
if (!Config.CURSOR_AIM_ENABLED.get()) {
return node;
}
CursorAimRegistry.Aim aim = CursorAimRegistry.get(node.key());
if (aim == null || speed < CursorAimConfig.MIN_SPEED.get()) {
if (aim == null) {
state.rateIntegral.zero();
state.debugActive = false;
return node;
}
double fullAngle = Math.toRadians(CursorAimConfig.FULL_DEFLECTION_DEGREES.get());
double maxTurn = Math.toRadians(CursorAimConfig.MAX_TURN_RATE_DEGREES.get());
double maxRoll = Math.toRadians(CursorAimConfig.MAX_ROLL_RATE_DEGREES.get());
double maxBank = Math.toRadians(CursorAimConfig.MAX_BANK_DEGREES.get());
double fullAngle = Math.toRadians(Config.CURSOR_AIM_FULL_DEFLECTION_DEGREES.get());
double maxRoll = Math.toRadians(Config.CURSOR_AIM_MAX_ROLL_RATE_DEGREES.get());
double maxBank = Math.toRadians(Config.CURSOR_AIM_MAX_BANK_DEGREES.get());
double[] f = aim.forward();
double[] r = aim.right();
double[] u = aim.up();
// Cursor as angles from the reticle: sideways (positive right) and up, both through any 270 degree cone
double sideways = Math.atan2(aim.cr(), aim.cf());
double vertical = Math.atan2(aim.cu(), Math.hypot(aim.cf(), aim.cr()));
// Deadzone around the reticle: shrink the cursor offset by its radius, direction kept
double offset = Math.hypot(sideways, vertical);
double deadzone = Math.toRadians(CursorAimConfig.DEADZONE_DEGREES.get());
if (offset > 1.0E-9) {
double scale = Math.max(0.0, offset - deadzone) / offset;
sideways *= scale;
vertical *= scale;
}
double lateral = saturate(sideways / fullAngle);
double blend = smoothstep(Math.abs(sideways), FINE_START, FINE_END);
learnSigns(state, f, r, u);
// Bank of the craft: positive when the right wing is down, through upside down
Vector3d rightWorld = rotation.transform(new Vector3d(r[0], r[1], r[2]));
Vector3d upWorld = rotation.transform(new Vector3d(u[0], u[1], u[2]));
double bank = Math.atan2(-rightWorld.y, upWorld.y);
double bankCommand = maxBank * lateral * blend;
double rollRate = clamp(CursorAimConfig.BANK_GAIN.get() * (bankCommand - bank), -maxRoll, maxRoll);
// Pitch: the stick, plus the pull that carries the turn while banked
double pitchStick = saturate(vertical / fullAngle);
double pitchRate = maxTurn * (pitchStick >= 0.0 ? pitchStick : pitchStick * CursorAimConfig.PUSH_FACTOR.get());
if (Math.abs(bank) < MAX_PULL_BANK) {
pitchRate += maxTurn * Math.abs(lateral) * blend * Math.sin(Math.abs(bank));
// Deadzone around the reticle: shrink the cursor's offset by its radius, direction kept
double cf = aim.cf();
double cr = aim.cr();
double cu = aim.cu();
double deadzone = Math.toRadians(Config.CURSOR_AIM_DEADZONE_DEGREES.get());
double offset = Math.acos(clamp(cf, -1.0, 1.0));
double across = Math.hypot(cr, cu);
if (deadzone > 0.0 && offset > 1.0E-9 && across > 1.0E-9) {
double shrunk = Math.max(0.0, offset - deadzone);
cf = Math.cos(shrunk);
cr *= Math.sin(shrunk) / across;
cu *= Math.sin(shrunk) / across;
}
if (wingStalled && pitchRate > 0.0) {
pitchRate *= CursorAimConfig.STALLED_PITCH_FACTOR.get();
// Cursor as angles from the reticle as seen from the cockpit: sideways (positive right) and up, through any
// 270 degree cone
double sideways = Math.atan2(cr, cf);
double vertical = Math.atan2(cu, Math.hypot(cf, cr));
// The cursor as it would be with the wings level: what the bank is chosen by
double levelRight = cr * Math.cos(bank) + cu * Math.sin(bank);
double levelUp = cu * Math.cos(bank) - cr * Math.sin(bank);
double levelVertical = Math.atan2(levelUp, Math.hypot(cf, levelRight));
// Flying or not: the rate loop only makes sense once the wings can carry the craft. Below that (taxiing, the
// takeoff run, a stall) it would plan with almost no lift, ask for almost no pitch rate, and the angle of attack
// limiter would forbid rotating a nose high craft on its gear, so the elevators would hardly move. So the cursor
// moves the surfaces directly until the wings can lift a bit more than the craft's weight, and again once they
// fall a bit short of it. Without a stall model only the minimum speed decides
double liftMargin = Config.CURSOR_AIM_LIFT_MARGIN.get();
boolean slow = speed < Config.CURSOR_AIM_MIN_SPEED.get();
state.debugLoadFactor = liftMargin * liftAcceleration / GRAVITY;
if (slow || !Double.isFinite(liftAcceleration)) {
state.directMode = slow;
} else {
double loadFactor = liftMargin * liftAcceleration / GRAVITY;
state.directMode = state.directMode ? loadFactor < FLYING_LOAD_ENTER : loadFactor < FLYING_LOAD_EXIT;
}
if (state.directMode) {
// The cursor moves the surfaces directly, rudder towards it (which also steers on the ground) and elevator up
// or down, so the controls follow the cursor while taxiing and rotating. The ailerons bank towards a cursor to
// the side and otherwise hold the wings level, damped by the roll rate so they do not rock the craft
state.reversing = false;
state.rateIntegral.zero();
state.debugActive = false;
double pitch = clamp(state.pitchSign * saturate(vertical / fullAngle) - state.trimOutput, -1.0, 1.0);
double yaw = state.yawSign * saturate(sideways / fullAngle);
double bankCommand = maxBank * saturate(sideways / fullAngle);
double rollRate = w[0] * f[0] + w[1] * f[1] + w[2] * f[2];
double roll = state.rollSign * clamp(DIRECT_BANK_GAIN * (bankCommand - bank) - DIRECT_ROLL_DAMPING * rollRate, -1.0, 1.0);
return withInputs(node, pitch, yaw, roll);
}
// What this craft can do right now. Lift: pitch rate at the highest usable lift (a = L / m, rate = a / v), and
// the bank whose level turn needs that load factor. Without a stall model only the control limit applies
double usableLift = liftMargin * liftAcceleration;
double liftRate = usableLift > 0.0 ? usableLift / speed : Double.POSITIVE_INFINITY;
double controlRate = state.controlRate > 0.0 ? state.controlRate : Double.POSITIVE_INFINITY;
double maxTurn = Math.min(liftRate, controlRate);
if (!Double.isFinite(maxTurn)) {
maxTurn = FALLBACK_TURN_RATE;
}
// Fastest pull: part of the lift already holds the craft up against gravity, only the rest curves the flight
// path, so asking for the full lift rate in a steep bank would drag the wings into the stall
double maxPull = maxTurn;
if (Double.isFinite(liftAcceleration)) {
double loadFactor = usableLift / GRAVITY;
double levelBank = loadFactor > 1.0 ? Math.acos(1.0 / loadFactor) : 0.0;
maxBank = Math.max(Math.min(MIN_BANK, maxBank), Math.min(maxBank, levelBank));
double pullLift = (usableLift - GRAVITY * upWorld.y) / speed;
maxPull = clamp(Math.min(pullLift, controlRate), MIN_PULL_SHARE * maxTurn, maxTurn);
}
// Reversal: entered when the cursor is far below the nose, left once the reticle is close to it or the cursor
// is above the nose again
double offsetAngle = Math.acos(clamp(cf, -1.0, 1.0));
if (state.reversing) {
state.reversing = offsetAngle > FINE_END && levelVertical < 0.0;
} else {
state.reversing = levelVertical < -Math.toRadians(Config.CURSOR_AIM_REVERSE_TURN_DEGREES.get());
}
// Bank: roll so the cursor is above the canopy, which is where a pull takes the nose. Judged by the direction of
// the cursor around the nose, which stays well defined wherever the cursor is, even 90 degrees off the nose
// straight above or to the side; the angle of the cursor from the nose only sets how much of that bank is used,
// so the wings come level as the reticle closes on the cursor
double blend = state.reversing ? 1.0 : smoothstep(offsetAngle, FINE_START, FINE_END);
double bankError;
if (state.reversing) {
// All the way over, the shorter way round
bankError = wrap(Math.atan2(levelRight, levelUp) - bank);
} else {
// A cursor below the nose counts as if it were as far above it: only its sideways part banks the craft, and
// the craft is never rolled upside down for a slight descent
double overhead = Math.atan2(levelRight, Math.abs(levelUp));
double bankCommand = clamp(overhead, -maxBank, maxBank) * saturate(offsetAngle / fullAngle) * blend;
bankError = bankCommand - bank;
}
double rollRate = clamp(Config.CURSOR_AIM_BANK_GAIN.get() * bankError, -maxRoll, maxRoll);
// Pitch: the pull of a level turn at the current bank, the turn rate g tan(bank) / v seen about the pitch axis,
// plus a pull towards the cursor. Past 90 degrees of bank the level turn pull would only dive, so it is left out
double pitchStick = saturate(vertical / fullAngle);
double push = Config.CURSOR_AIM_PUSH_FACTOR.get();
// A cursor past full deflection asks for everything: more pull than planned, which the angle of attack limiter
// below cuts to the most the wings can give, like holding the key, only without stalling
double overdrive = 1.0 + (OVERDRIVE - 1.0) * smoothstep(vertical / fullAngle, 1.0, OVERDRIVE_END);
double pullLimit = maxPull * overdrive;
// While rolling into a reversal the cursor is still below the nose, pushing there would only work against it
double pitchRate = pitchStick >= 0.0 ? pullLimit * pitchStick : state.reversing ? 0.0 : maxTurn * pitchStick * push;
double cosBank = Math.cos(bank);
if (cosBank > 0.0) {
double sinBank = Math.sin(bank);
pitchRate += GRAVITY * sinBank * sinBank / (speed * Math.max(cosBank, 0.1));
}
// Stalled wings ease the pull back, and it comes back as gently once they fly again: switching it at once with
// the stall would make the craft buck in and out of the stall
double stallTarget = wingStalled ? Config.CURSOR_AIM_STALLED_PITCH_FACTOR.get() : 1.0;
state.stallEase += (stallTarget - state.stallEase) * Math.min(1.0, dt / STALL_EASE_SECONDS);
pitchRate = clamp(pitchRate, -maxTurn * push, pullLimit);
if (pitchRate > 0.0) {
pitchRate *= state.stallEase;
}
// Angle of attack limiter: the wings may go up to the lift margin's share of their stall angle, no further. Measured,
// so it holds whatever the lift estimate gets wrong
double actualPitch = w[0] * r[0] + w[1] * r[1] + w[2] * r[2];
if (Double.isFinite(stallRatio)) {
double alphaCap = actualPitch + ALPHA_GAIN * (liftMargin - stallRatio);
if (pitchRate > alphaCap) {
pitchRate = alphaCap;
}
}
// Close to the reticle the nose follows the cursor with the rudder
double yawRate = maxTurn * lateral * (1.0 - blend);
double yawRate = maxTurn * saturate(sideways / fullAngle) * (1.0 - blend);
// Wanted angular velocity in ship axes: roll about forward, pitch about right, yaw (nose right) about -up
Vector3d wanted = new Vector3d(
@@ -90,7 +235,10 @@ final class CursorPilot {
rollRate * f[1] + pitchRate * r[1] - yawRate * u[1],
rollRate * f[2] + pitchRate * r[2] - yawRate * u[2]);
Vector3d error = wanted.sub(w[0], w[1], w[2]);
Vector3d acceleration = error.mul(CursorAimConfig.RATE_GAIN.get());
double rateGain = Config.CURSOR_AIM_RATE_GAIN.get();
// Proportional on the rate error plus its integral: holding a turn needs a steady deflection against the craft's
// damping and stability, which the proportional part alone only gives while the craft falls short of the rate
Vector3d acceleration = error.mul(rateGain, new Vector3d()).add(state.rateIntegral);
Vector3d torque = inertia.transform(acceleration, new Vector3d());
double[] inputs = solve(state.pitchResponse, state.yawResponse, state.rollResponse, torque);
@@ -98,9 +246,31 @@ final class CursorPilot {
return node;
}
double pitch = clamp(inputs[0] - state.trimOutput, -1.0, 1.0);
double rawPitch = inputs[0] - state.trimOutput;
boolean saturated = Math.abs(rawPitch) > 1.0 || Math.abs(inputs[1]) > 1.0 || Math.abs(inputs[2]) > 1.0;
if (!saturated) {
// Only while the surfaces have room left, so the integral cannot wind up against full deflection
state.rateIntegral.add(error.mul(Config.CURSOR_AIM_RATE_INTEGRAL_GAIN.get() * dt, new Vector3d()));
double limit = rateGain * MAX_ERROR_FOR_INTEGRAL;
if (state.rateIntegral.length() > limit) {
state.rateIntegral.normalize(limit);
}
}
state.debugCommandedPitch = pitchRate;
state.debugActualPitch = actualPitch;
state.debugMaxPull = maxPull;
state.debugStallRatio = stallRatio;
state.debugActive = true;
double pitch = clamp(rawPitch, -1.0, 1.0);
double yaw = clamp(inputs[1], -1.0, 1.0);
double roll = clamp(inputs[2], -1.0, 1.0);
return withInputs(node, pitch, yaw, roll);
}
/** The node with these inputs on the axes the pilot is not holding a key for. */
private static NodeData withInputs(NodeData node, double pitch, double yaw, double roll) {
return new NodeData(node.key(),
Math.abs(node.pitch()) > MANUAL_OVERRIDE ? node.pitch() : pitch,
Math.abs(node.yaw()) > MANUAL_OVERRIDE ? node.yaw() : yaw,
@@ -108,6 +278,26 @@ final class CursorPilot {
node.throttle(), node.autoTrim(), node.storedTrim(), node.flapSetting());
}
/**
* Remembers which way each input turns the craft, from the measured torque per unit of input: + nose up for pitch,
* nose right for yaw. Kept from the last time it was measurable, since with no airflow nothing can be measured.
*/
private static void learnSigns(AeroState.NodeState state, double[] f, double[] r, double[] u) {
state.pitchSign = sign(state.pitchResponse, r, 1.0, state.pitchSign);
state.yawSign = sign(state.yawResponse, u, -1.0, state.yawSign);
state.rollSign = sign(state.rollResponse, f, 1.0, state.rollSign);
}
private static double sign(Vector3d response, double[] axis, double direction, double previous) {
double length = response.length();
if (!(length > 1.0E-6)) {
return previous;
}
double along = direction * (response.x * axis[0] + response.y * axis[1] + response.z * axis[2]) / length;
// Only trusted while the input clearly turns the craft about that axis
return Math.abs(along) > 0.3 ? Math.signum(along) : previous;
}
/** Least squares for the three inputs whose responses add up to the torque, ridge-regularized so unused axes stay at zero. */
private static double[] solve(Vector3d pitch, Vector3d yaw, Vector3d roll, Vector3d torque) {
Matrix3d responses = new Matrix3d(pitch, yaw, roll);
@@ -129,6 +319,11 @@ final class CursorPilot {
return new double[]{x.x, x.y, x.z};
}
/** The angle brought into -pi to pi. */
private static double wrap(double angle) {
return Math.atan2(Math.sin(angle), Math.cos(angle));
}
private static double saturate(double value) {
return clamp(value, -1.0, 1.0);
}
@@ -0,0 +1,145 @@
package dev.lumareth.improvedAerodynamics.flight;
import dev.lumareth.improvedAerodynamics.Config;
import dev.lumareth.improvedAerodynamics.network.DeflectionPacket;
import dev.lumareth.improvedAerodynamics.network.Network;
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ShortOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.server.ServerStoppedEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.network.PacketDistributor;
import org.joml.Vector3d;
import org.valkyrienskies.core.api.ships.Ship;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import java.util.HashMap;
import java.util.Map;
/**
* Tells the clients near a ship how far its control surfaces and flaps are deflected, so they can show it, see
* {@code SurfaceAnimation}. Every tick each Warium control surface block reports its deflection from the procedure that
* runs it ({@link WariumCapture#begin}); at the end of the tick what changed goes out, in one packet per ship, and
* everything again once a second so players who arrive later get it too.
* <p>
* The deflection is what the physics uses: the smoothed pilot input plus trim and flap angle of the surface's control
* node. While the ship is not flying (no physics, nothing is moving) the node's raw input and flap lever are used, and the
* flaps are moved towards their angle here, at the configured speed.
*/
public final class DeflectionSync {
/** Degrees of deflection the physics gives a surface at full input, see ImprovedAero.MAX_DEFLECTION. */
private static final double FULL_INPUT_DEGREES = 20.0;
private static final long RESEND_TICKS = 20;
private static final double RANGE = 256.0;
private record Batch(ServerLevel level, double x, double y, double z, LongArrayList positions, ShortArrayList tenths) {
}
private static final Map<ResourceKey<Level>, Long2ShortOpenHashMap> LAST_VALUE = new HashMap<>();
private static final Map<ResourceKey<Level>, Long2LongOpenHashMap> LAST_SENT = new HashMap<>();
// Flap angle (radians) shown while a node is not flying, and the tick it was last moved
private static final Map<Long, double[]> STATIC_FLAPS = new HashMap<>();
private static final Map<Long, Batch> BATCHES = new HashMap<>();
private DeflectionSync() {
}
/** Called by the procedure of every control surface block each tick, before it adds its forces. */
static void report(ServerLevel level, BlockPos pos, SurfaceType type) {
if (!type.controllable) {
return;
}
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity == null) {
return;
}
CompoundTag tag = blockEntity.getPersistentData();
BlockPos nodePos = BlockPos.containing(tag.getDouble("ControlX"), tag.getDouble("ControlY"), tag.getDouble("ControlZ"));
BlockEntity nodeEntity = level.getBlockEntity(nodePos);
if (nodeEntity == null || !tag.contains("ControlX")) {
return;
}
long nodeKey = nodePos.asLong();
CompoundTag nodeTag = nodeEntity.getPersistentData();
AeroState.NodeState state = LiveInputs.flying(nodeKey);
boolean flying = state != null;
double input = axisInput(nodeKey, nodeTag, tag.getString("Key")) + axisInput(nodeKey, nodeTag, tag.getString("Key2"));
double degrees = FULL_INPUT_DEGREES * Math.max(-1.0, Math.min(1.0, input));
if (type == SurfaceType.CONTROL_SURFACE && tag.getBoolean(WariumCapture.FLAP_TAG)) {
degrees += Math.toDegrees(flying ? state.flapAngle : staticFlap(level, nodeKey, nodeTag));
}
short tenths = (short) Math.round(Math.max(-90.0, Math.min(90.0, degrees)) * 10.0);
ResourceKey<Level> dimension = level.dimension();
Long2ShortOpenHashMap lastValue = LAST_VALUE.computeIfAbsent(dimension, key -> new Long2ShortOpenHashMap());
Long2LongOpenHashMap lastSent = LAST_SENT.computeIfAbsent(dimension, key -> new Long2LongOpenHashMap());
long posKey = pos.asLong();
long now = level.getGameTime();
if (lastSent.containsKey(posKey) && lastValue.get(posKey) == tenths && now - lastSent.get(posKey) < RESEND_TICKS) {
return;
}
lastValue.put(posKey, tenths);
lastSent.put(posKey, now);
Ship ship = VSGameUtilsKt.getShipManagingPos(level, pos);
long shipId = ship != null ? ship.getId() : Long.MIN_VALUE;
Batch batch = BATCHES.get(shipId);
if (batch == null) {
Vector3d world = new Vector3d(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5);
if (ship != null) {
ship.getShipToWorld().transformPosition(world);
}
batch = new Batch(level, world.x, world.y, world.z, new LongArrayList(), new ShortArrayList());
BATCHES.put(shipId, batch);
}
batch.positions.add(posKey);
batch.tenths.add(tenths);
}
/** One key's share of a surface's input, like Warium reads "Pitch+" / "Roll-". Throttle keys do not deflect it. */
private static double axisInput(long nodeKey, CompoundTag nodeTag, String key) {
return ControlAxis.of(key) == ControlAxis.THROTTLE ? 0.0 : LiveInputs.key(nodeKey, nodeTag, key);
}
/** Flap angle of a node that is not flying: moves towards the lever's angle, once per tick however many flaps ask. */
private static double staticFlap(ServerLevel level, long nodeKey, CompoundTag nodeTag) {
double target = Math.toRadians(FlapSetting.of(nodeTag.getInt(WariumCapture.FLAP_SETTING_TAG)).degrees());
double[] entry = STATIC_FLAPS.computeIfAbsent(nodeKey, key -> new double[]{target, Double.NaN});
long now = level.getGameTime();
if (entry[1] != now) {
entry[1] = now;
entry[0] = AeroState.moveTowards(entry[0], target, Math.toRadians(Config.FLAP_SLEW_DEGREES.get()) / 20.0);
}
return entry[0];
}
@SubscribeEvent
public static void onServerTick(TickEvent.ServerTickEvent event) {
if (event.phase != TickEvent.Phase.END || BATCHES.isEmpty()) {
return;
}
for (Batch batch : BATCHES.values()) {
DeflectionPacket packet = new DeflectionPacket(batch.positions.toLongArray(), batch.tenths.toShortArray());
Network.CHANNEL.send(PacketDistributor.NEAR.with(() -> new PacketDistributor.TargetPoint(batch.x, batch.y, batch.z,
RANGE, batch.level.dimension())), packet);
}
BATCHES.clear();
}
@SubscribeEvent
public static void onServerStopped(ServerStoppedEvent event) {
BATCHES.clear();
LAST_VALUE.clear();
LAST_SENT.clear();
STATIC_FLAPS.clear();
}
}
@@ -38,6 +38,12 @@ final class ImprovedAero {
private static final double REFERENCE_SPEED = 40.0;
// Oswald efficiency of a finite wing
private static final double EFFICIENCY = 0.85;
// Airfoil efficiency in the DATCOM lift slope formula
private static final double AIRFOIL_EFFICIENCY = 0.95;
// How far sweep moves the stall angle between root and tip: at 45 degrees of sweep the tip of an aft swept wing
// stalls about 18% earlier than the middle of the half wing and the root as much later, the other way round when
// swept forward
private static final double SWEEP_STALL_SPREAD = 0.25;
private ImprovedAero() {
}
@@ -52,12 +58,13 @@ final class ImprovedAero {
/** The config values the model reads, taken once per evaluation. */
record Tuning(double liftScale, double stallAngleScale, double thinDrag,
boolean transonic, double speedOfSound, double transonicPeakDrag, double supersonicDrag,
boolean vortex, double vortexStallBonus, double vortexLiftStrength) {
boolean vortex, double vortexStallBonus, double vortexLiftStrength, boolean sweep) {
static Tuning read() {
return new Tuning(Config.LIFT_SCALE.get(), Config.STALL_ANGLE_SCALE.get(), Config.STABILIZER_DRAG.get(),
Config.TRANSONIC_DRAG.get(), Config.SPEED_OF_SOUND.get(), Config.TRANSONIC_PEAK_DRAG.get(),
Config.SUPERSONIC_DRAG.get(),
Config.VORTEX_LIFT.get(), Config.VORTEX_STALL_BONUS.get(), Config.VORTEX_LIFT_STRENGTH.get());
Config.VORTEX_LIFT.get(), Config.VORTEX_STALL_BONUS.get(), Config.VORTEX_LIFT_STRENGTH.get(),
Config.SWEEP_EFFECTS.get());
}
}
@@ -93,8 +100,12 @@ final class ImprovedAero {
// Warium's own per-block drag, lower for thin surfaces since they stabilize with lift, not drag. Shock waves
// multiply it near the speed of sound; this is the only zero-lift drag a craft has, induced and stall drag
// below are not affected
// Of the airflow only the part across the leading edge counts for the shock waves, so a swept wing's drag rise
// comes at a higher speed
double aftSweep = aftSweep(s, vx * s.facingX() + vz * s.facingZ(), tuning);
double shockDrag = tuning.transonic()
? TransonicDrag.multiplier(localSpeed / tuning.speedOfSound(), tuning.transonicPeakDrag(), tuning.supersonicDrag())
? TransonicDrag.multiplier(localSpeed * Math.cos(aftSweep) / tuning.speedOfSound(), tuning.transonicPeakDrag(),
tuning.supersonicDrag())
: 1.0;
double parasitic = -localSpeed * (s.type().thin ? tuning.thinDrag() : s.type().parasiticDrag) * shockDrag * p / m;
out.addForce(r, vx * parasitic, vy * parasitic, vz * parasitic);
@@ -129,11 +140,8 @@ final class ImprovedAero {
// Low aspect ratio wings keep their flow attached longer thanks to leading-edge vortices. Control surfaces
// in a wing's group stall with the wing instead of before it
double vortexFactor = tuning.vortex() && (s.type() == SurfaceType.WING || s.wingGroup() && s.type().controllable)
? VortexLift.factor(s.aspectRatio()) : 0.0;
double baseStall = s.slab() ? SurfaceType.SLAB_STALL_ANGLE
: s.wingGroup() && s.type().controllable ? SurfaceType.WING.stallAngle : s.type().stallAngle;
double critical = Math.toRadians(baseStall) * (1.0 + tuning.vortexStallBonus() * vortexFactor) * tuning.stallAngleScale();
double vortexFactor = vortexFactor(s, tuning);
double critical = criticalAngle(s, tuning, vortexFactor, aftSweep);
double absAlpha = Math.abs(alpha);
if (alpha >= 0.0) {
critical += FLAP_STALL_GAIN * flapShift;
@@ -149,9 +157,8 @@ final class ImprovedAero {
// Finite wing theory: lift slope and induced drag follow from the wing's aspect ratio
double aspectRatio = s.aspectRatio();
double slope = aspectRatio > 0.0 ? 2.0 * Math.PI / (1.0 + 2.0 / aspectRatio) : LIFT_SLOPE;
double inducedDrag = aspectRatio > 0.0 ? 1.0 / (Math.PI * EFFICIENCY * aspectRatio) : INDUCED_DRAG;
double attachedLift = slope * alpha;
double attachedLift = liftSlope(s, aftSweep) * alpha;
if (s.type() == SurfaceType.WING) {
attachedLift += VortexLift.liftCoefficient(alpha, vortexFactor, tuning.vortexLiftStrength());
}
@@ -175,6 +182,99 @@ final class ImprovedAero {
return stalled;
}
/**
* Lift (along the surface's normal) the surface would make in the airflow it sees now at the highest angle of
* attack it can fly without starting to stall, flaps included. Only meaningful for horizontal surfaces.
*/
static double maxLift(AeroSurface s, double[] v, double[] w, double[] r, double flap, double weight, Tuning tuning) {
double vx = v[0] + w[1] * r[2] - w[2] * r[1];
double vy = v[1] + w[2] * r[0] - w[0] * r[2];
double vz = v[2] + w[0] * r[1] - w[1] * r[0];
double u = vx * s.facingX() + vz * s.facingZ();
double planeSpeedSq = u * u + vy * vy;
double aftSweep = aftSweep(s, u, tuning);
double vortexFactor = vortexFactor(s, tuning);
double flapShift = FLAP_ALPHA_FACTOR * flap;
double alpha = criticalAngle(s, tuning, vortexFactor, aftSweep) + FLAP_STALL_GAIN * flapShift - STALL_BLEND;
double lift = liftSlope(s, aftSweep) * alpha;
if (s.type() == SurfaceType.WING) {
lift += VortexLift.liftCoefficient(alpha, vortexFactor, tuning.vortexLiftStrength());
}
return LIFT_CONSTANT * s.pressure() * weight * s.fluidFactor() * planeSpeedSq * lift * tuning.liftScale();
}
/**
* How close the surface flies to its stall: its angle of attack (camber and flaps included, positive pulling) over
* the angle at which it starts to stall, so 1 is the edge of the stall. Only meaningful for horizontal surfaces,
* NaN without airflow in the surface's plane.
*/
static double stallRatio(AeroSurface s, double[] v, double[] w, double[] r, double flap, Tuning tuning) {
double vx = v[0] + w[1] * r[2] - w[2] * r[1];
double vy = v[1] + w[2] * r[0] - w[0] * r[2];
double vz = v[2] + w[0] * r[1] - w[1] * r[0];
double u = vx * s.facingX() + vz * s.facingZ();
if (u * u + vy * vy < 1.0E-4) {
return Double.NaN;
}
double flapShift = FLAP_ALPHA_FACTOR * flap;
double alpha = Math.atan2(-vy, Math.abs(u)) + Math.toRadians(s.type().camber) + flapShift;
double critical = criticalAngle(s, tuning, vortexFactor(s, tuning), aftSweep(s, u, tuning)) + FLAP_STALL_GAIN * flapShift;
return alpha / critical;
}
private static double vortexFactor(AeroSurface s, Tuning tuning) {
return tuning.vortex() && (s.type() == SurfaceType.WING || s.wingGroup() && s.type().controllable)
? VortexLift.factor(s.aspectRatio()) : 0.0;
}
/**
* Angle of attack (radians, camber and flaps included) at which the surface starts to stall, without flaps. On a
* swept wing the air drifts along the span towards whichever end lies aft and thickens the boundary layer there, so
* that end stalls first: the tips of an aft swept wing, the root of a forward swept one.
*/
private static double criticalAngle(AeroSurface s, Tuning tuning, double vortexFactor, double aftSweep) {
// Slab and full block variants stall alike: which one a builder picks is about looks, not airfoil thickness
double baseStall = s.wingGroup() && s.type().controllable ? SurfaceType.WING.stallAngle : s.type().stallAngle;
double spanwise = 1.0 - SWEEP_STALL_SPREAD * Math.sin(aftSweep) * (2.0 * s.spanFraction() - 1.0);
return Math.toRadians(baseStall) * (1.0 + tuning.vortexStallBonus() * vortexFactor) * tuning.stallAngleScale() * spanwise;
}
/**
* Lift slope per radian from finite wing theory, from the wing's aspect ratio, lowered by sweep as the DATCOM
* formula has it. Scaled so an unswept wing keeps the slope it always had.
*/
private static double liftSlope(AeroSurface s, double aftSweep) {
double aspectRatio = s.aspectRatio();
if (!(aspectRatio > 0.0)) {
return LIFT_SLOPE * Math.cos(aftSweep);
}
double slope = 2.0 * Math.PI / (1.0 + 2.0 / aspectRatio);
if (aftSweep == 0.0) {
return slope;
}
return slope * datcomSlope(aspectRatio, aftSweep) / datcomSlope(aspectRatio, 0.0);
}
/** DATCOM lift slope of a swept wing, per radian. */
private static double datcomSlope(double aspectRatio, double sweep) {
double tan = Math.tan(sweep);
double root = Math.sqrt(4.0 + aspectRatio * aspectRatio * (1.0 + tan * tan) / (AIRFOIL_EFFICIENCY * AIRFOIL_EFFICIENCY));
return 2.0 * Math.PI * aspectRatio / (2.0 + root);
}
/**
* The surface's sweep as the air sees it, radians: positive swept aft (tips behind the root), negative swept forward,
* 0 with sweep effects off. Warium's surfaces work with the air coming from either end of the chord, so which of the
* two a wing is follows from the direction of the flow along its facing.
*/
private static double aftSweep(AeroSurface s, double u, Tuning tuning) {
if (!tuning.sweep() || s.sweep() == 0.0 || u == 0.0) {
return 0.0;
}
// Moving along +facing (u > 0), a tip further towards +facing (sweep > 0) leads the root: forward sweep
return -s.sweep() * Math.signum(u);
}
static double smoothstep(double edge0, double edge1, double x) {
double t = Math.max(0.0, Math.min(1.0, (x - edge0) / (edge1 - edge0)));
return t * t * (3.0 - 2.0 * t);
@@ -0,0 +1,44 @@
package dev.lumareth.improvedAerodynamics.flight;
import net.minecraft.nbt.CompoundTag;
/**
* The pilot inputs a control node's devices should follow, for everything that is not an aerodynamic surface (thrust
* vectoring nozzles). While the node's ship is flying these are the inputs the flight physics uses: the pilot's keys
* smoothed, or what cursor aim asks for in their place, plus the flight computer's pitch trim. A node without physics
* (on the ground, nothing moving, physics rate aerodynamics off) gives Warium's raw key values, as before.
*/
public final class LiveInputs {
// A node state the physics thread has not touched for this long is not flying any more
private static final long FRESH_MILLIS = 500;
private LiveInputs() {
}
/** The node's physics state if its ship is flying now, else null. */
static AeroState.NodeState flying(long nodeKey) {
AeroState.NodeState state = AeroState.nodeState(nodeKey);
return state != null && System.currentTimeMillis() - state.updatedMillis < FRESH_MILLIS ? state : null;
}
/**
* One axis of a node, in Warium's node units (-1 to 1), without any key multiplier.
*
* @param nodeTag the node block entity's persistent data, for the raw value
*/
public static double value(long nodeKey, CompoundTag nodeTag, ControlAxis axis) {
AeroState.NodeState state = flying(nodeKey);
return switch (axis) {
case PITCH -> state != null ? state.pitch + state.trimOutput : nodeTag.getDouble("Pitch");
case YAW -> state != null ? state.yaw : nodeTag.getDouble("Yaw");
case ROLL -> state != null ? state.roll : nodeTag.getDouble("Roll");
case THROTTLE -> nodeTag.getDouble("Throttle");
case NONE -> 0.0;
};
}
/** A Warium key such as "Pitch+" or "Roll-" read from the node: its axis' value times the key's sign. */
public static double key(long nodeKey, CompoundTag nodeTag, String key) {
return value(nodeKey, nodeTag, ControlAxis.of(key)) * ControlAxis.multiplier(key);
}
}
@@ -23,11 +23,13 @@ public final class MixinPlugin implements IMixinConfigPlugin {
private boolean warium;
private boolean futurism;
private boolean geckolib;
@Override
public void onLoad(String mixinPackage) {
warium = supported("valkyrien_warium", WARIUM_VERSION, "Warium VS");
futurism = supported("futurism", FUTURISM_VERSION, "Futurism");
geckolib = LoadingModList.get().getModFileById("geckolib") != null;
}
private static boolean supported(String modId, String version, String name) {
@@ -49,6 +51,10 @@ public final class MixinPlugin implements IMixinConfigPlugin {
if (mixinClassName.contains(".mixin.warium.")) {
return warium;
}
if (mixinClassName.contains(".mixin.gecko.")) {
// Animates Warium's control surfaces, which are GeckoLib models
return warium && geckolib;
}
if (mixinClassName.contains(".mixin.futurism.")) {
return futurism;
}
@@ -15,9 +15,6 @@ public enum SurfaceType {
VERTICAL_CONTROL_SURFACE(true, true, false, 15.0, 0.0, 0.0),
AIR_BRAKE(false, false, false, 0.0, 0.0, 0.0);
// Slab variants are thinner and stall earlier
static final double SLAB_STALL_ANGLE = 12.0;
/** Surface stands upright, so its lift acts sideways. */
final boolean vertical;
/** Deflects with pilot input. */
@@ -50,6 +50,9 @@ public final class WariumCapture {
world = level;
pos = BlockPos.containing(x, y, z);
state = blockState;
if (surfaceType.controllable && level instanceof ServerLevel serverLevel && Config.PHYSICS_RATE_AERODYNAMICS.get()) {
DeflectionSync.report(serverLevel, pos, surfaceType);
}
}
/**
@@ -70,9 +73,8 @@ public final class WariumCapture {
}
private static AeroSurface createSurface(ServerLevel level, AeroState aero) {
Direction facing = facing(state);
ResourceLocation id = ForgeRegistries.BLOCKS.getKey(state.getBlock());
boolean slab = id != null && id.getPath().contains("slab");
Direction direction = facing(state);
Direction facing = direction != null ? direction : Direction.NORTH;
double speedMultiplier = Math.max(1, level.getGameRules().getInt(ValkyrienWariumModGameRules.AERODYNAMIC_SPEED_MULTIPLIER) / 100);
double pressure = PressureGetProcedure.execute(level, pos.getX(), pos.getY(), pos.getZ());
@@ -99,10 +101,10 @@ public final class WariumCapture {
}
}
return new AeroSurface(pos.asLong(), type, slab, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
facing.getStepX(), facing.getStepZ(), pressure, fluidFactor(level), speedMultiplier,
return new AeroSurface(pos.asLong(), type, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5,
facing.getStepX(), facing.getStepZ(), direction != null, pressure, fluidFactor(level), speedMultiplier,
node, ControlAxis.of(key), ControlAxis.multiplier(key), ControlAxis.of(key2), ControlAxis.multiplier(key2),
brakeInput, 0.0, false, flap);
brakeInput, 0.0, false, flap, 0.0, 0.0);
}
/** Registers a node's inputs once per tick, and saves its trim to it now and then so it survives reloading. */
@@ -158,16 +160,28 @@ public final class WariumCapture {
return inLiquid ? underWater : 1.0;
}
/** Warium's way of reading a block's direction: its "facing", else the positive end of its "axis", else north. */
/**
* The block's chord direction: its "facing" (Warium's way), or "orientation" (Warframes' slab wings), else the
* positive end of its "axis". Null if it has none that lies horizontal, as on Warframes' plain slab wings and
* stabilizers, or only one that says which side the block sits on, as on Warframes' layer blocks. Warium's own lift
* ignores direction, so such blocks get the direction the rest of the craft's surfaces agree on, see WingShape.
*/
private static Direction facing(BlockState blockState) {
Property<?> facing = blockState.getBlock().getStateDefinition().getProperty("facing");
if (facing instanceof DirectionProperty directionProperty) {
return blockState.getValue(directionProperty);
java.util.Collection<Property<?>> properties = blockState.getBlock().getStateDefinition().getProperties();
ResourceLocation id = ForgeRegistries.BLOCKS.getKey(blockState.getBlock());
if (id != null && id.getPath().contains("_layer")) {
return null;
}
for (String name : new String[]{"facing", "orientation"}) {
Property<?> property = blockState.getBlock().getStateDefinition().getProperty(name);
if (property != null && properties.contains(property) && blockState.getValue(property) instanceof Direction value) {
return value.getAxis().isHorizontal() ? value : null;
}
}
Property<?> axis = blockState.getBlock().getStateDefinition().getProperty("axis");
if (axis instanceof EnumProperty<?> enumProperty && blockState.getValue(enumProperty) instanceof Direction.Axis value) {
return Direction.fromAxisAndDirection(value, Direction.AxisDirection.POSITIVE);
return value.isHorizontal() ? Direction.fromAxisAndDirection(value, Direction.AxisDirection.POSITIVE) : null;
}
return Direction.NORTH;
return null;
}
}
@@ -10,12 +10,21 @@ import java.util.Collection;
* <p>
* Neighbouring blocks of the same kind and facing form one wing. Horizontal wings also join across gaps of up to
* {@link #MAX_GAP} blocks along the span, so the two halves of a wing either side of a fuselage count as one wing and
* the span is the full wingspan. Air brakes are not wings and are left alone.
* the span is the full wingspan. Blocks that only touch at an edge on the same level join too, so a swept wing built
* as a diagonal counts as one wing. Air brakes are not wings and are left alone.
* <p>
* Horizontal wings also get their sweep: for each half (either side of the middle of the span) the line through its
* blocks' chordwise positions against their distance from the middle, whose slope is the tangent of the sweep. And every
* block learns how far out along its half it sits, which decides where a swept wing starts to stall.
*/
final class WingShape {
private static final int MAX_GAP = 6;
private static final double MIN_ASPECT_RATIO = 0.5;
private static final double MAX_ASPECT_RATIO = 12.0;
private static final double MAX_SWEEP = Math.toRadians(60.0);
// A half wing needs this many blocks and this much spread along the span before a sweep is read from it
private static final int MIN_SWEEP_BLOCKS = 3;
private static final double MIN_SPAN_VARIANCE = 0.5;
private WingShape() {
}
@@ -23,6 +32,23 @@ final class WingShape {
static AeroSurface[] withAspectRatios(Collection<AeroSurface> surfaces) {
AeroSurface[] all = surfaces.toArray(new AeroSurface[0]);
int n = all.length;
// Blocks that do not say their direction take the axis most of the craft's surfaces face along. The sign does
// not matter: lift works with the air coming from either end, and sweep is judged from the flow
int alongX = 0;
int alongZ = 0;
for (AeroSurface s : all) {
if (s.facingKnown() && s.type() != SurfaceType.AIR_BRAKE) {
alongX += Math.abs(s.facingX());
alongZ += Math.abs(s.facingZ());
}
}
if (alongX + alongZ > 0) {
for (int i = 0; i < n; i++) {
if (!all[i].facingKnown()) {
all[i] = all[i].withFacingAxis(alongX >= alongZ);
}
}
}
int[] cell = new int[n * 3];
Long2IntOpenHashMap index = new Long2IntOpenHashMap(n * 2);
index.defaultReturnValue(-1);
@@ -52,6 +78,12 @@ final class WingShape {
}
// Gaps along the span of horizontal wings, only in one direction since the other side finds this block
if (!s.type().vertical) {
// Blocks touching only at an edge on the same level, a diagonal wing
for (int a = -1; a <= 1; a += 2) {
for (int b = -1; b <= 1; b += 2) {
union(parent, i, index.get(key(s, x + s.rightX() * a + s.facingX() * b, y, z + s.rightZ() * a + s.facingZ() * b)));
}
}
for (int gap = 2; gap <= MAX_GAP + 1; gap++) {
union(parent, i, index.get(key(s, x + s.rightX() * gap, y, z + s.rightZ() * gap)));
}
@@ -75,6 +107,28 @@ final class WingShape {
minSpan[root] = Math.min(minSpan[root], span);
maxSpan[root] = Math.max(maxSpan[root], span);
}
// Sweep: per wing and side of its middle, least squares sums of the distance from the middle (o) and the
// position along the chord axis (c, the positive x or z axis)
double[] sums = new double[n * 2 * 5];
for (int i = 0; i < n; i++) {
if (all[i].type() == SurfaceType.AIR_BRAKE || all[i].type().vertical) {
continue;
}
int root = find(parent, i);
double offset = spanCoordinate(all[i], cell, i) - (minSpan[root] + maxSpan[root]) / 2.0;
if (offset == 0.0) {
continue;
}
int at = (root * 2 + (offset > 0.0 ? 1 : 0)) * 5;
double o = Math.abs(offset);
double c = chordCoordinate(all[i], cell, i);
sums[at] += 1.0;
sums[at + 1] += o;
sums[at + 2] += c;
sums[at + 3] += o * o;
sums[at + 4] += o * c;
}
for (int i = 0; i < n; i++) {
if (all[i].type() == SurfaceType.AIR_BRAKE) {
continue;
@@ -82,11 +136,50 @@ final class WingShape {
int root = find(parent, i);
double span = maxSpan[root] - minSpan[root] + 1;
double aspectRatio = span * span / count[root];
all[i] = all[i].withWingShape(Math.max(MIN_ASPECT_RATIO, Math.min(MAX_ASPECT_RATIO, aspectRatio)), hasWing[root]);
double sweep = 0.0;
double spanFraction = 0.0;
if (!all[i].type().vertical) {
double offset = spanCoordinate(all[i], cell, i) - (minSpan[root] + maxSpan[root]) / 2.0;
double halfSpan = (maxSpan[root] - minSpan[root]) / 2.0;
spanFraction = halfSpan > 0.0 ? Math.abs(offset) / halfSpan : 0.0;
// Blocks on the middle line take the sweep of the side with more blocks
int side = offset > 0.0 ? 1 : offset < 0.0 ? 0 : sums[(root * 2 + 1) * 5] >= sums[root * 2 * 5] ? 1 : 0;
double axisSweep = halfSweep(sums, (root * 2 + side) * 5);
// Measured along the positive axis, stored along the block's own facing
sweep = axisSweep * (all[i].facingX() + all[i].facingZ());
}
all[i] = all[i].withWingShape(Math.max(MIN_ASPECT_RATIO, Math.min(MAX_ASPECT_RATIO, aspectRatio)), hasWing[root],
sweep, spanFraction);
}
return all;
}
/** Sweep of one half wing from its sums, radians, positive when the tip lies towards the positive chord axis. */
private static double halfSweep(double[] sums, int at) {
double count = sums[at];
if (count < MIN_SWEEP_BLOCKS) {
return 0.0;
}
double meanO = sums[at + 1] / count;
double meanC = sums[at + 2] / count;
double variance = sums[at + 3] / count - meanO * meanO;
if (variance < MIN_SPAN_VARIANCE) {
return 0.0;
}
double slope = (sums[at + 4] / count - meanO * meanC) / variance;
return Math.max(-MAX_SWEEP, Math.min(MAX_SWEEP, Math.atan(slope)));
}
/** Position along the span axis of a horizontal block, the same coordinate the span is measured in. */
private static double spanCoordinate(AeroSurface s, int[] cell, int i) {
return cell[i * 3] * Math.abs(s.rightX()) + cell[i * 3 + 2] * Math.abs(s.rightZ());
}
/** Position along the chord axis (the positive x or z axis the facing lies on). */
private static double chordCoordinate(AeroSurface s, int[] cell, int i) {
return cell[i * 3] * Math.abs(s.facingX()) + cell[i * 3 + 2] * Math.abs(s.facingZ());
}
/** Position plus everything that must match for two blocks to be the same wing. */
private static long key(AeroSurface s, int x, int y, int z) {
long kind = (s.type().vertical ? 2 : 0) | (s.facingX() != 0 ? 1 : 0);
@@ -0,0 +1,55 @@
package dev.lumareth.improvedAerodynamics.flight.mixin.futurism;
import dev.lumareth.improvedAerodynamics.flight.LiveInputs;
import net.mcreator.futurism.procedures.VectoredAfterburnerSystemProcedure;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
/**
* Futurism's vectored afterburner reads the raw key values of its control node, so it ignored cursor aim and the flight
* computer. This hands it the same inputs the control surfaces follow ({@link LiveInputs}), through the same Key and Key2
* it is configured with. It stays what Futurism made it, 2D vectoring: the procedure still only tilts the thrust up or
* down. The value is kept on Warium's 0.2 key steps, since every change of it plays Futurism's servo sound and sends a
* block update.
*/
@Mixin(value = VectoredAfterburnerSystemProcedure.class, remap = false)
public abstract class VectoredAfterburnerSystemProcedureMixin {
private static final double STEP = 0.2;
@ModifyVariable(method = "execute", at = @At("STORE"), name = "input")
private static double improvedAerodynamics$liveInput(double input, LevelAccessor world, double x, double y, double z, BlockState blockstate) {
if (!(world instanceof ServerLevel level)) {
return input;
}
BlockEntity own = level.getBlockEntity(BlockPos.containing(x, y, z));
if (own == null) {
return input;
}
CompoundTag tag = own.getPersistentData();
BlockPos nodePos = BlockPos.containing(tag.getDouble("ControlX"), tag.getDouble("ControlY"), tag.getDouble("ControlZ"));
BlockEntity node = level.getBlockEntity(nodePos);
if (node == null) {
return input;
}
long nodeKey = nodePos.asLong();
CompoundTag nodeTag = node.getPersistentData();
double live = keyInput(nodeKey, nodeTag, tag.getString("Key")) + keyInput(nodeKey, nodeTag, tag.getString("Key2"));
live = Math.max(-1.0, Math.min(1.0, live));
return Math.round(live / STEP) * STEP;
}
/** Like Futurism reads a key: pitch, yaw or roll with its sign, anything else (Throttle, Null) adds nothing. */
private static double keyInput(long nodeKey, CompoundTag nodeTag, String key) {
if (key.startsWith("Pitch") || key.startsWith("Yaw") || key.startsWith("Roll")) {
return LiveInputs.key(nodeKey, nodeTag, key);
}
return 0.0;
}
}
@@ -0,0 +1,25 @@
package dev.lumareth.improvedAerodynamics.flight.mixin.gecko;
import dev.lumareth.improvedAerodynamics.client.animation.SurfaceAnimation;
import net.minecraft.world.level.block.entity.BlockEntity;
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.callback.CallbackInfo;
import software.bernie.geckolib.core.animatable.GeoAnimatable;
import software.bernie.geckolib.core.animation.AnimationState;
import software.bernie.geckolib.model.GeoModel;
/**
* Runs after GeckoLib has played an animatable's animations, to put Warium's control surfaces at the deflection the
* flight physics gives them, see {@link SurfaceAnimation}.
*/
@Mixin(value = GeoModel.class, remap = false)
public abstract class GeoModelMixin {
@Inject(method = "handleAnimations", at = @At("RETURN"))
private void improvedAerodynamics$deflectSurface(GeoAnimatable animatable, long instanceId, AnimationState animationState, CallbackInfo ci) {
if (animatable instanceof BlockEntity blockEntity) {
SurfaceAnimation.apply(blockEntity, (GeoModel<?>) (Object) this);
}
}
}
@@ -0,0 +1,50 @@
package dev.lumareth.improvedAerodynamics.network;
import dev.lumareth.improvedAerodynamics.client.animation.SurfaceAnimation;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;
import java.util.function.Supplier;
/** How far control surfaces are deflected, for the clients near a ship: block positions and tenths of a degree. */
public final class DeflectionPacket {
private final long[] positions;
private final short[] tenths;
public DeflectionPacket(long[] positions, short[] tenths) {
this.positions = positions;
this.tenths = tenths;
}
public void encode(FriendlyByteBuf buffer) {
buffer.writeVarInt(positions.length);
for (int i = 0; i < positions.length; i++) {
buffer.writeLong(positions[i]);
buffer.writeShort(tenths[i]);
}
}
public static DeflectionPacket decode(FriendlyByteBuf buffer) {
int count = buffer.readVarInt();
long[] positions = new long[count];
short[] tenths = new short[count];
for (int i = 0; i < count; i++) {
positions[i] = buffer.readLong();
tenths[i] = buffer.readShort();
}
return new DeflectionPacket(positions, tenths);
}
public void handle(Supplier<NetworkEvent.Context> context) {
long[] pos = positions;
short[] values = tenths;
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
for (int i = 0; i < pos.length; i++) {
SurfaceAnimation.set(pos[i], values[i] / 10.0F);
}
});
context.get().setPacketHandled(true);
}
}
@@ -44,6 +44,11 @@ public final class Network {
.decoder(ControlModePacket::decode)
.consumerMainThread(ControlModePacket::handle)
.add();
CHANNEL.messageBuilder(DeflectionPacket.class, 11, NetworkDirection.PLAY_TO_CLIENT)
.encoder(DeflectionPacket::encode)
.decoder(DeflectionPacket::decode)
.consumerMainThread(DeflectionPacket::handle)
.add();
// Higher id than the neighbours so it does not collide with packets added on other branches
CHANNEL.messageBuilder(FlapPacket.class, 10, NetworkDirection.PLAY_TO_SERVER)
.encoder(FlapPacket::encode)
@@ -216,6 +216,18 @@
{
"id": "valkyrien_warium:green_control_surface_offset_top",
"required": false
},
{
"id": "warframes:airframe_control_surface",
"required": false
},
{
"id": "warframes:airframe_half_control_surface",
"required": false
},
{
"id": "warframes:airframe_stepped_control_surface",
"required": false
}
]
}
}
@@ -6,6 +6,8 @@
"compatibilityLevel": "JAVA_17",
"mixins": [
"futurism.ForceInducedShipsMixin",
"futurism.VectoredAfterburnerSystemProcedureMixin",
"gecko.GeoModelMixin",
"warium.AirBrakeTickProcedureMixin",
"warium.BypassedCompressorScriptProcedureMixin",
"warium.ControlSurfaceTickProcedureMixin",