Documentation
¶
Overview ¶
Package physics2d is a Box2D-backed 2D physics plugin for Cardinal. ECS components live in component; simulation and reconciliation systems live in system; package-level runtime state is owned here (see Runtime, ResetRuntime).
Usage:
world := cardinal.NewWorld(cardinal.WorldOptions{...})
cardinal.RegisterPlugin(world, physics2d.NewPlugin(physics2d.Config{}))
world.StartGame()
Call ResetRuntime from init/restore hooks when you rebuild the Cardinal world or after FromProto so the derived physics state matches ECS.
ResetRuntime (this package) discards derived physics state; the next PreUpdate reconcile performs a full ECS->Box2D rebuild when it sees no world on the C side.
Index ¶
- Constants
- func FlushBufferedContacts()
- func ResetRuntime()
- func SetStepContactEmitter(e ContactEventEmitter)
- func WorldID() uint32
- type AABBOverlapHit
- type AABBOverlapRequest
- type AABBOverlapResult
- type ActiveContacts
- type BodyType
- type CircleSweepRequest
- type CircleSweepResult
- type ColliderShape
- type Config
- type ContactBeginEvent
- type ContactEndEvent
- type ContactEventEmitter
- type ContactEventPayload
- type ContactPairEntry
- type Filter
- type FixtureFilterBits
- type PhysicsBody2D
- type PhysicsSingletonTag
- type Plugin
- type RaycastRequest
- type RaycastResult
- type ShapeType
- type Transform2D
- type TriggerBeginEvent
- type TriggerEndEvent
- type Vec2
- type Velocity2D
Constants ¶
const ( BodyTypeStatic = component.BodyTypeStatic BodyTypeDynamic = component.BodyTypeDynamic BodyTypeKinematic = component.BodyTypeKinematic BodyTypeManual = component.BodyTypeManual )
Body kinds (PhysicsBody2D).
const ( ShapeTypeCircle = component.ShapeTypeCircle ShapeTypeBox = component.ShapeTypeBox ShapeTypeConvexPolygon = component.ShapeTypeConvexPolygon ShapeTypeStaticChain = component.ShapeTypeStaticChain ShapeTypeStaticChainLoop = component.ShapeTypeStaticChainLoop ShapeTypeEdge = component.ShapeTypeEdge ShapeTypeCapsule = component.ShapeTypeCapsule )
Collider shape kinds (ColliderShape).
Variables ¶
This section is empty.
Functions ¶
func FlushBufferedContacts ¶
func FlushBufferedContacts()
FlushBufferedContacts emits all contact/trigger events from the last physics step via the ContactEventEmitter set by SetStepContactEmitter, then clears the buffer and the emitter slot. If an emitter was set for that step, one-shot contact suppression (after reset/rebuild) ends here. Ordering matches C-side callback order for that step only; see internal.FlushBufferedContacts.
func ResetRuntime ¶
func ResetRuntime()
ResetRuntime drops all derived physics simulation state (no world, no bodies, empty maps). ECS components are unchanged. The next PhysicsPipelineSystem (PreUpdate) runs FullRebuildFromECS from current physics entities, same as recovering after snapshot restore.
func SetStepContactEmitter ¶
func SetStepContactEmitter(e ContactEventEmitter)
SetStepContactEmitter assigns the sink for the next physics step's flushed contact events.
func WorldID ¶
func WorldID() uint32
WorldID returns the Box2D v3 world id packed as a uint32. Returns 0 when no world exists (before init or after ResetRuntime). Customers can use this with their own CGO code to call any Box2D v3 function via b2LoadWorldId(). This enables custom queries, joints, or any Box2D feature not directly exposed by the plugin.
Types ¶
type AABBOverlapHit ¶
type AABBOverlapHit = physicsquery.AABBOverlapHit
Query API (v1): raycast, AABB overlap, circle sweep.
type AABBOverlapRequest ¶
type AABBOverlapRequest = physicsquery.AABBOverlapRequest
Query API (v1): raycast, AABB overlap, circle sweep.
type AABBOverlapResult ¶
type AABBOverlapResult = physicsquery.AABBOverlapResult
Query API (v1): raycast, AABB overlap, circle sweep.
func OverlapAABB ¶
func OverlapAABB(req AABBOverlapRequest) AABBOverlapResult
OverlapAABB returns distinct (entity, shape index) pairs whose shapes overlap the world-space AABB.
type ActiveContacts ¶
type ActiveContacts = component.ActiveContacts
Re-export component types for callers that import the plugin root only.
type CircleSweepRequest ¶
type CircleSweepRequest = physicsquery.CircleSweepRequest
Query API (v1): raycast, AABB overlap, circle sweep.
type CircleSweepResult ¶
type CircleSweepResult = physicsquery.CircleSweepResult
Query API (v1): raycast, AABB overlap, circle sweep.
func CircleSweep ¶
func CircleSweep(req CircleSweepRequest) CircleSweepResult
CircleSweep sweeps a circle from req.Start to req.End and returns the earliest TOI hit along that segment.
type ColliderShape ¶
type ColliderShape = component.ColliderShape
Re-export component types for callers that import the plugin root only.
type Config ¶
type Config struct {
// Gravity is applied to the Box2D world (world gravity vector).
Gravity Vec2
// TickRate is simulation steps per second: each Cardinal tick calls cbridge.Step(1/TickRate, sub-steps).
// Match cardinal.WorldOptions.TickRate so simulated time advances one tick of wall-clock intent per tick.
// Zero or negative defaults to 60 (same as historical FixedDT 1/60).
TickRate float64
// SubStepCount is the number of sub-steps per physics step. Zero defaults to 4.
SubStepCount int
}
Config holds plugin options for simulation and stepping.
type ContactBeginEvent ¶
type ContactBeginEvent = physicevent.ContactBeginEvent
Contact / trigger system events (implement ecs.SystemEvent; register with WithSystemEventEmitter).
type ContactEndEvent ¶
type ContactEndEvent = physicevent.ContactEndEvent
Contact / trigger system events (implement ecs.SystemEvent; register with WithSystemEventEmitter).
type ContactEventEmitter ¶
type ContactEventEmitter = physicevent.ContactEventEmitter
Contact / trigger system events (implement ecs.SystemEvent; register with WithSystemEventEmitter).
type ContactEventPayload ¶
type ContactEventPayload = physicevent.ContactEventPayload
Contact / trigger system events (implement ecs.SystemEvent; register with WithSystemEventEmitter).
type ContactPairEntry ¶
type ContactPairEntry = component.ContactPairEntry
Re-export component types for callers that import the plugin root only.
type Filter ¶
type Filter = physicsquery.Filter
Query API (v1): raycast, AABB overlap, circle sweep.
type FixtureFilterBits ¶
type FixtureFilterBits = physicevent.FixtureFilterBits
Contact / trigger system events (implement ecs.SystemEvent; register with WithSystemEventEmitter).
type PhysicsBody2D ¶
type PhysicsBody2D = component.PhysicsBody2D
Components entities require to participate in physics simulation.
type PhysicsSingletonTag ¶
type PhysicsSingletonTag = component.PhysicsSingletonTag
Re-export component types for callers that import the plugin root only.
type Plugin ¶
type Plugin struct {
// contains filtered or unexported fields
}
Plugin implements cardinal.Plugin for the physics2d package.
type RaycastRequest ¶
type RaycastRequest = physicsquery.RaycastRequest
Query API (v1): raycast, AABB overlap, circle sweep.
type RaycastResult ¶
type RaycastResult = physicsquery.RaycastResult
Query API (v1): raycast, AABB overlap, circle sweep.
func Raycast ¶
func Raycast(req RaycastRequest) RaycastResult
Raycast casts a ray along the segment from req.Origin to req.End and returns the closest hit. Requires an initialized physics runtime with a C-side world (e.g. after FullRebuildFromECS). A zero-length segment returns Hit=false. When Filter is nil, all category/mask pairs match and sensors are skipped (same as Filter{CategoryBits: ^uint64(0), MaskBits: ^uint64(0), IncludeSensors: false}).
type Transform2D ¶
type Transform2D = component.Transform2D
Components entities require to participate in physics simulation.
type TriggerBeginEvent ¶
type TriggerBeginEvent = physicevent.TriggerBeginEvent
Contact / trigger system events (implement ecs.SystemEvent; register with WithSystemEventEmitter).
type TriggerEndEvent ¶
type TriggerEndEvent = physicevent.TriggerEndEvent
Contact / trigger system events (implement ecs.SystemEvent; register with WithSystemEventEmitter).
type Velocity2D ¶
type Velocity2D = component.Velocity2D
Components entities require to participate in physics simulation.