-
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
Conversation
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.
Pull request overview
This pull request fixes duplicate entity ticks and corrects the hash grid implementation. The changes address critical bugs in the collision detection system and entity management that could cause incorrect game behavior.
Changes:
- Fixed hash grid collision pair tracking by implementing triangular matrix indexing, reducing memory usage from O(n²) to O(n²/2)
- Corrected hash multiplier calculation in the spatial hash grid by removing an erroneous bit shift operation
- Prevented duplicate tick() calls for CameraEntity instances by adding proper type checking in the entity manager
- Cleaned up unused imports and fixed property access patterns
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/Physics/HashGrid.ts | Refactored collision pair deduplication to use triangular matrix indexing, fixed hash multiplier calculation, corrected positionData access, and improved error messages |
| src/Native/Manager.ts | Added type guards to prevent CameraEntity from being ticked twice (once in main loop and once in dedicated camera loop) |
| src/Entity/Misc/Mothership.ts | Removed unused ArenaEntity import, keeping only the used ArenaState export |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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 |
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 comment says "row * (row - 1) / 2 + col" but the actual formula uses idB and idA. For clarity, consider updating the comment to match the variable names: "Triangular matrix index: idB * (idB - 1) / 2 + idA, where idB > idA".
| // Triangular matrix index: row * (row - 1) / 2 + col, where row > col | |
| // Triangular matrix index: idB * (idB - 1) / 2 + idA, where idB > idA |
| const [entA, entB] = eidA < eidB ? [entityA, entityB] : [entityB, entityA]; | ||
|
|
||
| // Triangular matrix index: row * (row - 1) / 2 + col, where row > col | ||
| const triangularIndex = (idB * (idB - 1) / 2 + 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; |
| 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"); |
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 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.
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot forEachCollisionPair() entity outside of tick"); | |
| if (this.isLocked) throw new Error("HashGrid is locked! Cannot call forEachCollisionPair() outside of tick"); |
|
Should probably work |
Why:
Fixes dup ticks & hash grid
Summarize what's being changed (include any screenshots, code, or other media if available):
Confirm the following: