|
| 1 | +//nolint:recvcheck // UnmarshalJSON must be pointer receiver to support json.Unmarshal |
| 2 | +package component |
| 3 | + |
| 4 | +import ( |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/goccy/go-json" |
| 9 | +) |
| 10 | + |
| 11 | +// PhysicsBody2D holds simulation parameters for a rigid body and its collider shapes. |
| 12 | +// |
| 13 | +// BodyType selects static vs dynamic vs kinematic behavior. LinearDamping and AngularDamping |
| 14 | +// are simulation damping coefficients. GravityScale multiplies the world's gravity vector |
| 15 | +// for this body; world gravity itself is runtime configuration, not a component field. |
| 16 | +// |
| 17 | +// # Body flags |
| 18 | +// |
| 19 | +// Active controls whether the body participates in the simulation at all. An inactive body |
| 20 | +// has no contacts, no collisions, and is effectively removed from Box2D without destroying it. |
| 21 | +// Set Active=false to temporarily disable an entity's physics (e.g. a dormant trap). |
| 22 | +// |
| 23 | +// Awake controls whether the body is currently awake in the simulation. Setting Awake=true |
| 24 | +// wakes a sleeping body; Box2D may put it back to sleep on subsequent ticks if nothing |
| 25 | +// disturbs it and SleepingAllowed is true. To keep a body permanently awake (e.g. a |
| 26 | +// stationary kinematic sensor that must always generate contacts), set SleepingAllowed=false |
| 27 | +// instead. |
| 28 | +// |
| 29 | +// SleepingAllowed controls whether Box2D is permitted to put the body to sleep when it comes |
| 30 | +// to rest. When false, the body stays awake indefinitely. Use this for kinematic/manual |
| 31 | +// bodies that are stationary but must still generate contacts (the common "sensor" pattern). |
| 32 | +// |
| 33 | +// Bullet enables continuous collision detection (CCD) for fast-moving dynamic bodies to |
| 34 | +// prevent tunneling through thin geometry. Has a performance cost; only enable for |
| 35 | +// projectiles or similarly fast objects. |
| 36 | +// |
| 37 | +// FixedRotation prevents the body from rotating in response to torques or collisions. |
| 38 | +// Useful for top-down characters that should not spin. |
| 39 | +// |
| 40 | +// # Shapes |
| 41 | +// |
| 42 | +// Shapes holds the compound collider description. Cardinal allows one instance per component |
| 43 | +// type per entity, so compound colliders are modeled as multiple ColliderShape entries. |
| 44 | +// Shape identity (v1): index i in Shapes identifies fixture slot i. |
| 45 | +// |
| 46 | +// # Defaults |
| 47 | +// |
| 48 | +// Box2D defaults Active, Awake, and SleepingAllowed to true and GravityScale to 1. Use |
| 49 | +// [NewPhysicsBody2D] to create a PhysicsBody2D with these defaults set correctly. Bare struct |
| 50 | +// literals leave bool fields at false and GravityScale at 0, which produces an inactive, |
| 51 | +// sleeping body with no gravity — almost never what you want. |
| 52 | +// |
| 53 | +// When deserializing from JSON (e.g. snapshot recovery), missing fields are defaulted to |
| 54 | +// their Box2D values automatically via a custom UnmarshalJSON. Explicitly serialized false |
| 55 | +// values are preserved exactly. |
| 56 | +// |
| 57 | +// Bullet and FixedRotation default to false (off), matching Box2D defaults. |
| 58 | +// |
| 59 | +// # Post-step writeback |
| 60 | +// |
| 61 | +// Writeback applies to dynamic and kinematic bodies. Static and manual bodies are |
| 62 | +// not written back. |
| 63 | +type PhysicsBody2D struct { |
| 64 | + BodyType BodyType `json:"body_type"` |
| 65 | + LinearDamping float64 `json:"linear_damping"` |
| 66 | + AngularDamping float64 `json:"angular_damping"` |
| 67 | + GravityScale float64 `json:"gravity_scale"` |
| 68 | + Active bool `json:"active"` |
| 69 | + Awake bool `json:"awake"` |
| 70 | + SleepingAllowed bool `json:"sleeping_allowed"` |
| 71 | + Bullet bool `json:"bullet"` |
| 72 | + FixedRotation bool `json:"fixed_rotation"` |
| 73 | + |
| 74 | + Shapes []ColliderShape `json:"shapes"` |
| 75 | +} |
| 76 | + |
| 77 | +// NewPhysicsBody2D returns a PhysicsBody2D with the given body type, Box2D-compatible defaults |
| 78 | +// (Active=true, Awake=true, SleepingAllowed=true, GravityScale=1), and the provided shapes. |
| 79 | +func NewPhysicsBody2D(bodyType BodyType, shapes ...ColliderShape) PhysicsBody2D { |
| 80 | + return PhysicsBody2D{ |
| 81 | + BodyType: bodyType, |
| 82 | + GravityScale: 1, |
| 83 | + Active: true, |
| 84 | + Awake: true, |
| 85 | + SleepingAllowed: true, |
| 86 | + Shapes: shapes, |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// UnmarshalJSON decodes a PhysicsBody2D from JSON, applying Box2D-compatible defaults for |
| 91 | +// fields missing from the payload. This handles old snapshots that predate the body flags |
| 92 | +// (Active, Awake, SleepingAllowed default to true; GravityScale defaults to 1) while |
| 93 | +// preserving explicitly serialized values including false. |
| 94 | +func (p *PhysicsBody2D) UnmarshalJSON(data []byte) error { |
| 95 | + type raw struct { |
| 96 | + BodyType BodyType `json:"body_type"` |
| 97 | + LinearDamping float64 `json:"linear_damping"` |
| 98 | + AngularDamping float64 `json:"angular_damping"` |
| 99 | + GravityScale *float64 `json:"gravity_scale"` |
| 100 | + Active *bool `json:"active"` |
| 101 | + Awake *bool `json:"awake"` |
| 102 | + SleepingAllowed *bool `json:"sleeping_allowed"` |
| 103 | + Bullet bool `json:"bullet"` |
| 104 | + FixedRotation bool `json:"fixed_rotation"` |
| 105 | + Shapes []ColliderShape `json:"shapes"` |
| 106 | + } |
| 107 | + var aux raw |
| 108 | + if err := json.Unmarshal(data, &aux); err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + *p = PhysicsBody2D{ |
| 112 | + BodyType: aux.BodyType, |
| 113 | + LinearDamping: aux.LinearDamping, |
| 114 | + AngularDamping: aux.AngularDamping, |
| 115 | + GravityScale: 1, |
| 116 | + Active: true, |
| 117 | + Awake: true, |
| 118 | + SleepingAllowed: true, |
| 119 | + Bullet: aux.Bullet, |
| 120 | + FixedRotation: aux.FixedRotation, |
| 121 | + Shapes: aux.Shapes, |
| 122 | + } |
| 123 | + if aux.GravityScale != nil { |
| 124 | + p.GravityScale = *aux.GravityScale |
| 125 | + } |
| 126 | + if aux.Active != nil { |
| 127 | + p.Active = *aux.Active |
| 128 | + } |
| 129 | + if aux.Awake != nil { |
| 130 | + p.Awake = *aux.Awake |
| 131 | + } |
| 132 | + if aux.SleepingAllowed != nil { |
| 133 | + p.SleepingAllowed = *aux.SleepingAllowed |
| 134 | + } |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +// Name returns the ECS component name. |
| 139 | +func (PhysicsBody2D) Name() string { return "physics_body_2d" } |
| 140 | + |
| 141 | +// Validate guards against NaN/Inf in float fields, an invalid body type tag, and invalid shapes. |
| 142 | +func (p PhysicsBody2D) Validate() error { |
| 143 | + switch p.BodyType { |
| 144 | + case BodyTypeStatic, BodyTypeDynamic, BodyTypeKinematic, BodyTypeManual: |
| 145 | + default: |
| 146 | + return fmt.Errorf("physics_body_2d.body_type: invalid value %d", p.BodyType) |
| 147 | + } |
| 148 | + if !isFinite(p.LinearDamping) { |
| 149 | + return fmt.Errorf("physics_body_2d.linear_damping: must be finite, got %v", p.LinearDamping) |
| 150 | + } |
| 151 | + if !isFinite(p.AngularDamping) { |
| 152 | + return fmt.Errorf("physics_body_2d.angular_damping: must be finite, got %v", p.AngularDamping) |
| 153 | + } |
| 154 | + if !isFinite(p.GravityScale) { |
| 155 | + return fmt.Errorf("physics_body_2d.gravity_scale: must be finite, got %v", p.GravityScale) |
| 156 | + } |
| 157 | + if len(p.Shapes) == 0 { |
| 158 | + return errors.New("physics_body_2d.shapes: at least one ColliderShape is required") |
| 159 | + } |
| 160 | + for i := range p.Shapes { |
| 161 | + if err := p.Shapes[i].Validate(); err != nil { |
| 162 | + return fmt.Errorf("physics_body_2d.shapes[%d]: %w", i, err) |
| 163 | + } |
| 164 | + } |
| 165 | + return nil |
| 166 | +} |
0 commit comments