gpt4 book ai didi

javascript - 为什么我的圆圈以一定 Angular 从直线表面弹起?

转载 作者:行者123 更新时间:2023-11-29 10:05:03 25 4
gpt4 key购买 nike

我的世界里有一个球和两个笔直的垂直表面。

当我对球施加力时,我希望它保持在一条直线上,但它似乎以一个 Angular 反弹。

fiddle :https://jsfiddle.net/zvjvvzeL/11/

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_0 = Bodies.circle(100, 150, 11, {
density: 0.04,
frictionAir: 0.06,
restitution: 0.8,
friction: 0.3
});

var cushion_left = Bodies.rectangle(34, 160, 100, 208, { isStatic: true });
var cushion_right = Bodies.rectangle(492, 160, 100, 208, { isStatic: true });

// add all of the bodies to the world
World.add(engine.world, [cushion_left, cushion_right, ball_0]);

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

Body.applyForce(ball_0, { x: 0, y: 0 }, { x: 0.5, y: 0 });

最佳答案

对 MatterJS 不太熟悉,但似乎球默认应用了 Angular 旋转。我认为这只会影响像您构建的封闭系统。

从长远来看,也许你会想要它,但现在你可以设置 intertia : Infinity

var ball_0 = Bodies.circle(100, 150, 11, {
density: 0.04,
frictionAir: 0.06,
restitution: 0.8,
friction: 0.3,
inertia : Infinity
});

但现在您还必须稍微用力才能让球接触到墙壁。我刚把它调到 .6

Body.applyForce(ball_0, { x: 0, y: 0 }, { x: .6, y: 0 });

关于javascript - 为什么我的圆圈以一定 Angular 从直线表面弹起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45170664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com