wave5-physics-velocity-force-application
Apply forces and impulses to Unity Physics dynamic bodies by writing PhysicsVelocity from systems scheduled in FixedStepSimulationSystemGroup before PhysicsSystemGroup.
- License 许可
- MIT
- 最近更新
- 2026-07-25
- Stars
- ★ 8
- Forks
- 0
01 前言 Front Matter
- name
- wave5-physics-velocity-force-application
- tags
- [ "physics" ]
- metadata
- { "tier": 3, "internal-only": true }
- description
- Apply forces and impulses to Unity Physics dynamic bodies by writing PhysicsVelocity from systems scheduled in FixedStepSimulationSystemGroup before PhysicsSystemGroup.
03 SKILL.md 正文
Physics Velocity and Force Application
Intent
Apply forces and impulses to Unity Physics dynamic bodies by writing PhysicsVelocity from systems scheduled in FixedStepSimulationSystemGroup before PhysicsSystemGroup.
Use When
- Continuous forces (gravity well, wind, magnetic attraction) on dynamic rigid bodies
- Setting initial velocity on spawned physics bodies
- Impulses (explosions, projectile hits) on dynamic bodies
Avoid When
- Moving kinematic bodies — write LocalTransform directly; PhysicsVelocity is ignored for kinematics
- Applying forces in SimulationSystemGroup (variable rate) — always use FixedStepSimulationSystemGroup
Senior Pattern
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateBefore(typeof(PhysicsSystemGroup))]
[BurstCompile]
public partial struct ForceSystem : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
state.Dependency = new ApplyForceJob
{
DeltaTime = SystemAPI.Time.fixedDeltaTime,
WellPosition = SystemAPI.GetSingleton<GravityWell>().Position
}.ScheduleParallel(state.Dependency);
}
}
[BurstCompile]
public partial struct ApplyForceJob : IJobEntity
{
public float DeltaTime;
public float3 WellPosition;
[BurstCompile]
public void Execute(ref PhysicsVelocity velocity, in PhysicsMass mass, in LocalTransform transform)
{
// Guard: mass.InverseMass == 0 for static/infinite-mass bodies
if (mass.InverseMass == 0f) return;
float3 dir = math.normalize(WellPosition - transform.Position);
velocity.Linear += dir * (1f / mass.InverseMass) * DeltaTime;
}
}
// One-shot initial velocity via ISystemStartStop:
public void OnStartRunning(ref SystemState state)
{
foreach (var (initial, vel) in SystemAPI.Query<RefRO<InitialVelocity>, RefRW<PhysicsVelocity>>())
{
vel.ValueRW.Linear = initial.ValueRO.Linear;
vel.ValueRW.Angular = initial.ValueRO.Angular;
}
}
Anti-Patterns
- Placing force systems in SimulationSystemGroup — variable rate, not fixed timestep, produces jitter and frame-rate-dependent physics.
- Writing LocalTransform to teleport a physics body — physics broad/narrow phase data is stale; body jumps but collision detection has a gap.
- Using SystemAPI.Time.DeltaTime inside FixedStepSimulationSystemGroup instead of fixedDeltaTime — same value at runtime but intent is wrong; use fixedDeltaTime explicitly.
- Dividing by InverseMass without guarding zero — static bodies have InverseMass == 0; division produces NaN velocity.
Runtime Risks
- PhysicsMass.InverseMass == 0 for static (infinite-mass) bodies — always guard before force calculations.
- PhysicsVelocity.ApplyExplosionForce is a convenience extension — correct but has more per-call cost than simple velocity.Linear += for tight loops.
Performance Notes
- ScheduleParallel over physics entities scales with body count.
- Profile FixedStepSimulationSystemGroup separately from the render-rate group — it may run 0 or 2+ times per render frame.
Architecture Guidance
- Pre-solve forces: [UpdateBefore(PhysicsSystemGroup)] in FixedStepSimulationSystemGroup.
- Post-solve event processing: [UpdateAfter(PhysicsSimulationGroup)] in FixedStepSimulationSystemGroup.
- Initial velocity (one-shot): use ISystemStartStop.OnStartRunning paired with RequireForUpdate on InitialVelocity query.
Related Skills
[[wave5-fixed-step-simulation-system-group]], [[isystem-start-stop]], [[wave5-physics-world-singleton-queries]], [[fixed-step-simulation]]
04 同仓库更多 Skills
dots-update-groups
How to choose the right ECS system group and ordering so per-frame and per-fixed-step work is deterministic and never races its own readers. Covers InitializationSystemGroup / SimulationSystemGroup / PresentationSystemGroup / FixedStepSimulationSystemGroup / VariableRateSimulationSystemGroup, UpdateBefore / UpdateAfter peer ordering, OrderFirst / OrderLast group-boundary placement, RateManager, and the cross-group ordering trap. Use when adding an ISystem, fixing "value visible one frame late" bugs, or coupling simulation to a fixed timestep.
dots-transform-patterns
The Entities 1.x transform model — LocalTransform (writable input) vs LocalToWorld (system-owned matrix), Parent/Child hierarchy, reparenting cost, uniform vs non-uniform scale (PostTransformMatrix), and when to opt out with custom transform systems. Use when designing parent/child hierarchies, runtime reparenting (mount/dismount/socket), or choosing transform components at the Baker boundary.
dots-singleton-patterns
Model cross-system shared configuration and game state as a single-entity component with a strict single-writer contract. Covers RequireForUpdate gating, SystemAPI.GetSingleton / GetSingletonRW / HasSingleton / TryGetSingleton, bake-time vs runtime-created vs ECB-system singletons, the "exactly one entity" runtime invariant, and the single-writer rule. Use when designing frame-stable global config, shared mutable state, or any component intended to exist on exactly one entity.
dots-hybrid-bridge
The one-way bridge between DOTS-owned runtime state and Unity-owned presentation. Covers UnityObjectRef for managed asset refs in components, the DOTS-writes / Unity-reads ownership rule, when to use managed IComponentData vs UnityObjectRef vs CompanionComponent, baker-bridge vs runtime-bridge, and SubScene boundary rules. Use when an entity must reference Unity assets (Mesh, Material, AudioClip, ScriptableObject), a MonoBehaviour view must read entity state, or input/UI must trigger ECS work.
dots-event-driven-ecs
Model one-frame events and external requests in ECS without dual ownership, polling, or persistent state leaks. Covers request-tag pattern with ECB, enableable command components (no structural change), event buffers (IBufferElementData), explicit consumer ownership, one-frame latency rules, and the banned C# event / UnityEvent / static bus anti-patterns. Use when input/UI/network must trigger ECS work, when a one-frame signal fans out to N consumers (damage, hit, collision), or when state changes must be processed exactly once.
valid_skill
A complete, well-formed test fixture skill for SkillHub validation testing. Covers basic task routing and code review for Unity projects.