-
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
Merged
Merged
Fix hash grid #189
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.