Skip to content

Commit

Permalink
fix: bodies are now able to sleep (removed circular angle and pos upd…
Browse files Browse the repository at this point in the history
…ating)
  • Loading branch information
KeSuave committed Dec 15, 2024
1 parent b756aaf commit b5db27d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 33 deletions.
25 changes: 2 additions & 23 deletions src/lib/components/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ export interface KPBodyComp extends Comp {
* @type {Tag[]}
*/
collisionIgnore: Tag[];

/**
* @internal
* The color to use for debugging the physics body.
*
* @type {{ r: number; g: number; b: number }}
*/
inspectColor: { r: number; g: number; b: number };

/**
* Applies an angular impulse to the body.
*
Expand Down Expand Up @@ -919,8 +910,8 @@ export default function body(
});
},
fixedUpdate(this: BodyCompThis) {
this.setKPPosition(this.body.getPosition());
this.setKPAngle(this.body.getAngle());
this.setKPPosition(this.body.getPosition(), true);
this.setKPAngle(this.body.getAngle(), true);
},
destroy(this: BodyCompThis) {
const world = this.body.getWorld();
Expand All @@ -929,18 +920,6 @@ export default function body(

_body = null;
},

get inspectColor() {
if (!this.body) return { r: 0, g: 0, b: 0 };

if (this.body.isDynamic()) {
return { r: 0, g: 191, b: 255 };
} else if (this.body.isKinematic()) {
return { r: 238, g: 130, b: 238 };
}

return { r: 255, g: 255, b: 255 };
},
};
}

Expand Down
13 changes: 8 additions & 5 deletions src/lib/components/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export interface KPPosComp extends Comp {
* Sets the position of the object.
*
* @param {Vec2Value} pos
* @param {boolean} fromBody This is an internal flag that states it was updated by the body
*/
setKPPosition(pos: Vec2Value): void;
setKPPosition(pos: Vec2Value, fromBody?: boolean): void;
/**
* Moves the object by a given amount.
*
Expand Down Expand Up @@ -76,13 +77,15 @@ export default function pos(k: KAPLAYCtx, ...args: KPVec2Args): KPPosComp {

return k2pVec2(this.pos);
},
setKPPosition(this: PosCompThis, pos: Vec2Value) {
setKPPosition(this: PosCompThis, pos: Vec2Value, fromBody = false) {
this.pos = p2kVec2(k, pos);

const bodyComp = this.c("kpBody") as KPBodyComp | null;
if (!fromBody) {
const bodyComp = this.c("kpBody") as KPBodyComp | null;

if (bodyComp && bodyComp.body) {
bodyComp.body.setPosition(pos);
if (bodyComp && bodyComp.body) {
bodyComp.body.setPosition(pos);
}
}
},

Expand Down
13 changes: 8 additions & 5 deletions src/lib/components/Rotate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ export interface KPRotateComp extends Comp {
* Sets the rotation angle of the object in radians.
*
* @param {number} angle
* @param {boolean} fromBody This is an internal flag that states it was updated by the body
*/
setKPAngle(angle: number): void;
setKPAngle(angle: number, fromBody?: boolean): void;

/**
* Rotates the object by a given angle in radians.
Expand Down Expand Up @@ -45,13 +46,15 @@ export default function rotate(k: KAPLAYCtx, angle = 0): KPRotateComp {

return k.deg2rad(this.angle);
},
setKPAngle(this: RotateCompThis, angle: number) {
setKPAngle(this: RotateCompThis, angle: number, fromBody = false) {
this.angle = k.rad2deg(angle);

const bodyComp = this.c("kpBody") as KPBodyComp | null;
if (!fromBody) {
const bodyComp = this.c("kpBody") as KPBodyComp | null;

if (bodyComp && bodyComp.body) {
bodyComp.body.setAngle(angle);
if (bodyComp && bodyComp.body) {
bodyComp.body.setAngle(angle);
}
}
},

Expand Down

0 comments on commit b5db27d

Please sign in to comment.