Matter.JS 检测两个静态传感器之间的碰撞 [英] Matter.JS Detect collision between two static sensors
问题描述
我正在尝试向我的游戏世界中添加一些可以四处移动的圆体.我需要检测它们何时发生碰撞,但也要让它们通过.
I'm trying to add some circle bodies to my game world that can be moved around. And i need to detect when they collide, but also let them passtrough.
我试图使圆圈 isSensor 和 Static - 未检测到碰撞.我试图让它们 isSensor 和 NO Static - 检测到碰撞,但它们因为重力/物理而跌落世界.我尝试移除物理但未检测到碰撞.
I've tried to make the circles isSensor and Static - no collision detected. I've tried to make them isSensor and NO Static - collision detected but they fall trough the world beucase of the gravity/physics. I've tried to remove the physics but no collision detected.
怎么办?
推荐答案
你可以使用你自己的碰撞检测和毕达哥拉斯定理,就像这样:
You can use your own collision detection with Pythagorean's theorum, like so:
Events.on(engine, 'beforeUpdate', function() {
var circleAX = circleA.position.x;
var circleAY = circleA.position.y;
var circleBX = circleB.position.x;
var circleBY = circleB.position.y;
var dx = circleAX - circleBX;
var dy = circleAY - circleBY;
var dist = Math.sqrt(dy*dy + dx*dx);
//substitute radiusA and radiusB for the radiuses of the circles
if (dist < radiusA+radiusB) {
//Collision
}
});
为确保它们不会发生碰撞,请使用碰撞过滤器.
如果你追求高效率,这不是最好的方法.但是,除非您要检查数百个物体是否发生碰撞,否则它不应使游戏速度减慢到足以产生真正影响的程度.
To make sure they don't collide, use collisionFilter.
If you're going for high efficiency, this isn't the best way. However, it shouldn't slow the game down enough to make a real difference unless there's hundreds of objects you are checking for collisions.
这篇关于Matter.JS 检测两个静态传感器之间的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!