-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaabb_collider.go
More file actions
96 lines (81 loc) · 2.17 KB
/
Copy pathaabb_collider.go
File metadata and controls
96 lines (81 loc) · 2.17 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
package engine
import (
"image/color"
"engine/vec2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
type AABBCollider struct {
BaseNode
Position *vec2.T
Size *vec2.T
OnCollision *Signal[Node]
DebugMode bool
}
func NewAABBCollider(name string, position *vec2.T, size *vec2.T) *AABBCollider {
collider := &AABBCollider{
BaseNode: *NewNode(name),
Position: position,
Size: size,
OnCollision: NewSignal[Node](),
}
collider.AddTag("Collider")
return collider
}
func (c *AABBCollider) Update() error {
if err := c.BaseNode.Update(); err != nil {
return err
}
if ebiten.IsKeyPressed(ebiten.KeyF10) {
c.DebugMode = true
}
if ebiten.IsKeyPressed(ebiten.KeyControl) && ebiten.IsKeyPressed(ebiten.KeyF10) {
c.DebugMode = false
}
return nil
}
func (c *AABBCollider) Draw(screen *ebiten.Image) {
c.BaseNode.Draw(screen)
if c.DebugMode {
vector.StrokeRect(
screen,
float32(c.Position.X),
float32(c.Position.Y),
float32(c.Size.X),
float32(c.Size.Y),
1,
color.White,
false,
)
}
}
func (c *AABBCollider) CollidesWithAABB(other *AABBCollider) bool {
return c.Position.X < other.Position.X+other.Size.X &&
c.Position.X+c.Size.X > other.Position.X &&
c.Position.Y < other.Position.Y+other.Size.Y &&
c.Position.Y+c.Size.Y > other.Position.Y
}
func (c *AABBCollider) CollidesWithCircle(other *CircleCollider) bool {
// Check if the circle is inside the AABB
if other.Position.X >= c.Position.X &&
other.Position.X+other.Radius <= c.Position.X+c.Size.X &&
other.Position.Y >= c.Position.Y &&
other.Position.Y+other.Radius <= c.Position.Y+c.Size.Y {
return true
}
// Check if the AABB is inside the circle
if c.Position.X >= other.Position.X &&
c.Position.X+c.Size.X <= other.Position.X+other.Radius &&
c.Position.Y >= other.Position.Y &&
c.Position.Y+c.Size.Y <= other.Position.Y+other.Radius {
return true
}
// Check if the AABB is outside the circle
if c.Position.X > other.Position.X+other.Radius ||
c.Position.X+c.Size.X < other.Position.X ||
c.Position.Y > other.Position.Y+other.Radius ||
c.Position.Y+c.Size.Y < other.Position.Y {
return false
}
return true
}