matlabe二维球撞墙

来源:百度知道 编辑:UC知道 时间:2024/07/04 11:12:48
在判断球反弹墙面的时候,为什么会有下面这么复杂的判断,需要考虑速度的正负呢?
function newCircle = reflectCircle( circle, board)
newCircle = circle;
if (circle.xVelocity >= 0 && circle.yVelocity >= 0)&&(circle.xpos + circle.radius >= board.xmax && circle.ypos + circle.radius >= board.ymax))=1
newCircle.xVelocity = -1*circle.xVelocity;
newCircle.yVelocity = -1*circle.yVelocity;
elseif (circle.xpos + circle.radius >= board.xmax)
newCircle.xVelocity = -1*circle.xVelocity;

end
elseif (circle.xVelocity >= 0 && circle.yVelocity < 0)
if (circle.xpos + circle.radius >= board.xmax && circle.ypos - circle.radius <= board.ymin)
newCircle.xVelocity = -1*circle.xVelocity;
newCircle.yVelocity = -1*circle.yVelocity;
elseif (circle.xpos + circle.radius >= board.xmax)
newCircle.xVelocity = -1*circle.xVelocity;

很简单,球是一个有半径的刚性球,球被一个方形围墙围起来了。

作者考虑速度的正负,是想知道球可能撞到哪面墙

如果x或y坐标的绝对值加上半径的和 大于边界 就判定碰撞,速度方向改变。

不要同时考虑x,y那样情况太多。
把Vx,Vy独立考虑
如果abs(xmax)=abs(xmin)的话

直接if abs(sign(Vx)*x-abs(xmax))<r
Vx=-Vx;

同理if abs(sign(Vy)*x-abs(ymax))<r
Vy=-Vy;
两句话足够了