From b5db27dfc35e61d5db364be3d2ecb2ca1f8898bd Mon Sep 17 00:00:00 2001 From: KeSuave Date: Sun, 15 Dec 2024 08:47:31 -0800 Subject: [PATCH] fix: bodies are now able to sleep (removed circular angle and pos updating) --- src/lib/components/Body.ts | 25 ++----------------------- src/lib/components/Position.ts | 13 ++++++++----- src/lib/components/Rotate.ts | 13 ++++++++----- 3 files changed, 18 insertions(+), 33 deletions(-) diff --git a/src/lib/components/Body.ts b/src/lib/components/Body.ts index c224c15..565e37a 100644 --- a/src/lib/components/Body.ts +++ b/src/lib/components/Body.ts @@ -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. * @@ -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(); @@ -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 }; - }, }; } diff --git a/src/lib/components/Position.ts b/src/lib/components/Position.ts index 7688ab0..ba45d86 100644 --- a/src/lib/components/Position.ts +++ b/src/lib/components/Position.ts @@ -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. * @@ -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); + } } }, diff --git a/src/lib/components/Rotate.ts b/src/lib/components/Rotate.ts index 5ef12e2..b8f4daf 100644 --- a/src/lib/components/Rotate.ts +++ b/src/lib/components/Rotate.ts @@ -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. @@ -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); + } } },