具有受约束主体的Matter.js碰撞过滤器

时间:2019-10-22 12:40:23

标签: matter.js

我正在努力使something.js引起关注。我的瞳孔和虹膜被约束在一起。我的问题是我需要使两个物体都忽略碰撞,以便可以将一个物体放置在另一个物体上。

var defaultCategory = 0x0001

var iris = Bodies.circle(0, 0, 20);
var pupil = Bodies.circle(300, 200, 30)
var ground = Bodies.rectangle(400, 380, 810, 60, { isStatic: true });


 var bodyA = Bodies.polygon(100, 100, 6, 20,{
   collisionFilter: {
     mask: defaultCategory
   }
 });
    var bodyB = Bodies.polygon(200, 100, 10, 50,{
       collisionFilter: {
                category: defaultCategory
            }
    });

    var constraint = Constraint.create({
        bodyA: bodyA,
        pointA: { x: -10, y: -10 },
        bodyB: bodyB,
        pointB: { x: -10, y: -10 },
        length: 40
    });

    World.add(world, [pupil, iris, ground, constraint]);

1 个答案:

答案 0 :(得分:0)

不确定是否有更好的方法,但是我没有使用类别和遮罩,而是使用组,这允许两个主体重叠,请参见以下内容:

var Engine = Matter.Engine,
    Render = Matter.Render,
    Runner= Matter.Runner,
    World = Matter.World,
    Bodies = Matter.Bodies,
    Composites = Matter.Composites,
    Common = Matter.Common,
    Constraint = Matter.Constraint,
    Body = Matter.Body

// no longer using category    
var defaultCategory = 0x0001

//using group instead
var group = Body.nextGroup(true)


var engine = Engine.create(),
    world = engine.world;

var render = Render.create({
                element: document.body,
                engine: engine,
                options: {
                    width: 800,
                    height: 400,
                    wireframes: false
                }
             });


var ground = Bodies.rectangle(400, 380, 810, 60, { isStatic: true });


 var bodyA = Bodies.polygon(100, 100, 6, 20,{
   collisionFilter: { group: group }
 });
    var bodyB = Bodies.polygon(200, 100, 10, 50,{
       collisionFilter: { group: group }
    });

    var constraint = Constraint.create({
        bodyA: bodyA,
        pointA: { x: -10, y: -10 },
        bodyB: bodyB,
        pointB: { x: -10, y: -10 },
        length: 40
    });

    World.add(world, [bodyA, bodyB, ground, constraint]);

// World.add(world, [leftEye, leftEyeBall, ground, constraint]);

Engine.run(engine);
Render.run(render);