Prevent force-dragging bodies through other bodies with MatterJS

后端 未结 4 1676
醉梦人生
醉梦人生 2021-02-02 14:27

I\'m using MatterJs for a physics based game and have not found a solution for the problem of preventing bodies being force-dragged by the mouse through other bodies. If you dra

4条回答
  •  借酒劲吻你
    2021-02-02 15:17

    To control collision when dragged you need to utilize collision filter and events.

    Create bodies with default collision filter mask 0x0001. Add catch startdrag and enddrag events and set different body collision filter category to temporarily avoid collisions.

    Matter.Events.on(mouseConstraint, 'startdrag', function(event) {
        event.body.collisionFilter.category = 0x0008; // move body to new category to avoid collision
    });
    Matter.Events.on(mouseConstraint, 'enddrag', function(event) {
         event.body.collisionFilter.category = 0x0001; // return body to default category to activate collision
    });
    

    window.addEventListener('load', function () {
    
      //Fetch our canvas
      var canvas = document.getElementById('world');
    
      //Setup Matter JS
      var engine = Matter.Engine.create();
      var world = engine.world;
      var render = Matter.Render.create({
                                          canvas: canvas,
                                          engine: engine,
                                          options: {
                                            width: 800,
                                            height: 800,
                                            background: 'transparent',
                                            wireframes: false,
                                            showAngleIndicator: false
                                          }
                                        });
    
      //Add a ball
      const size = 50;
      const stack = Matter.Composites.stack(350, 470 - 6 * size, 1, 6, 0, 0, (x, y) => {
        return Matter.Bodies.rectangle(x, y, size * 2, size, {
          collisionFilter: {
                mask: 0x0001,
          },
          slop: 0.5,
          friction: 1,
          frictionStatic: Infinity,
        });
      });
    
      Matter.World.add(engine.world, stack);
    
      //Add a floor
      var floor = Matter.Bodies.rectangle(250, 520, 500, 40, {
        isStatic: true, //An immovable object
        render: {
          visible: false
        }
      });
      Matter.World.add(world, floor);
    
      //Make interactive
      var mouseConstraint = Matter.MouseConstraint.create(engine, { //Create Constraint
        element: canvas,
    
        constraint: {
          render: {
            visible: false
          },
          stiffness: 0.8
        }
      });
      Matter.World.add(world, mouseConstraint);
    
      // add events to listen drag
      Matter.Events.on(mouseConstraint, 'startdrag', function (event) {
        event.body.collisionFilter.category = 0x0008; // move body to new category to avoid collision
      });
      Matter.Events.on(mouseConstraint, 'enddrag', function (event) {
        event.body.collisionFilter.category = 0x0001; // return body to default category to activate collision
      });
    
      //Start the engine
      Matter.Engine.run(engine);
      Matter.Render.run(render);
    
    });
    
    

提交回复
热议问题