-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcollision_optimizer.go
More file actions
126 lines (105 loc) · 3.8 KB
/
Copy pathcollision_optimizer.go
File metadata and controls
126 lines (105 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spx
import "github.com/goplus/spx/v2/internal/base/collision"
// ======================== Collision Optimization System ========================
// This file implements spatial partitioning and AABB (Axis-Aligned Bounding Box)
// based broad-phase collision detection to reduce expensive pixel-perfect checks.
// ======================== Configuration ========================
// defaultSpatialHashCellSize defines the default cell size for spatial hash grid.
// This value can be tuned based on your game's average sprite size:
const defaultSpatialHashCellSize = 100.0
func newSpriteAABB(sprite *SpriteImpl) *collision.Entry[*SpriteImpl] {
bounds := sprite.bounds()
if bounds == nil {
return nil
}
return &collision.Entry[*SpriteImpl]{
Value: sprite,
Box: collision.AABB{
MinX: bounds.Position.X,
MinY: bounds.Position.Y,
MaxX: bounds.Position.X + bounds.Size.X,
MaxY: bounds.Position.Y + bounds.Size.Y,
},
}
}
// buildSpatialHashForNames builds a spatial hash with sprites matching the given name filter.
// Uses a reusable spatial hash to avoid repeated allocations.
func (p *Game) buildSpatialHashForNames(dst *SpriteImpl, nameFilter func(string) bool) *collision.SpatialHash[*SpriteImpl] {
// Lazy initialization of the reusable spatial hash
if p.spatialHash == nil {
p.spatialHash = collision.NewSpatialHash[*SpriteImpl](defaultSpatialHashCellSize)
}
// Clear and reuse the existing spatial hash
p.spatialHash.Clear()
for _, item := range p.shapeMgr.items {
if sp, ok := item.(*SpriteImpl); ok && sp != dst {
if nameFilter(sp.name) && sp.spriteState.IsVisible && !sp.spriteState.IsDying && sp.runtimeState.SyncSprite != nil {
aabb := newSpriteAABB(sp)
if aabb != nil {
p.spatialHash.Insert(aabb)
}
}
}
}
return p.spatialHash
}
// findCollisionsInSpatialHash performs AABB and pixel-perfect collision detection.
func findCollisionsInSpatialHash(
dstAABB *collision.Entry[*SpriteImpl],
spatialHash *collision.SpatialHash[*SpriteImpl],
findFirst bool,
) []*SpriteImpl {
var results []*SpriteImpl
// Query spatial hash for potential collisions
potentialCollisions := spatialHash.Query(dstAABB.Box)
// AABB intersection and pixel-perfect collision tests
for _, candidateAABB := range potentialCollisions {
if !dstAABB.Box.Intersects(candidateAABB.Box) {
continue
}
// Pixel-perfect collision detection (narrow-phase)
if candidateAABB.Value.touchingSprite(dstAABB.Value) {
results = append(results, candidateAABB.Value)
if findFirst {
return results
}
}
}
return results
}
// findTouchingSpriteOptimized uses spatial partitioning for efficient collision detection.
func (p *Game) findTouchingSpriteOptimized(dst *SpriteImpl, name string) *SpriteImpl {
if dst == nil || dst.runtimeState.SyncSprite == nil {
return nil
}
// Create AABB for the target sprite
dstAABB := newSpriteAABB(dst)
if dstAABB == nil {
return nil
}
// Build spatial hash with name filter
spatialHash := p.buildSpatialHashForNames(dst, func(spriteName string) bool {
return spriteName == name
})
// Find first collision
results := findCollisionsInSpatialHash(dstAABB, spatialHash, true)
if len(results) > 0 {
return results[0]
}
return nil
}