Constraints
SwingTwist Constraint
Kombinuje eliptické swing limits, signed twist range, friction a dva samostatné native motory.
jolt_swing_twist() přímo zpřístupňuje SwingTwistConstraint z Jolt Physics 5.6. Drží dva attachment points pohromadě a řídí orientaci mezi články, končetinami, řetězy a dalšími klouby, pro které je Cone příliš jednoduchý.
Syntaxe#
jolt_swing_twist(body_a, body_b, anchor_x, anchor_y, anchor_z, options=undefined)Options#
| Pole | Výchozí | Popis |
|---|---|---|
| twist_axis | {x:0,y:0,z:1} | World-space constraint X axis, kolem které probíhá twist. |
| plane_axis | {x:1,y:0,z:0} | World-space constraint Z axis určující swing frame. |
| swing_angle | 45 | Alias ve stupních použitý pro oba swing half-angles, nejsou-li zadány samostatně. |
| normal_half_angle | swing_angle | Swing half-angle kolem constraint-space normal/Y, 0..180 stupňů. |
| plane_half_angle | swing_angle | Swing half-angle kolem constraint-space plane/Z, 0..180 stupňů. |
| twist_min | -30 | Inclusive signed minimum twist ve stupních, nejméně -180. |
| twist_max | 30 | Inclusive signed maximum twist ve stupních, nejvýše 180. |
| friction_torque | 0 | Nezáporný maximální pasivní friction torque v N m. |
| collision | false | Zda připojená tělesa mohou kolidovat. |
| enabled | true | Počáteční stav solveru. |
Referenční frame#
- Anchor i obě osy jsou při vytvoření world-space hodnoty.
- Twist Axis se stane constraint-space X. Plane Axis je constraint-space Z a Jolt odvodí normal/Y osu.
- Osy se normalizují a plane axis se ortogonalizuje kolem twist axis.
- Nulové nebo téměř rovnoběžné osy jsou odmítnuté.
- Odlišné normal a plane half-angles vytvářejí skutečný eliptický swing range.
Limity a stav#
| Metoda | Výsledek |
|---|---|
| set_limits(normal, plane, twist_min, twist_max) | Aktualizuje všechny čtyři limity ve stupních. |
| set_swing_limits(normal, plane) | Aktualizuje jen eliptické swing half-angles. |
| set_twist_limits(minimum, maximum) | Aktualizuje jen signed twist range. |
| get_limits() | Vrátí {normal_half_angle, plane_half_angle, twist_min, twist_max}. |
| set_friction_torque(value) / get_friction_torque() | Změní nebo přečte maximální pasivní friction torque. |
| get_twist_angle() | Vrátí aktuální signed twist ve stupních. |
| get_orientation() | Vrátí aktuální constraint-space quaternion {x,y,z,w}. |
| get_swing_twist_impulses() | Vrátí position, twist, swing a motor solver impulses. |
Samostatné motory#
| Swing | Twist |
|---|---|
| set_swing_motor_velocity(y_deg_s, z_deg_s) | set_twist_motor_velocity(deg_s) |
| set_swing_motor_position(angle_deg, axis_y=1, axis_z=0) | set_twist_motor_position(angle_deg) |
| enable_swing_motor(enabled) | enable_twist_motor(enabled) |
| set_swing_motor_max_torque(value) | set_twist_motor_max_torque(value) |
| set_swing_motor_spring(frequency, damping) | set_twist_motor_spring(frequency, damping) |
| get_swing_motor_state() | get_twist_motor_state() |
Swing velocity komponenty používají constraint-space Y/Z. Swing position je úhel kolem normalizované osy v této Y/Z rovině. Twist používá constraint-space X. Tyto high-level motor velocities jsou ve stupních za sekundu.
Příklad: kloubový článek#
var upper = jolt_box_create_dynamic(0, 0, 5, 0.5, 0.5, 1, { mass: 2 });
var lower = jolt_box_create_dynamic(0, 0, 4, 0.5, 0.5, 1, { mass: 2 });
var joint = jolt_swing_twist(upper, lower, 0, 0, 4.5, {
twist_axis: { x:0, y:0, z:1 },
plane_axis: { x:1, y:0, z:0 },
normal_half_angle: 20,
plane_half_angle: 60,
twist_min: -30,
twist_max: 30,
friction_torque: 0.5,
collision: false
});Příklad: twist motor#
joint.set_twist_motor_max_torque(20);
joint.set_twist_motor_spring(3, 0.8);
joint.set_twist_motor_position(25);
var twist = joint.get_twist_angle();
var state = joint.get_twist_motor_state();Životní cyklus, debug a omezení#
- is_valid() / get_native_handle(): Ověří wrapper nebo vrátí jeho neprůhledný native handle.
- destroy(): Idempotentně uvolní constraint bez zničení kteréhokoli tělesa.
- set_enabled() / get_enabled(): Změní účast v solveru a zachová resource.
- set_collision_enabled() / get_collision_enabled(): Řídí connected-body pair filter.
- Zničení kteréhokoli tělesa zneplatní generační constraint handle; jolt_stop() jej uklidí před tělesy a worldem.
- JOLT_DEBUG_CONSTRAINTS kreslí native frame, eliptickou hranici, twist arc, aktuální orientaci a aktivní motory.
- Creator podporuje pouze body-to-body Cone swing. Nemá world convenience variantu ani veřejný rope/chain helper.
- Solver impulses jsou Lagrange multipliers z posledního kroku, nikoli forces nebo torques za sekundu.
Ověřené kontrakty operací#
Následující kontrakty rozlišují jednotky, návratové hodnoty, chyby a životnost podle aktuální implementace. Nevynechávej kontrolu výsledku: false, 0 a undefined mohou mít u různých metod odlišný význam.
| Symbol / vlastník | Signatura | Kontrakt operace |
|---|---|---|
| jolt_swing_twist | jolt_swing_twist(body_a, body_b, anchor_x, anchor_y, anchor_z, options = undefined) | Vrací JoltConstraint/undefined, nevlastní tělesa. Dvě různá živá tělesa stejného světa; _world varianta jedno Dynamic těleso a pevný svět. Enabled default true; běžné dvoutělesové collision default true, pokud není dále uvedeno jinak. Po vytvoření ověř i jolt_has_error: některé navazující options mohou selhat a přesto vrátit vytvořený constraint. Světová kotva; twist_axis default+Z, plane_axis+X, nenulové neparalelní. Swing half angles0..180° default 45, twist-30..30° uvnitř±180, friction_torque>=0 default 0. Výchozí collision false. |
| jolt_swing_twist_world | jolt_swing_twist_world(body,anchor_x,anchor_y,anchor_z,options=undefined) | Vrací JoltConstraint/undefined, nevlastní tělesa. Dvě různá živá tělesa stejného světa; _world varianta jedno Dynamic těleso a pevný svět. Enabled default true; běžné dvoutělesové collision default true, pokud není dále uvedeno jinak. Po vytvoření ověř i jolt_has_error: některé navazující options mohou selhat a přesto vrátit vytvořený constraint. Světová kotva; twist_axis default+Z, plane_axis+X, nenulové neparalelní. Swing half angles0..180° default 45, twist-30..30° uvnitř±180, friction_torque>=0 default 0. Výchozí collision false. |
| JoltConstraint.enable_swing_motor | JoltConstraint.enable_swing_motor(enabled) | Pouze SwingTwist; Boolean úspěchu. False nastaví OFF, true obnoví poslední zvolený režim (před prvním nastavením VELOCITY). |
| JoltConstraint.enable_twist_motor | JoltConstraint.enable_twist_motor(enabled) | Pouze SwingTwist; Boolean úspěchu. False nastaví OFF, true obnoví poslední zvolený režim (před prvním nastavením VELOCITY). |
| JoltConstraint.get_current_orientation | JoltConstraint.get_current_orientation() | SixDOF: cílový/aktuální quaternion {x, y, z, w} v prostoru constraintu. Skládá nativní čtení bez spolehlivého undefined sentinel; nejdřív ověř typ/platnost a chyby. |
| JoltConstraint.get_current_orientation_euler | JoltConstraint.get_current_orientation_euler() | SixDOF: Euler {x, y, z} ve stupních z příslušného quaternion getteru; nejdřív ověř platnost a chyby. |
| JoltConstraint.get_orientation | JoltConstraint.get_orientation() | Pouze SwingTwist: aktuální relativní orientace v prostoru constraintu jako {x, y, z, w}; undefined při chybě. |
| JoltConstraint.get_swing_motor_state | JoltConstraint.get_swing_motor_state() | SwingTwist struct {mode, velocity_y, velocity_z, max_torque, spring_frequency, spring_damping}. Rychlosti °/s, moment N·m, frekvence Hz. undefined pro neplatný typ/wrapper; jednotlivé native chyby čti zvlášť. |
| JoltConstraint.get_swing_twist_impulses | JoltConstraint.get_swing_twist_impulses() | Pouze SwingTwist, poslední solver impulzy: position {x, y, z} (N·s), twist/swing_y/swing_z a motor {x, y, z} (N·m·s). undefined pro neplatný typ/wrapper; nejsou to síly za sekundu. |
| JoltConstraint.get_target_orientation | JoltConstraint.get_target_orientation() | SixDOF: cílový/aktuální quaternion {x, y, z, w} v prostoru constraintu. Skládá nativní čtení bez spolehlivého undefined sentinel; nejdřív ověř typ/platnost a chyby. |
| JoltConstraint.get_target_orientation_euler | JoltConstraint.get_target_orientation_euler() | SixDOF: Euler {x, y, z} ve stupních z příslušného quaternion getteru; nejdřív ověř platnost a chyby. |
| JoltConstraint.get_twist_angle | JoltConstraint.get_twist_angle() | Pouze SwingTwist: aktuální twist kolem constraint X ve stupních; 0 při chybě/neplatném typu. |
| JoltConstraint.get_twist_motor_state | JoltConstraint.get_twist_motor_state() | SwingTwist struct {mode, velocity, max_torque, spring_frequency, spring_damping}; velocity °/s, N·m, Hz. undefined pro neplatný typ/wrapper; native chyby čti zvlášť. |
| JoltConstraint.set_swing_limits | JoltConstraint.set_swing_limits(normal, plane) | Pouze SwingTwist; Boolean. Swing půlúhly 0..180°, twist min/max v -180..180° s min≤max. Zachová druhou část současných limitů. |
| JoltConstraint.set_swing_motor_max_torque | JoltConstraint.set_swing_motor_max_torque(torque) | Pouze SwingTwist; konečný nezáporný limit N·m; Boolean. Samotné nastavení limitu nezapne motor. |
| JoltConstraint.set_swing_motor_position | JoltConstraint.set_swing_motor_position(angle, constraint_axis_y=1, constraint_axis_z=0) | Pouze SwingTwist: konečný úhel ve stupních a nenulová osa v rovině Y/Z, default (1, 0), normalizovaná nativně. Boolean; zapne position motor a zachová twist cíle. |
| JoltConstraint.set_swing_motor_spring | JoltConstraint.set_swing_motor_spring(frequency,damping) | Pouze SwingTwist; konečná frequency ≥0 Hz, damping ≥0 jako poměr tlumení; Boolean. |
| JoltConstraint.set_swing_motor_velocity | JoltConstraint.set_swing_motor_velocity(constraint_y, constraint_z) | Pouze SwingTwist: konečné rychlosti kolem constraint Y/Z ve stupních/s. Boolean; také zapne swing motor v režimu velocity. |
| JoltConstraint.set_target_orientation | JoltConstraint.set_target_orientation(x,y,z,w) | SixDOF: orientace v prostoru constraintu; quaternion x/y/z/w konečný nenulový, nativně normalizovaný, nebo Euler XYZ ve stupních. Boolean. Motory je nutné zapnout samostatně. |
| JoltConstraint.set_target_orientation_euler | JoltConstraint.set_target_orientation_euler(x,y,z) | SixDOF: orientace v prostoru constraintu; quaternion x/y/z/w konečný nenulový, nativně normalizovaný, nebo Euler XYZ ve stupních. Boolean. Motory je nutné zapnout samostatně. |
| JoltConstraint.set_twist_limits | JoltConstraint.set_twist_limits(minimum, maximum) | Pouze SwingTwist; Boolean. Swing půlúhly 0..180°, twist min/max v -180..180° s min≤max. Zachová druhou část současných limitů. |
| JoltConstraint.set_twist_motor_max_torque | JoltConstraint.set_twist_motor_max_torque(torque) | Pouze SwingTwist; konečný nezáporný limit N·m; Boolean. Samotné nastavení limitu nezapne motor. |
| JoltConstraint.set_twist_motor_position | JoltConstraint.set_twist_motor_position(angle) | Pouze SwingTwist: konečný twist úhel ve stupních. Boolean; zapne position motor a zachová swing cíle. |
| JoltConstraint.set_twist_motor_spring | JoltConstraint.set_twist_motor_spring(frequency,damping) | Pouze SwingTwist; konečná frequency ≥0 Hz, damping ≥0 jako poměr tlumení; Boolean. |
| JoltConstraint.set_twist_motor_velocity | JoltConstraint.set_twist_motor_velocity(degrees_per_second) | Pouze SwingTwist: konečná rychlost kolem constraint X ve stupních/s. Boolean; také zapne twist motor v režimu velocity. |