如何在 Matter JS 中定义重物

问题描述 投票:0回答:1

为了更好地解释我需要什么,请考虑一个水箱和一罐苏打水。 不管那罐汽水的速度有多快,坦克一旦被击中就不会移动。 但当对其施加力时,坦克应该能够移动。

已经在这里提出了一个问题,其中包含更多详细信息: https://github.com/liabru/matter-js/issues/841

我还在这里做了一个我需要的例子。

var Engine = Matter.Engine,
  Render = Matter.Render,
  World = Matter.World,
  Bodies = Matter.Bodies,
  Body = Matter.Body;

var engine = Engine.create();
engine.positionIterations = 10;
engine.velocityIterations = 8;

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

engine.world.gravity.y = 0;

var topWall = Bodies.rectangle(400, 50, 720, 20, { isStatic: true });
var leftWall = Bodies.rectangle(50, 210, 20, 300, { isStatic: true });
var rightWall = Bodies.rectangle(750, 210, 20, 300, { isStatic: true });
var bottomWall = Bodies.rectangle(400, 350, 720, 20, { isStatic: true });

var ball = Bodies.circle(100, 200, 5, { 
  friction: 0.05,
  frictionAir: 0.05,
  density: 0.001,
  restitution: 0.2
});

var bigBox = Matter.Bodies.rectangle(500, 200, 120, 120, {
  inertia: Infinity,
  frictionAir: 1,
  friction: 1,
  density: 0.1
});

World.add(engine.world, [topWall, leftWall, rightWall, bottomWall, ball, bigBox]);
Engine.run(engine);
Render.run(render);

// CMD + SHIFT + 7 to reload

$('.shoot').on('click', function () {
  var speed = 0.02;
  var angle = 0;
  let vector = Matter.Vector.create(Math.cos(angle) * speed, Math.sin(angle) * speed);
  Body.applyForce(ball, ball.position, vector);
});

https://codepen.io/dmerlea/pen/BaoQJYK

javascript matter.js
1个回答
0
投票

https://brm.io/matter-js/docs/classes/Body.htm

Body.mass
Body.density
Body.friction
Body.frictionAir - when friction is slow, object look like heavy
Body.frictionStatic 

© www.soinside.com 2019 - 2024. All rights reserved.