Skip to content

Commit 497de7c

Browse files
authored
Fix Drag Mode Angular Momentum [AARD-1966] (#1215)
2 parents e444448 + 9bd1f0e commit 497de7c

File tree

1 file changed

+17
-33
lines changed

1 file changed

+17
-33
lines changed

fission/src/systems/scene/DragModeSystem.ts

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import * as THREE from "three"
22
import WorldSystem from "../WorldSystem"
33
import World from "../World"
44
import JOLT from "@/util/loading/JoltSyncLoader"
5-
import { convertThreeVector3ToJoltVec3, convertJoltVec3ToThreeVector3 } from "@/util/TypeConversions"
5+
import { convertJoltVec3ToThreeVector3, convertThreeVector3ToJoltVec3 } from "@/util/TypeConversions"
66
import MirabufSceneObject, { RigidNodeAssociate } from "@/mirabuf/MirabufSceneObject"
77
import {
8-
InteractionStart,
9-
InteractionMove,
108
InteractionEnd,
9+
InteractionMove,
10+
InteractionStart,
1111
PRIMARY_MOUSE_INTERACTION,
1212
} from "./ScreenInteractionHandler"
1313
import { CustomOrbitControls, SphericalCoords } from "./CameraControls"
1414
import Jolt from "@azaleacolburn/jolt-physics"
1515
import { MiraType } from "@/mirabuf/MirabufLoader"
16+
import InputSystem from "@/systems/input/InputSystem.ts"
1617

1718
interface DragTarget {
1819
bodyId: Jolt.BodyID
@@ -59,6 +60,7 @@ class DragModeSystem extends WorldSystem {
5960
// Precision and sensitivity
6061
MINIMUM_DISTANCE_THRESHOLD: 0.02, // Minimum distance to apply forces (smaller = more precision)
6162
WHEEL_SCROLL_SENSITIVITY: -0.01, // Mouse wheel scroll sensitivity for Z-axis
63+
ROTATION_SPEED: 200.0, // speed of arrow key rotation. lower = more precise, higher = more
6264
} as const
6365

6466
private _enabled: boolean = false
@@ -564,25 +566,19 @@ class DragModeSystem extends WorldSystem {
564566
const joltForce = convertThreeVector3ToJoltVec3(forceNeeded)
565567
body.AddForce(joltForce)
566568

567-
// Calculate torque to simulate force applied at the drag point
568-
// Use the current world offset (which rotates with the body)
569-
const leverArm = currentWorldOffset // vector from COM to drag point in world coordinates
570-
const torque = leverArm.cross(forceNeeded) // Cross product gives us the torque
571-
const joltTorque = convertThreeVector3ToJoltVec3(torque)
572-
body.AddTorque(joltTorque)
573-
574-
// Reduce angular damping since we want the natural rotation from the applied force
575-
const angularVel = body.GetAngularVelocity()
576-
const angularDampingStrength = Math.min(
577-
mass * DragModeSystem.DRAG_FORCE_CONSTANTS.ANGULAR_DAMPING_BASE,
578-
DragModeSystem.DRAG_FORCE_CONSTANTS.ANGULAR_DAMPING_MAX
569+
const yawRotation = new JOLT.Vec3(
570+
0,
571+
DragModeSystem.DRAG_FORCE_CONSTANTS.ROTATION_SPEED *
572+
(InputSystem.isKeyPressed("ArrowRight") ? 1 : 0 - (InputSystem.isKeyPressed("ArrowLeft") ? 1 : 0)),
573+
0
579574
)
580-
const angularDampingTorque = new JOLT.Vec3(
581-
-angularVel.GetX() * angularDampingStrength,
582-
-angularVel.GetY() * angularDampingStrength,
583-
-angularVel.GetZ() * angularDampingStrength
575+
const cameraVector = World.sceneRenderer.mainCamera.getWorldDirection(new THREE.Vector3(0, 0, 0))
576+
const pitchRotation = new JOLT.Vec3(cameraVector.z, 0, -cameraVector.x).Mul(
577+
DragModeSystem.DRAG_FORCE_CONSTANTS.ROTATION_SPEED *
578+
(InputSystem.isKeyPressed("ArrowUp") ? 1 : 0 - (InputSystem.isKeyPressed("ArrowDown") ? 1 : 0))
584579
)
585-
body.AddTorque(angularDampingTorque)
580+
body.AddTorque(yawRotation)
581+
body.AddTorque(pitchRotation)
586582
} else {
587583
// When close to target, apply braking forces and gravity compensation
588584
const currentVel = body.GetLinearVelocity()
@@ -602,21 +598,9 @@ class DragModeSystem extends WorldSystem {
602598
const gravityCompensationY = mass * DragModeSystem.DRAG_FORCE_CONSTANTS.GRAVITY_MAGNITUDE
603599
brakingForce.SetY(brakingForce.GetY() + gravityCompensationY)
604600
}
605-
606601
body.AddForce(brakingForce)
607-
608-
const angularVel = body.GetAngularVelocity()
609-
const angularBrakingStrength = Math.min(
610-
mass * DragModeSystem.DRAG_FORCE_CONSTANTS.ANGULAR_BRAKING_BASE,
611-
DragModeSystem.DRAG_FORCE_CONSTANTS.ANGULAR_BRAKING_MAX
612-
)
613-
const angularBrakingTorque = new JOLT.Vec3(
614-
-angularVel.GetX() * angularBrakingStrength,
615-
-angularVel.GetY() * angularBrakingStrength,
616-
-angularVel.GetZ() * angularBrakingStrength
617-
)
618-
body.AddTorque(angularBrakingTorque)
619602
}
603+
body.SetAngularVelocity(new JOLT.Vec3())
620604
}
621605

622606
private handleWheelDuringDrag(event: WheelEvent): void {

0 commit comments

Comments
 (0)