Skip to content

Commit

Permalink
feat!: rename body active property to enabled and set newContacts
Browse files Browse the repository at this point in the history
  • Loading branch information
zOadT committed Aug 12, 2023
1 parent 509c4f0 commit 71204d2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 37 deletions.
2 changes: 1 addition & 1 deletion example/PolyShapes.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ testbed.keydown = function(code, char) {
case 'Z':
for (var i = 0; i < bodies.length; i += 2) {
var body = bodies[i];
body.setActive(!body.isActive());
body.setEnabled(!body.isEnabled());
}
break;

Expand Down
55 changes: 29 additions & 26 deletions src/dynamics/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ export interface BodyDef {
*/
awake?: boolean;
/**
* Does this body start out active?
* Does this body start out enabled?
*/
active?: boolean;
enabled?: boolean;
userData?: any;
}

Expand All @@ -131,7 +131,7 @@ const BodyDefDefault: BodyDef = {

allowSleep : true,
awake : true,
active : true,
enabled : true,

userData : null
};
Expand Down Expand Up @@ -185,7 +185,7 @@ export class Body {
/** @internal */ m_autoSleepFlag: boolean;
/** @internal */ m_bulletFlag: boolean;
/** @internal */ m_fixedRotationFlag: boolean;
/** @internal */ m_activeFlag: boolean;
/** @internal */ m_enabledFlag: boolean;
/** @internal */ m_islandFlag: boolean;
/** @internal */ m_toiFlag: boolean;
/** @internal */ m_userData: unknown;
Expand Down Expand Up @@ -234,7 +234,7 @@ export class Body {
this.m_autoSleepFlag = def.allowSleep;
this.m_bulletFlag = def.bullet;
this.m_fixedRotationFlag = def.fixedRotation;
this.m_activeFlag = def.active;
this.m_enabledFlag = def.enabled;

this.m_islandFlag = false;
this.m_toiFlag = false;
Expand Down Expand Up @@ -486,39 +486,42 @@ export class Body {
}
}

isActive(): boolean {
return this.m_activeFlag;
isEnabled(): boolean {
return this.m_enabledFlag;
}

/**
* 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
* Allow a body to be disabled. A disabled 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 disabled bodies.
* Fixtures on a disabled body are implicitly disabled and will not
* participate in collisions, ray-casts, or queries.
* Joints connected to a disabled body are implicitly disabled.
* An diabled body is still owned by a b2World object and remains in the body
* list.
*/
setActive(flag: boolean): void {
setEnabled(flag: boolean): void {
_ASSERT && console.assert(this.isWorldLocked() == false);

if (flag == this.m_activeFlag) {
if (flag == this.m_enabledFlag) {
return;
}

this.m_activeFlag = !!flag;
this.m_enabledFlag = !!flag;

if (this.m_activeFlag) {
if (this.m_enabledFlag) {
// Create all proxies.
const broadPhase = this.m_world.m_broadPhase;
for (let f = this.m_fixtureList; f; f = f.m_next) {
f.createProxies(broadPhase, this.m_xf);
}
// Contacts are created the next time step.
// Contacts are created at the beginning of the next time step.
this.m_world.m_newContacts = true;

} else {
// Destroy all proxies.
Expand Down Expand Up @@ -1021,7 +1024,7 @@ export class Body {
return null;
}

if (this.m_activeFlag) {
if (this.m_enabledFlag) {
const broadPhase = this.m_world.m_broadPhase;
fixture.createProxies(broadPhase, this.m_xf);
}
Expand All @@ -1036,7 +1039,7 @@ export class Body {

// Let the world know we have a new fixture. This will cause new contacts
// to be created at the beginning of the next time step.
this.m_world.m_newFixture = true;
this.m_world.m_newContacts = true;

return fixture;
}
Expand Down Expand Up @@ -1124,7 +1127,7 @@ export class Body {
}
}

if (this.m_activeFlag) {
if (this.m_enabledFlag) {
const broadPhase = this.m_world.m_broadPhase;
fixture.destroyProxies(broadPhase);
}
Expand Down
6 changes: 3 additions & 3 deletions src/dynamics/Joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ export abstract class Joint {
}

/**
* Short-cut function to determine if either body is inactive.
* Short-cut function to determine if either body is enabled.
*/
isActive(): boolean {
return this.m_bodyA.isActive() && this.m_bodyB.isActive();
isEnabled(): boolean {
return this.m_bodyA.isEnabled() && this.m_bodyB.isEnabled();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/dynamics/Solver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class Solver {
continue;
}

if (seed.isAwake() == false || seed.isActive() == false) {
if (seed.isAwake() == false || seed.isEnabled() == false) {
continue;
}

Expand All @@ -205,7 +205,7 @@ export class Solver {
while (stack.length > 0) {
// Grab the next body off the stack and add it to the island.
const b = stack.pop();
_ASSERT && console.assert(b.isActive() == true);
_ASSERT && console.assert(b.isEnabled() == true);
this.addBody(b);

// Make sure the body is awake (without resetting sleep timer).
Expand Down Expand Up @@ -262,7 +262,7 @@ export class Solver {
const other = je.other;

// Don't simulate joints connected to inactive bodies.
if (other.isActive() == false) {
if (other.isEnabled() == false) {
continue;
}

Expand Down
8 changes: 4 additions & 4 deletions src/dynamics/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class World {
/** @internal */ m_allowSleep: boolean;
/** @internal */ m_gravity: Vec2;
/** @internal */ m_clearForces: boolean;
/** @internal */ m_newFixture: boolean;
/** @internal */ m_newContacts: boolean;
/** @internal */ m_locked: boolean;
/** @internal */ m_warmStarting: boolean;
/** @internal */ m_continuousPhysics: boolean;
Expand Down Expand Up @@ -158,7 +158,7 @@ export class World {
this.m_gravity = Vec2.clone(def.gravity);

this.m_clearForces = true;
this.m_newFixture = false;
this.m_newContacts = false;
this.m_locked = false;

// These are for debugging the solver.
Expand Down Expand Up @@ -794,9 +794,9 @@ export class World {
positionIterations = positionIterations || this.m_positionIterations;

// If new fixtures were added, we need to find the new contacts.
if (this.m_newFixture) {
if (this.m_newContacts) {
this.findNewContacts();
this.m_newFixture = false;
this.m_newContacts = false;
}

this.m_locked = true;
Expand Down

0 comments on commit 71204d2

Please sign in to comment.