dongyong1897 2019-05-16 05:20
浏览 82
已采纳

我的游戏中的圆形碰撞比图像稍大

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(s.center.X-p.center.X, 2)+math.Pow(s.center.Y-p.center.Y, 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.
        Moved(g.center)
    g.sprite.Draw(win, mat)
}

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

  • 写回答

1条回答 默认 最新

  • 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(s.center.X-p.center.X, 2)+math.Pow(s.center.Y-p.center.Y, 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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置