我做了支钢笔,其中我有一个function Rectangle(),它创建一个矩形并处理与创建的对象的冲突。
但我只能让两个边工作(顶和左),其他的工作像反向和拉球在碰撞,而不是反弹他们,不知怎的,我不知道如何解决这个问题。
我已经玩了好几个小时了,这是当前的代码:
// Top
if (bp.y > posY - brd && bp.y < yy + brd && bp.x > posX && bp.x < xx) {
ball.velocity.y *= ball.restitution;
ball.position.y = posY - brd;
}
// Bottom
if (bp.y > yy + brd && bp.y < posY - brd && bp.x > posX && bp.x < xx) {
ball.velocity.y *= ball.restitution;
ball.position.y = yy + brd;
}
// Left
if (bp.x > posX - brd && bp.x < xx + brd && bp.y > posY && bp.y < yy) {
ball.velocity.x *= ball.restitution;
ball.position.x = posX - brd;
}
// Right
if (bp.x > xx + brd && bp.x < posX - brd && bp.y > posY && bp.y < yy) {
ball.velocity.x *= ball.restitution;
ball.position.x = xx + brd;
}在第116行,你会找到变量,这样你就不会被条件弄糊涂了。
发布于 2018-04-03 14:25:46
试试这个:
// Top
if (bp.y > posY - brd && bp.y < posY && bp.x > posX && bp.x < xx) {
ball.velocity.y *= ball.restitution;
ball.position.y = posY - brd;
}
// Bottom
if (bp.y < yy + brd && bp.y > yy && bp.x > posX && bp.x < xx) {
ball.velocity.y *= ball.restitution;
ball.position.y = yy + brd;
}
// Left
if (bp.x > posX - brd && bp.x < posX && bp.y > posY && bp.y < yy) {
ball.velocity.x *= ball.restitution;
ball.position.x = posX - brd;
}
// Right
if (bp.x < xx + brd && bp.x > xx && bp.y > posY && bp.y < yy) {
ball.velocity.x *= ball.restitution;
ball.position.x = xx + brd;
}摘要:在底部和右边的检查中,我在第一个子句中翻转了操作符,并在第二个子句中更改了右操作数。
https://stackoverflow.com/questions/49631846
复制相似问题