-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle_collider.go
More file actions
75 lines (62 loc) · 1.58 KB
/
Copy pathcircle_collider.go
File metadata and controls
75 lines (62 loc) · 1.58 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
package engine
import (
"image/color"
"math"
"engine/vec2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
)
type CircleCollider struct {
BaseNode
Position *vec2.T
Radius float64
OnCollision *Signal[Node]
DebugMode bool
}
func NewCircleCollider(name string, position *vec2.T, radius float64) *CircleCollider {
collider := &CircleCollider{
BaseNode: *NewNode(name),
Position: position,
Radius: radius,
OnCollision: NewSignal[Node](),
}
collider.AddTag("Collider")
return collider
}
func (c *CircleCollider) 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 *CircleCollider) Draw(screen *ebiten.Image) {
c.BaseNode.Draw(screen)
if c.DebugMode {
vector.StrokeCircle(
screen,
float32(c.Position.X+c.Radius),
float32(c.Position.Y+c.Radius),
float32(c.Radius),
1,
color.White,
false,
)
}
}
func (c *CircleCollider) CollidesWithCircle(other *CircleCollider) bool {
// Calculate distance between centers
dx := c.Position.X + c.Radius - (other.Position.X + other.Radius)
dy := c.Position.Y + c.Radius - (other.Position.Y + other.Radius)
distance := math.Sqrt(dx*dx + dy*dy)
// If distance is less than sum of radii, they are colliding
return distance < (c.Radius + other.Radius)
}
func (c *CircleCollider) CollidesWithAABB(other *AABBCollider) bool {
return other.CollidesWithCircle(c)
}