Skip to main content

FreeBodyDiagramMixin

@physion/app / Exports / FreeBodyDiagramMixin

Class: FreeBodyDiagramMixin

Draws the forces acting on a BodyNode as labelled arrows, on top of the scene.

Which forces are drawn, and how, is controlled by a single object-valued property (freeBodyDiagram), edited in the inspector by FreeBodyDiagramEditor and settable from a script. Each key is a types.ForceKey and each value is an options object switched on by its enabled flag:

{
"gravity": { "enabled": true },
"normal": { "enabled": true, "color": "#ffffff", "scale": 0.05, "components": true }
}

The default value spells every force out with enabled: false, so that a scene records what it was asked for rather than inheriting whatever the defaults become. Unrecognized keys are kept in the stored value, so a typo survives a save/load round trip, but are ignored when drawing.

Gravity is computed from the body and the scene on every frame. The other four are sampled once per physics step, in the body's local frame, and are not discarded when the simulation stops: pausing to read a value works, and a frozen arrow follows its body if the body is then dragged.

By default each force is drawn from where it acts. Setting atCenter on every force instead produces the textbook diagram, in which the body is a point and every arrow radiates from its centre of mass; setting it on only some of them gives neither picture.

The overlay is drawn on the scene's freeBodyDiagrams layer rather than inside the body, so that it is never hidden behind another body. Like TextMixin, this is a composition helper rather than a TypeScript mixin: the host node owns one, forwards the property accessor to it, and calls its lifecycle methods. The Mixin suffix keeps it out of NodeFactory.getClassNames.

Constructors

constructor

new FreeBodyDiagramMixin(node, freeBodyDiagram?): FreeBodyDiagramMixin

Parameters

NameTypeDefault value
nodeBodyNodeundefined
freeBodyDiagramIFreeBodyDiagramdefaultFreeBodyDiagram

Returns

FreeBodyDiagramMixin

Properties

DEFAULT_FREE_BODY_DIAGRAM

Static Readonly DEFAULT_FREE_BODY_DIAGRAM: IFreeBodyDiagram = defaultFreeBodyDiagram

Accessors

freeBodyDiagram

get freeBodyDiagram(): IFreeBodyDiagram

The raw, user-authored value. Replace it to change what is drawn; mutating the returned object in place does not trigger a redraw of the resolved configuration.

Returns

IFreeBodyDiagram

set freeBodyDiagram(v): void

Parameters

NameType
vIFreeBodyDiagram

Returns

void


frictionForces

get frictionForces(): readonly IContactForce[]

The contact friction forces of the last physics step, in the body's local frame.

Returns

readonly IContactForce[]


graphics

get graphics(): Graphics

Returns

Graphics


isEmpty

get isEmpty(): boolean

True when nothing at all is drawn.

Returns

boolean


jointForces

get jointForces(): readonly IJointForce[]

The joint reaction forces of the last physics step, in the body's local frame.

Returns

readonly IJointForce[]


needsContactData

get needsContactData(): boolean

True when the contact forces on the host body are being read.

Returns

boolean


needsJointData

get needsJointData(): boolean

True when the reaction forces of the joints attached to the host body are being read.

Returns

boolean


needsSampling

get needsSampling(): boolean

True when anything at all has to be sampled from the simulation. A diagram that is empty, or draws gravity alone, needs nothing, which keeps the feature free for the bodies not using it.

Returns

boolean


needsSpringData

get needsSpringData(): boolean

True when the springs attached to the host body must report the force they apply.

Returns

boolean


node

get node(): BodyNode

Returns

BodyNode


normalForces

get normalForces(): readonly IContactForce[]

The contact normal forces of the last physics step, in the body's local frame.

Returns

readonly IContactForce[]


springForces

get springForces(): readonly IBodyForce[]

The spring forces of the last physics step, in the body's local frame.

Returns

readonly IBodyForce[]

Methods

addSpringForce

addSpringForce(point, force): void

Records the force a spring applies to the host body, from SpringNode.update(). A spring is the one force in Physion that Box2D never sees, so the body cannot discover it on its own.

Parameters

NameTypeDescription
pointIPointWhere the spring is anchored on the host body, in scene coordinates.
forceIPointThe force it applies there, in Newtons and scene coordinates.

Returns

void


attach

attach(scene): void

Parameters

NameType
sceneScene

Returns

void


destroyFreeBodyDiagram

destroyFreeBodyDiagram(): void

Returns

void


detach

detach(): void

Returns

void


draw

draw(): void

Redraws the overlay, once per rendered frame. Everything is in scene coordinates: the samples are converted out of the body's frame here, against its current transform, so they follow it.

Returns

void


reset

reset(): void

Discards everything sampled from the simulation. Called when a run starts, not when it stops: a stopped scene is a paused one, so its last sample is still the right thing to have on screen.

Returns

void


sample

sample(): void

Samples the forces that cannot be computed from the body alone. Must be called from the host's update(), which the Scene runs after the step, so the impulses describe the frame drawn next.

Returns

void


propertyDescriptors

propertyDescriptors(): IPropertyDescriptor[]

Returns

IPropertyDescriptor[]