如何用vx和vy进行碰撞检测?
How to collision detect objects with vx and vy?
我一直在努力找出如何阻止玩家以正确的方式移动。但是,我现在一直在做的方式完全阻止了玩家移动。
我希望玩家在接触到方块碰撞区域的侧面时停止水平移动,如果它接触到方块碰撞区域的顶部或底部,我希望它只停止垂直移动。这样一来,您仍然可以在触摸侧面时上下移动,在触摸顶部或底部时仍然可以左右移动。谢谢
if (player.collisionArea.hitTestObject(block.collisionArea))
{
player.y -= vy;
player.x -= vx;
}
首先,这个问题非常真实 - 下一次,请务必澄清问题的背景(即我正在制作一款游戏actionscript 2d 图形,等等等等等等)。
无论如何,我会把我在开发 javascript 游戏的那一天写的代码的一部分复制给你。由于我不知道你的对象是如何排列的,我将以一种只介绍通用算法的方式对其进行修改。
// player: a class that holds an array of moves, and other relevant information. Varys on your program.
var optimizePosition = function (player) {
//after each time the player attempts to move, i check if he touches of the blocks. if he does, i move him back.
foreach(Block block in blocks)// replace this with an iterator for all the blocks
{
if (player.x-block.x < 32 || player.y-block.y < 32 ) // if player is touching a block:
undoLastMove(getLastMove(player),player);
}
// a little extra: always good to check if he's going out of the screen in the same function:
if(player.x > canvas.width - 64 || player.y > canvas.height - 64)
undoLastMove(getLastMove(player),player);
}
每次玩家移动时,我都会调用函数:
optimizePosition(player);
undoLastMove(player)
的内容将包含您上面编写的代码。
你的基本方法是移动你的物体,测试当前位置是否撞到你的障碍物,如果撞到则将其移回。这可以很容易地分离出 x 轴和 y 轴(为清楚起见减少代码):
player.x += vx;
if(player.hitTestObject(block)){
player.x -= vx;
}
player.y += vy;
if(player.hitTestObject(block)){
player.y -= vy;
}
但是,这有潜在的问题。首先,DisplayObject
/x
和y
四舍五入为“twips”,所以你对vx
和vy
的加减不一定会导致物体准确地回到原来的位置。这会产生物体卡在墙壁等问题。听起来很奇怪?试试这个:
var s:Sprite = new Sprite();
s.x = 1.1925;
trace(s.x); // 1.15
所以,为了避免这种情况,您可以单独存储位置,而不是依赖 DisplayObject
x
和 y
属性.
var oldPosition:Point = new Point(player.x, player.y);
player.x += vx;
if(player.hitTestObject(block)){
player.x = oldPosition.x;
}
player.y += vy;
if(player.hitTestObject(block)){
player.y = oldPosition.y;
}
其次,我想我只想提一下,虽然 hitTestObject()
很方便 API 检查两个显示对象边界矩形的交集,但根据我的经验,当你将它与移动和碰撞混合使用时response 它开始崩溃,因为它依赖于显示器的状态,它会受到诸如 twips 舍入之类的奇怪现象的影响,并且在时间上不灵活(例如,你不能投影你所有的动作,然后检查碰撞,因为您需要移动显示对象才能从 hitTestObject()
获得有效结果)。 IMO 更好的方法是使碰撞检测完全基于数学,例如圆 intersection/distance 检查。 hitTestObject()
真正做的只是 Rectangle/intersects()
基于显示对象边界。基于纯数学的碰撞检测需要更多的设置工作,但更灵活并且根据我的经验更可预测。也许只是对未来的思考!
我一直在努力找出如何阻止玩家以正确的方式移动。但是,我现在一直在做的方式完全阻止了玩家移动。 我希望玩家在接触到方块碰撞区域的侧面时停止水平移动,如果它接触到方块碰撞区域的顶部或底部,我希望它只停止垂直移动。这样一来,您仍然可以在触摸侧面时上下移动,在触摸顶部或底部时仍然可以左右移动。谢谢
if (player.collisionArea.hitTestObject(block.collisionArea))
{
player.y -= vy;
player.x -= vx;
}
首先,这个问题非常真实 - 下一次,请务必澄清问题的背景(即我正在制作一款游戏actionscript 2d 图形,等等等等等等)。
无论如何,我会把我在开发 javascript 游戏的那一天写的代码的一部分复制给你。由于我不知道你的对象是如何排列的,我将以一种只介绍通用算法的方式对其进行修改。
// player: a class that holds an array of moves, and other relevant information. Varys on your program.
var optimizePosition = function (player) {
//after each time the player attempts to move, i check if he touches of the blocks. if he does, i move him back.
foreach(Block block in blocks)// replace this with an iterator for all the blocks
{
if (player.x-block.x < 32 || player.y-block.y < 32 ) // if player is touching a block:
undoLastMove(getLastMove(player),player);
}
// a little extra: always good to check if he's going out of the screen in the same function:
if(player.x > canvas.width - 64 || player.y > canvas.height - 64)
undoLastMove(getLastMove(player),player);
}
每次玩家移动时,我都会调用函数:
optimizePosition(player);
undoLastMove(player)
的内容将包含您上面编写的代码。
你的基本方法是移动你的物体,测试当前位置是否撞到你的障碍物,如果撞到则将其移回。这可以很容易地分离出 x 轴和 y 轴(为清楚起见减少代码):
player.x += vx;
if(player.hitTestObject(block)){
player.x -= vx;
}
player.y += vy;
if(player.hitTestObject(block)){
player.y -= vy;
}
但是,这有潜在的问题。首先,DisplayObject
/x
和y
四舍五入为“twips”,所以你对vx
和vy
的加减不一定会导致物体准确地回到原来的位置。这会产生物体卡在墙壁等问题。听起来很奇怪?试试这个:
var s:Sprite = new Sprite();
s.x = 1.1925;
trace(s.x); // 1.15
所以,为了避免这种情况,您可以单独存储位置,而不是依赖 DisplayObject
x
和 y
属性.
var oldPosition:Point = new Point(player.x, player.y);
player.x += vx;
if(player.hitTestObject(block)){
player.x = oldPosition.x;
}
player.y += vy;
if(player.hitTestObject(block)){
player.y = oldPosition.y;
}
其次,我想我只想提一下,虽然 hitTestObject()
很方便 API 检查两个显示对象边界矩形的交集,但根据我的经验,当你将它与移动和碰撞混合使用时response 它开始崩溃,因为它依赖于显示器的状态,它会受到诸如 twips 舍入之类的奇怪现象的影响,并且在时间上不灵活(例如,你不能投影你所有的动作,然后检查碰撞,因为您需要移动显示对象才能从 hitTestObject()
获得有效结果)。 IMO 更好的方法是使碰撞检测完全基于数学,例如圆 intersection/distance 检查。 hitTestObject()
真正做的只是 Rectangle/intersects()
基于显示对象边界。基于纯数学的碰撞检测需要更多的设置工作,但更灵活并且根据我的经验更可预测。也许只是对未来的思考!