-
Notifications
You must be signed in to change notification settings - Fork 91
Fix hash grid #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix hash grid #189
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -41,7 +41,7 @@ export default class HashGrid implements CollisionManager { | |||||
| private hashMap: number[][] = []; | ||||||
| private gameLeftX: number = 0; | ||||||
| private gameTopY: number = 0 | ||||||
| private collisionPairsSeen = new Uint32Array(MAX_ENTITY_COUNT * MAX_ENTITY_COUNT / 32) | ||||||
| private collisionPairsSeen = new Uint32Array(MAX_ENTITY_COUNT * (MAX_ENTITY_COUNT - 1) / 2 / 32) | ||||||
|
|
||||||
| public constructor(game: GameServer) { | ||||||
| this.game = game; | ||||||
|
|
@@ -50,7 +50,7 @@ export default class HashGrid implements CollisionManager { | |||||
| public preTick(tick: number): void { | ||||||
| const widthInCells = (this.game.arena.width + (CELL_SIZE - 1)) >> CELL_SHIFT; | ||||||
| const heightInCells = (this.game.arena.height + (CELL_SIZE - 1)) >> CELL_SHIFT; | ||||||
| this.hashMul = widthInCells >> CELL_SHIFT; | ||||||
| this.hashMul = widthInCells; | ||||||
| this.hashMap = Array(widthInCells * heightInCells); | ||||||
| this.queryIdMap.fill(0); | ||||||
| this.lastQueryId = 0; | ||||||
|
|
@@ -65,7 +65,7 @@ export default class HashGrid implements CollisionManager { | |||||
| } | ||||||
|
|
||||||
| public insert(entity: ObjectEntity) { | ||||||
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot insert entity outside of tick"); | ||||||
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot insert() entity outside of tick"); | ||||||
| const { sides, size, width } = entity.physicsData.values; | ||||||
| const { x, y } = entity.positionData.values; | ||||||
| const isLine = sides === 2; | ||||||
|
|
@@ -98,7 +98,7 @@ export default class HashGrid implements CollisionManager { | |||||
| halfWidth: number, | ||||||
| halfHeight: number | ||||||
| ): PackedEntitySet { | ||||||
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot insert entity outside of tick"); | ||||||
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot retrieve() entity outside of tick"); | ||||||
| const result = this.resultSet; | ||||||
| result.clear(); | ||||||
|
|
||||||
|
|
@@ -140,7 +140,7 @@ export default class HashGrid implements CollisionManager { | |||||
| halfHeight: number, | ||||||
| predicate: (entity: ObjectEntity) => boolean | ||||||
| ): ObjectEntity | null { | ||||||
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot insert entity outside of tick"); | ||||||
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot getFirstMatch() outside of tick"); | ||||||
|
|
||||||
| const startX = (centerX - halfWidth - this.gameLeftX) >> CELL_SHIFT; | ||||||
| const startY = (centerY - halfHeight - this.gameTopY) >> CELL_SHIFT; | ||||||
|
|
@@ -180,9 +180,9 @@ export default class HashGrid implements CollisionManager { | |||||
|
|
||||||
| // No longer used | ||||||
| public retrieveEntitiesByEntity(entity: ObjectEntity): PackedEntitySet { | ||||||
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot insert entity outside of tick"); | ||||||
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot retrieveEntitiesByEntity() outside of tick"); | ||||||
| const { sides, size, width } = entity.physicsData.values; | ||||||
| const { x, y } = entity.positionData; | ||||||
| const { x, y } = entity.positionData.values; | ||||||
| const isLine = sides === 2; | ||||||
| const halfWidth = isLine ? size / 2 : size; | ||||||
| const halfHeight = isLine ? width / 2 : size; | ||||||
|
|
@@ -192,7 +192,7 @@ export default class HashGrid implements CollisionManager { | |||||
| public forEachCollisionPair( | ||||||
| callback: (entityA: ObjectEntity, entityB: ObjectEntity) => void | ||||||
| ): void { | ||||||
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot insert entity outside of tick"); | ||||||
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot forEachCollisionPair() entity outside of tick"); | ||||||
|
|
||||||
| const collisionsSeen = this.collisionPairsSeen; | ||||||
| collisionsSeen.fill(0); | ||||||
|
|
@@ -213,27 +213,20 @@ export default class HashGrid implements CollisionManager { | |||||
| const entityB = this.game.entities.inner[eidB] as ObjectEntity; | ||||||
| if (!entityB || entityB.hash === 0) continue; | ||||||
|
|
||||||
| if (eidA < eidB) { | ||||||
| // Prevent extra-cell duplicates | ||||||
| const pairHash = (eidA << MAX_ENTITY_ID_BITS) | eidB; | ||||||
| const pairHashIndex = pairHash >>> 5; | ||||||
| const pairHashBit = 1 << (pairHash & 31); | ||||||
| if ((collisionsSeen[pairHashIndex] & pairHashBit) !== 0) continue; | ||||||
| collisionsSeen[pairHashIndex] |= pairHashBit; | ||||||
|
|
||||||
| // Ensure (x, y) -> x.id < y.id | ||||||
| callback(entityA, entityB); | ||||||
| } else { | ||||||
| // Prevent extra-cell duplicates | ||||||
| const pairHash = (eidB << MAX_ENTITY_ID_BITS) | eidA; | ||||||
| const pairHashIndex = pairHash >>> 5; | ||||||
| const pairHashBit = 1 << (pairHash & 31); | ||||||
| if ((collisionsSeen[pairHashIndex] & pairHashBit) !== 0) continue; | ||||||
| collisionsSeen[pairHashIndex] |= pairHashBit; | ||||||
|
|
||||||
| // Ensure (x, y) -> x.id < y.id | ||||||
| callback(entityB, entityA); | ||||||
| } | ||||||
| // Ensure eidA < eidB for triangular matrix indexing | ||||||
| const [idA, idB] = eidA < eidB ? [eidA, eidB] : [eidB, eidA]; | ||||||
| const [entA, entB] = eidA < eidB ? [entityA, entityB] : [entityB, entityA]; | ||||||
|
|
||||||
| // Triangular matrix index: row * (row - 1) / 2 + col, where row > col | ||||||
|
||||||
| // Triangular matrix index: row * (row - 1) / 2 + col, where row > col | |
| // Triangular matrix index: idB * (idB - 1) / 2 + idA, where idB > idA |
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The triangular index calculation uses JavaScript division which returns a floating-point number. For correctness and to avoid potential precision issues with large IDs, consider using integer division explicitly. Replace line 221 with: const triangularIndex = Math.floor(idB * (idB - 1) / 2) + idA; or use bit shifting for the division by 2: const triangularIndex = ((idB * (idB - 1)) >> 1) + idA;. While the current implementation will likely work, explicit integer operations make the intent clearer and avoid relying on JavaScript's automatic conversion to 32-bit integers during bitwise operations.
| const triangularIndex = (idB * (idB - 1) / 2 + idA); | |
| const triangularIndex = Math.floor(idB * (idB - 1) / 2) + idA; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message says "Cannot forEachCollisionPair() entity outside of tick" but it should probably be "Cannot call forEachCollisionPair() outside of tick" (without the word "entity") to match the pattern of the other error messages and be grammatically correct.