0

我正在尝试模拟一个球在台球桌周围弹跳并且遇到了一个问题。(视图从上方俯视桌子)。

我总是希望球根据反射定律从边界反弹。Angle of incidence = angle of reflection

在此处输入图像描述

在右侧边界的模拟结束时,球似乎沿着它滚动。为什么它不遵循反射定律?

示例:https ://jsfiddle.net/5sjx7oyw/

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

// create an engine
var engine = Engine.create();

var canvas = document.getElementById('canvas');

engine.world.gravity.y = 0; // gravity not needed in this app

// create a renderer
var render = Render.create({
    element: document.body,
    canvas: canvas,
    engine: engine,
    options: {wireframes: true}
});

var ball_options = {
    density: 0.04,
  restitution: 1,// 0 = no bouncing, 1 = 100% of kinetic energy bounce back
  friction: 0,
  frictionAir: 0.05,
  inertia: Infinity
};

var boundary_options = {
  isStatic: true
};

var ball = Bodies.circle(100, 150, 11, ball_options);

var boundary_left = Bodies.rectangle(10, 160, 100, 200, boundary_options);
var boundary_right = Bodies.rectangle(300, 160, 100, 200, boundary_options);
var boundary_top = Bodies.rectangle(150, 10, 200, 100, boundary_options);
var boundary_bottom = Bodies.rectangle(150, 300, 200, 100, boundary_options);

// add all of the bodies to the world
World.add(engine.world, [
    boundary_left,
  boundary_right,
  boundary_top,
  boundary_bottom,
  ball
  ]);

render.options.height = 300;
canvas.height = 300;
Engine.run(engine);
Render.run(render);

Body.applyForce(ball, { x: 0, y: 0 }, { x: 0.2, y: 0.5 });
4

2 回答 2

4

我在图书馆创建者的帮助下解决了这个问题。

https://jsfiddle.net/8exLLLv6/55/

将以下内容添加到我的代码中解决了该问题:

Matter.Resolver._restingThresh = 0.1;

有关更多信息,请参阅gibhub页面。

于 2017-07-24T10:52:49.783 回答
0

计算机中的数字不是实数;他们是interinter。“浮点数”的意思是“像浮点数一样的数字”。这就是为什么 x 轴上的速度不会连续减少到 0 的原因。在代码中,x 轴的初始速度小于 y 轴的 1。所以“离散效应”出现在 x 轴上。当恢复接近 1 时,您会感觉像真正的球。谢谢。

于 2017-07-22T08:21:54.797 回答