2019-05-16 05:20 阅读 80


I'm implementing circular collision in my game made in Go using a simple rendering framework called Pixel.

The collision works fine with shapes like a drawn circle

But when I try to use it with an image instead of a rendered shape, it thinks it collides slightly before it reaches the image. The larger the image, the larger the error.

Here is where it collides on the images:

Smaller image: enter image description here

Larger image: enter image description here

The collision is done like so:

[s being the solar panel, seen in the "smaller image", and p being the player.]

math.Pow(math.Pow(, 2)+math.Pow(, 2), 0.5) <= s.size.X

The size.X and size.Y of each image is the same since it's a circle

The centers are calculated correctly and this exact line works with shapes [you can see the yellow outline in the first image, that's the shape it works with.]

Am I doing something wrong? Why would there be an error caused by this when the calculation has nothing to do with the image itself?

Here's how the images are rendered, I assume the problem lies with that somehow:

[g being the generator, seen in the 'Larger image'

func (g Generator) render(win *pixelgl.Window, imd *imdraw.IMDraw) {
    mat := pixel.IM.
    g.sprite.Draw(win, mat)

The shape is drawn based on it's center as well.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doujuanju3076 doujuanju3076 2019-05-16 05:40

    If both colliding objects are circles with radii R1 and R2, then collision occurs when

    math.Pow(math.Pow(, 2)+math.Pow(, 2), 0.5) <= 
     s.radius + p.radius

    What is s.size - image square side? In this case you have to use size/2 to get radius.
    Why do you ignore size/radius of p?

    These two reasons together might cause described behavour (collision condition fires when center-center distance becomes equal to s side size)

    点赞 评论 复制链接分享