Skip to content

Commit

Permalink
feat: chatgpt optimize utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacek Pietal committed Feb 29, 2024
1 parent 8e4d6eb commit c380404
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 107 deletions.
3 changes: 2 additions & 1 deletion src/base-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
BodyOptions,
ChildrenData,
Data,
InTest,
Leaf,
PotentialVector,
RBush,
Expand Down Expand Up @@ -185,7 +186,7 @@ export class BaseSystem<TBody extends Body = Body>
/**
* remove body aabb from collision tree
*/
remove(body: TBody, equals?: (a: TBody, b: TBody) => boolean): RBush<TBody> {
remove(body: TBody, equals?: InTest<TBody>): RBush<TBody> {
body.system = undefined;

return super.remove(body, equals);
Expand Down
16 changes: 8 additions & 8 deletions src/intersect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ export function pointOnCircle(
* https://stackoverflow.com/a/68197894/1749528
*/
export function circleInCircle(
a: Pick<Circle, "pos" | "r">,
b: Pick<Circle, "pos" | "r">,
bodyA: Pick<Circle, "pos" | "r">,
bodyB: Pick<Circle, "pos" | "r">,
) {
const x1 = a.pos.x;
const y1 = a.pos.y;
const x2 = b.pos.x;
const y2 = b.pos.y;
const r1 = a.r;
const r2 = b.r;
const x1 = bodyA.pos.x;
const y1 = bodyA.pos.y;
const x2 = bodyB.pos.x;
const y2 = bodyB.pos.y;
const r1 = bodyA.r;
const r2 = bodyB.r;
const distSq = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

return distSq + r2 === r1 || distSq + r2 < r1;
Expand Down
5 changes: 5 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ export type SATTest<
Y extends {} = Circle | Polygon | SATPolygon,
> = (bodyA: T, bodyB: Y, response: Response) => boolean;

export type InTest<TBody extends Body = Body> = (
bodyA: TBody,
bodyB: TBody,
) => boolean;

export type TraverseFunction<TBody extends Body = Body> = (
child: Leaf<TBody>,
children: Leaf<TBody>[],
Expand Down
2 changes: 0 additions & 2 deletions src/system.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ describe("GIVEN System", () => {

// <-- works
physics.checkAll(({ a, b }) => {
console.log({ a: a.pos, b: b.pos });

// <-- doesn't collide or run this code
expect(true).toBe(false);
});
Expand Down
2 changes: 1 addition & 1 deletion src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class System<TBody extends Body = Body> extends BaseSystem<TBody> {
return false;
}

const sat = getSATTest(bodyA, bodyB) as SATTest;
const sat = getSATTest(bodyA, bodyB);

// 99% of cases
if (bodyA.isConvex && bodyB.isConvex) {
Expand Down
Loading

0 comments on commit c380404

Please sign in to comment.