简体   繁体   English

与墙碰撞时如何计算球的坐标?

[英]How to calculate coordinates of ball when collided with a wall?

I'm developing a game like bubble shooter and need to implement bounce of ball off the vertical wall. 我正在开发类似泡泡射击的游戏,并且需要实现垂直墙反弹球的功能。 I have the following inputs: 我有以下输入:

  1. Starting position of bubble: {left: 382, right: 378} 气泡的起始位置: {left: 382, right: 378}
  2. Firing angle in radians: 1.34 弧度射击角度: 1.34
  3. Game board sizes: {width: 760, height: 620} . 游戏板尺寸: {width: 760, height: 620}
  4. Sizes of the bubble are {width: 44px, height: 44px} 气泡的大小为{width: 44px, height: 44px}

I have two walls to the left side of the bubble and to the right side of the bubble. 我在气泡的左侧和气泡的右侧有两堵墙。

How could I calculate bubble's center point coordinates to stop it on collided left of right wall? 我如何计算气泡的中心点坐标以将其停在右墙左侧碰撞的地方?

在此处输入图片说明

If the dimensions for the bubble are 44x44, I assume it means that the bounding box is that size (because you don't measure circles with width and height). 如果气泡的尺寸为44x44,则表示边界框就是该尺寸(因为您没有测量宽度和高度的圆)。

If that's the case, than you have something like 如果是这样,那么您会遇到类似

框

That means that the diameter of the circle is 44px, which means the radius of the circle is 22px. 这意味着圆的直径为44像素,这意味着圆的半径为22像素。 The distance from the wall to the center of the circle, assuming the wall is tangent to the circle, would be 1r or 22px. 假设墙与圆相切,则墙到圆心的距离将为1r或22px。

If you're counting from the top left of the bounding box, you want to add 22px to both the x and the y coordinate, and that would give you the coordinates of the center. 如果要从边界框的左上角开始计数,则要向x和y坐标都添加22px,这将为您提供中心坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM