Skip to content
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

Better collision detection #1

Open
orsinium opened this issue Jun 12, 2024 · 0 comments
Open

Better collision detection #1

orsinium opened this issue Jun 12, 2024 · 0 comments

Comments

@orsinium
Copy link
Member

Currently, it builds a bounding box around the segment and checks if the head is in this box. This leads to false positives and for some reason false negatives.

snek/snake.go

Lines 187 to 203 in 1d661b0

func (s Snake) Collides(p firefly.Point) bool {
segment := s.Head.Tail
for segment != nil {
if segment.Tail != nil {
ph := segment.Head
pt := segment.Tail.Head
ph.X, pt.X = denormalizeX(ph.X, pt.X)
ph.Y, pt.Y = denormalizeY(ph.Y, pt.Y)
bbox := NewBBox(ph, pt, snakeWidth/2)
if bbox.Contains(p) {
return true
}
}
segment = segment.Tail
}
return false
}

We need a better algorithm. A single segment can be thought of as 3 circles with 8 pixels diameter. And it's easy to detect collision of 2 circles, we already do that for detecting collision with the apple. And we already know coordinates of 2 circles, these are the joints. And the third circle is right in the middle between them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant