2021-05-11 18:30:36 -07:00

27 KiB
Raw Permalink Blame History

Planck.js API Doc › Globals › Body

Class: Body

A rigid body composed of one or more fixtures.

To create a new Body use World.createBody.

Hierarchy

  • Body

Index

Properties

Methods

Properties

Static Readonly DYNAMIC

▪ DYNAMIC: BodyType = "dynamic"

Defined in src/dynamics/Body.ts:173

A dynamic body is fully simulated. They can be moved manually by the user, but normally they move according to forces. A dynamic body can collide with all body types. A dynamic body always has finite, non-zero mass. If you try to set the mass of a dynamic body to zero, it will automatically acquire a mass of one kilogram and it won't rotate.


Static Readonly KINEMATIC

▪ KINEMATIC: BodyType = "kinematic"

Defined in src/dynamics/Body.ts:164

A kinematic body moves under simulation according to its velocity. Kinematic bodies do not respond to forces. They can be moved manually by the user, but normally a kinematic body is moved by setting its velocity. A kinematic body behaves as if it has infinite mass, however, zero is stored for the mass and the inverse mass. Kinematic bodies do not collide with other kinematic or static bodies.


Static Readonly STATIC

▪ STATIC: BodyType = "static"

Defined in src/dynamics/Body.ts:156

A static body does not move under simulation and behaves as if it has infinite mass. Internally, zero is stored for the mass and the inverse mass. Static bodies can be moved manually by the user. A static body has zero velocity. Static bodies do not collide with other static or kinematic bodies.

Methods

advance

▸ advance(alpha: number): void

Defined in src/dynamics/Body.ts:607

Used in TOI.

Parameters:

Name Type
alpha number

Returns: void


applyAngularImpulse

▸ applyAngularImpulse(impulse: number, wake?: boolean): void

Defined in src/dynamics/Body.ts:979

Apply an angular impulse.

Parameters:

Name Type Description
impulse number The angular impulse in units of kgmm/s
wake? boolean Also wake up the body

Returns: void


applyForce

▸ applyForce(force: Vec2, point: Vec2, wake?: boolean): void

Defined in src/dynamics/Body.ts:896

Apply a force at a world point. If the force is not applied at the center of mass, it will generate a torque and affect the angular velocity. This wakes up the body.

Parameters:

Name Type Description
force Vec2 The world force vector, usually in Newtons (N).
point Vec2 The world position of the point of application.
wake? boolean Also wake up the body

Returns: void


applyForceToCenter

▸ applyForceToCenter(force: Vec2, wake?: boolean): void

Defined in src/dynamics/Body.ts:916

Apply a force to the center of mass. This wakes up the body.

Parameters:

Name Type Description
force Vec2 The world force vector, usually in Newtons (N).
wake? boolean Also wake up the body

Returns: void


applyLinearImpulse

▸ applyLinearImpulse(impulse: Vec2, point: Vec2, wake?: boolean): void

Defined in src/dynamics/Body.ts:958

Apply an impulse at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. This wakes up the body.

Parameters:

Name Type Description
impulse Vec2 The world impulse vector, usually in N-seconds or kg-m/s.
point Vec2 The world position of the point of application.
wake? boolean Also wake up the body

Returns: void


applyTorque

▸ applyTorque(torque: number, wake?: boolean): void

Defined in src/dynamics/Body.ts:936

Apply a torque. This affects the angular velocity without affecting the linear velocity of the center of mass. This wakes up the body.

Parameters:

Name Type Description
torque number About the z-axis (out of the screen), usually in N-m.
wake? boolean Also wake up the body

Returns: void


createFixture

▸ createFixture(def: FixtureDef): Fixture

Defined in src/dynamics/Body.ts:1053

Creates a fixture and attach it to this body.

If the density is non-zero, this function automatically updates the mass of the body.

Contacts are not created until the next time step.

Warning: This function is locked during callbacks.

Parameters:

Name Type
def FixtureDef

Returns: Fixture

▸ createFixture(shape: Shape, opt?: FixtureOpt): Fixture

Defined in src/dynamics/Body.ts:1054

Parameters:

Name Type
shape Shape
opt? FixtureOpt

Returns: Fixture

▸ createFixture(shape: Shape, density?: number): Fixture

Defined in src/dynamics/Body.ts:1055

Parameters:

Name Type
shape Shape
density? number

Returns: Fixture


destroyFixture

▸ destroyFixture(fixture: Fixture): void

Defined in src/dynamics/Body.ts:1080

Destroy a fixture. This removes the fixture from the broad-phase and destroys all contacts associated with this fixture. This will automatically adjust the mass of the body if the body is dynamic and the fixture has positive density. All fixtures attached to a body are implicitly destroyed when the body is destroyed.

Warning: This function is locked during callbacks.

Parameters:

Name Type Description
fixture Fixture The fixture to be removed.

Returns: void


getAngle

▸ getAngle(): number

Defined in src/dynamics/Body.ts:629

Get the current world rotation angle in radians.

Returns: number


getAngularDamping

▸ getAngularDamping(): number

Defined in src/dynamics/Body.ts:727

Returns: number


getAngularVelocity

▸ getAngularVelocity(): number

Defined in src/dynamics/Body.ts:700

Get the angular velocity.

Returns: number

the angular velocity in radians/second.


getContactList

▸ getContactList(): ContactEdge | null

Defined in src/dynamics/Body.ts:347

Warning: this list changes during the time step and you may miss some collisions if you don't use ContactListener.

Returns: ContactEdge | null


getFixtureList

▸ getFixtureList(): Fixture | null

Defined in src/dynamics/Body.ts:335

Returns: Fixture | null


getGravityScale

▸ getGravityScale(): number

Defined in src/dynamics/Body.ts:735

Returns: number


getInertia

▸ getInertia(): number

Defined in src/dynamics/Body.ts:760

Get the rotational inertia of the body about the local origin.

Returns: number

the rotational inertia, usually in kg-m^2.


getJointList

▸ getJointList(): JointEdge | null

Defined in src/dynamics/Body.ts:339

Returns: JointEdge | null


getLinearDamping

▸ getLinearDamping(): number

Defined in src/dynamics/Body.ts:719

Returns: number


getLinearVelocity

▸ getLinearVelocity(): Vec2

Defined in src/dynamics/Body.ts:656

Get the linear velocity of the center of mass.

Returns: Vec2

the linear velocity of the center of mass.


getLinearVelocityFromLocalPoint

▸ getLinearVelocityFromLocalPoint(localPoint: Vec2): Vec2

Defined in src/dynamics/Body.ts:676

Get the world velocity of a local point.

Parameters:

Name Type Description
localPoint Vec2 A point in local coordinates.

Returns: Vec2


getLinearVelocityFromWorldPoint

▸ getLinearVelocityFromWorldPoint(worldPoint: Vec2): Vec2

Defined in src/dynamics/Body.ts:665

Get the world linear velocity of a world point attached to this body.

Parameters:

Name Type Description
worldPoint Vec2 A point in world coordinates.

Returns: Vec2


getLocalCenter

▸ getLocalCenter(): Vec2

Defined in src/dynamics/Body.ts:647

Get the local position of the center of mass.

Returns: Vec2


getLocalPoint

▸ getLocalPoint(worldPoint: Vec2): Vec2

Defined in src/dynamics/Body.ts:1157

Gets the corresponding local point of a world point.

Parameters:

Name Type
worldPoint Vec2

Returns: Vec2


getLocalVector

▸ getLocalVector(worldVector: Vec2): Vec2

Defined in src/dynamics/Body.ts:1164

Gets the corresponding local vector of a world vector.

Parameters:

Name Type
worldVector Vec2

Returns: Vec2


getMass

▸ getMass(): number

Defined in src/dynamics/Body.ts:751

Get the total mass of the body.

Returns: number

The mass, usually in kilograms (kg).


getMassData

▸ getMassData(data: MassData): void

Defined in src/dynamics/Body.ts:768

Copy the mass data of the body to data.

Parameters:

Name Type
data MassData

Returns: void


getNext

▸ getNext(): Body | null

Defined in src/dynamics/Body.ts:323

Returns: Body | null


getPosition

▸ getPosition(): Vec2

Defined in src/dynamics/Body.ts:618

Get the world position for the body's origin.

Returns: Vec2


getTransform

▸ getTransform(): Transform

Defined in src/dynamics/Body.ts:559

Get the world transform for the body's origin.

Returns: Transform


getUserData

▸ getUserData(): unknown

Defined in src/dynamics/Body.ts:331

Returns: unknown


getWorld

▸ getWorld(): World

Defined in src/dynamics/Body.ts:319

Returns: World


getWorldCenter

▸ getWorldCenter(): Vec2

Defined in src/dynamics/Body.ts:640

Get the world position of the center of mass.

Returns: Vec2


getWorldPoint

▸ getWorldPoint(localPoint: Vec2): Vec2

Defined in src/dynamics/Body.ts:1143

Get the corresponding world point of a local point.

Parameters:

Name Type
localPoint Vec2

Returns: Vec2


getWorldVector

▸ getWorldVector(localVector: Vec2): Vec2

Defined in src/dynamics/Body.ts:1150

Get the corresponding world vector of a local vector.

Parameters:

Name Type
localVector Vec2

Returns: Vec2


isActive

▸ isActive(): boolean

Defined in src/dynamics/Body.ts:485

Returns: boolean


isAwake

▸ isAwake(): boolean

Defined in src/dynamics/Body.ts:460

Returns: boolean


isBullet

▸ isBullet(): boolean

Defined in src/dynamics/Body.ts:438

Returns: boolean


isDynamic

▸ isDynamic(): boolean

Defined in src/dynamics/Body.ts:355

Returns: boolean


isFixedRotation

▸ isFixedRotation(): boolean

Defined in src/dynamics/Body.ts:537

Returns: boolean


isKinematic

▸ isKinematic(): boolean

Defined in src/dynamics/Body.ts:359

Returns: boolean


isSleepingAllowed

▸ isSleepingAllowed(): boolean

Defined in src/dynamics/Body.ts:449

Returns: boolean


isStatic

▸ isStatic(): boolean

Defined in src/dynamics/Body.ts:351

Returns: boolean


isWorldLocked

▸ isWorldLocked(): boolean

Defined in src/dynamics/Body.ts:315

Returns: boolean


resetMassData

▸ resetMassData(): void

Defined in src/dynamics/Body.ts:779

This resets the mass properties to the sum of the mass properties of the fixtures. This normally does not need to be called unless you called SetMassData to override the mass and you later want to reset the mass.

Returns: void


setActive

▸ setActive(flag: boolean): void

Defined in src/dynamics/Body.ts:502

Set the active state of the body. An inactive body is not simulated and cannot be collided with or woken up. If you pass a flag of true, all fixtures will be added to the broad-phase. If you pass a flag of false, all fixtures will be removed from the broad-phase and all contacts will be destroyed. Fixtures and joints are otherwise unaffected.

You may continue to create/destroy fixtures and joints on inactive bodies. Fixtures on an inactive body are implicitly inactive and will not participate in collisions, ray-casts, or queries. Joints connected to an inactive body are implicitly inactive. An inactive body is still owned by a World object and remains

Parameters:

Name Type
flag boolean

Returns: void


setAngle

▸ setAngle(angle: number): void

Defined in src/dynamics/Body.ts:633

Parameters:

Name Type
angle number

Returns: void


setAngularDamping

▸ setAngularDamping(angularDamping: number): void

Defined in src/dynamics/Body.ts:731

Parameters:

Name Type
angularDamping number

Returns: void


setAngularVelocity

▸ setAngularVelocity(w: number): void

Defined in src/dynamics/Body.ts:709

Set the angular velocity.

Parameters:

Name Type
w number

Returns: void


setAwake

▸ setAwake(flag: boolean): void

Defined in src/dynamics/Body.ts:469

Set the sleep state of the body. A sleeping body has very low CPU cost.

Parameters:

Name Type Description
flag boolean Set to true to wake the body, false to put it to sleep.

Returns: void


setBullet

▸ setBullet(flag: boolean): void

Defined in src/dynamics/Body.ts:445

Should this body be treated like a bullet for continuous collision detection?

Parameters:

Name Type
flag boolean

Returns: void


setDynamic

▸ setDynamic(): Body

Defined in src/dynamics/Body.ts:371

Returns: Body


setFixedRotation

▸ setFixedRotation(flag: boolean): void

Defined in src/dynamics/Body.ts:544

Set this body to have fixed rotation. This causes the mass to be reset.

Parameters:

Name Type
flag boolean

Returns: void


setGravityScale

▸ setGravityScale(scale: number): void

Defined in src/dynamics/Body.ts:742

Scale the gravity applied to this body.

Parameters:

Name Type
scale number

Returns: void


setKinematic

▸ setKinematic(): Body

Defined in src/dynamics/Body.ts:376

Returns: Body


setLinearDamping

▸ setLinearDamping(linearDamping: number): void

Defined in src/dynamics/Body.ts:723

Parameters:

Name Type
linearDamping number

Returns: void


setLinearVelocity

▸ setLinearVelocity(v: Vec2): void

Defined in src/dynamics/Body.ts:685

Set the linear velocity of the center of mass.

Parameters:

Name Type Description
v Vec2 The new linear velocity of the center of mass.

Returns: void


setMassData

▸ setMassData(massData: MassData): void

Defined in src/dynamics/Body.ts:850

Set the mass properties to override the mass properties of the fixtures. Note that this changes the center of mass position. Note that creating or destroying fixtures can also alter the mass. This function has no effect if the body isn't dynamic.

Parameters:

Name Type Description
massData MassData The mass properties.

Returns: void


setPosition

▸ setPosition(p: Vec2): void

Defined in src/dynamics/Body.ts:622

Parameters:

Name Type
p Vec2

Returns: void


setSleepingAllowed

▸ setSleepingAllowed(flag: boolean): void

Defined in src/dynamics/Body.ts:453

Parameters:

Name Type
flag boolean

Returns: void


setStatic

▸ setStatic(): Body

Defined in src/dynamics/Body.ts:366

This will alter the mass and velocity.

Returns: Body


setTransform

▸ setTransform(position: Vec2, angle: number): void

Defined in src/dynamics/Body.ts:571

Set the position of the body's origin and rotation. Manipulating a body's transform may cause non-physical behavior. Note: contacts are updated on the next call to World.step.

Parameters:

Name Type Description
position Vec2 The world position of the body's local origin.
angle number The world rotation in radians.

Returns: void


setUserData

▸ setUserData(data: any): void

Defined in src/dynamics/Body.ts:327

Parameters:

Name Type
data any

Returns: void


shouldCollide

▸ shouldCollide(that: Body): boolean

Defined in src/dynamics/Body.ts:997

This is used to prevent connected bodies (by joints) from colliding, depending on the joint's collideConnected flag.

Parameters:

Name Type
that Body

Returns: boolean


synchronizeFixtures

▸ synchronizeFixtures(): void

Defined in src/dynamics/Body.ts:593

Update fixtures in broad-phase.

Returns: void


synchronizeTransform

▸ synchronizeTransform(): void

Defined in src/dynamics/Body.ts:586

Returns: void