Skip to content

Commit

Permalink
Insert a sprite into the map when we check for overlaps
Browse files Browse the repository at this point in the history
  • Loading branch information
aznhassan committed Jul 12, 2024
1 parent 3096cd4 commit b369b77
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 32 deletions.
2 changes: 1 addition & 1 deletion libs/game/physics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class ArcadePhysicsEngine extends PhysicsEngine {
s._y = Fx.add(s._y, stepY);

if (!(s.flags & SPRITE_NO_SPRITE_OVERLAPS)) {
this.map.insertSprite(s);
// this.map.insertSprite(s);
}
if (tileMap && tileMap.enabled) {
this.tilemapCollisions(ms, tileMap);
Expand Down
3 changes: 0 additions & 3 deletions libs/game/physics2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,6 @@ class NewArcadePhysicsEngine implements IPhysicsEngine {
s._x = Fx.add(s._x, stepX);
s._y = Fx.add(s._y, stepY);

if (!(s.flags & SPRITE_NO_SPRITE_OVERLAPS)) {
this.map.insertSprite(s);
}
if (tileMap && tileMap.enabled) {
this.tilemapCollisions(ms, tileMap, this.onXAxisCollision, this.onYAxisCollision);
}
Expand Down
45 changes: 17 additions & 28 deletions libs/game/spritemap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace sprites {
export interface ISpriteMap {
insertSprite(sprite: Sprite): void;
getOverlappingSprites(sprite: Sprite): Sprite[];
draw(camera: scene.Camera): void;
reset(sprites: Sprite[]): void;
Expand All @@ -24,8 +23,23 @@ namespace sprites {
const neighbors: { [key: string]: Sprite } = {};
const layer = sprite.layer;

for (const key of this.getBucketsKeys(sprite)) {
this.mergeAt(key, layer, neighbors)
const left = sprite.left;
const top = sprite.top;
const xn = Math.idiv(
sprite.width + this.cellWidth - 1,
this.cellWidth
);
const yn = Math.idiv(
sprite.height + this.cellHeight - 1,
this.cellHeight
);
for (let x = 0; x <= xn; x++) {
for (let y = 0; y <= yn; y++) {
const key = this.key(left + Math.min(sprite.width, x * this.cellWidth),
top + Math.min(sprite.height, y * this.cellHeight))
this.insertAt(key, sprite)
this.mergeAt(key, layer, neighbors)
}
}
neighbors[sprite.id] = undefined;
return neighbors;
Expand Down Expand Up @@ -109,31 +123,6 @@ namespace sprites {
if (bucket.indexOf(sprite) < 0) bucket.push(sprite);
}

private getBucketsKeys(sprite: Sprite): number[] {
const left = sprite.left;
const top = sprite.top;
const xn = Math.idiv(
sprite.width + this.cellWidth - 1,
this.cellWidth
);
const yn = Math.idiv(
sprite.height + this.cellHeight - 1,
this.cellHeight
);
let keys = []
for (let x = 0; x <= xn; x++)
for (let y = 0; y <= yn; y++)
keys.push(this.key(left + Math.min(sprite.width, x * this.cellWidth),
top + Math.min(sprite.height, y * this.cellHeight)))
return keys
}

public insertSprite(sprite: Sprite) {
for (const key of this.getBucketsKeys(sprite)) {
this.insertAt(key, sprite)
}
}

private mergeAt(key: number, layer: number, neighbors: { [key: string]: Sprite }) {
const bucket = this.buckets[key];
if (bucket) {
Expand Down

0 comments on commit b369b77

Please sign in to comment.