我是物理引擎的新手但是为了开始一个我想象的项目,我需要让六角形连续旋转到页面中心的固定位置。
我想我从根本上误解了物理引擎是如何工作的,但当我打电话给Matter.Body.rotate(hexagon,1)时;当由提供的参数(1)渲染时,它只是立即旋转六边形,并且不会再旋转六角形。如何让它连续旋转?
这是我的代码:
请注意,setStatic已设置为六边形不会脱离框架。
// module aliases
var Engine = Matter.Engine,
Render = Matter.Render,
World = Matter.World,
Bodies = Matter.Bodies;
Composites = Matter.Composites;
// create an engine
var engine = Engine.create();
// create a renderer
var render = Render.create({
element: document.body,
engine: engine
});
var hexagon = Bodies.polygon(375, 300, 6, 200, {inertia: Infinity}); // setting inertia to inifinty will prevent rotation upon collision
Matter.Body.setStatic(hexagon, true);
Matter.Body.rotate(hexagon, 1);
console.log(hexagon);
// Matter.Body.rotate(hexagon, 1)
// add all of the bodies to the world
World.add(engine.world, [hexagon]);
// run the engine
Engine.run(engine);
// run the renderer
Render.run(render);

<!DOCTYPE html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.js"></script>
<!-- <script src="matter.js" type="text/javascript"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.12.0/matter.js"></script>
<!-- <script async src="hexagondwana.js"></script> -->
<script async src="hex_no_p5.js"></script>
</head>
<body>
</body>
</html>
&#13;
答案 0 :(得分:0)
您应该包含一个值,该值表示六边形的当前旋转速度和六边形旋转的速度。然后,使用循环更新以增加六角形的旋转,并使用Matter.Body.setAngle
设置旋转。看起来像这样:
var hexagon = Bodies.polygon(375, 300, 6, 200, {
isStatic: true,
inertia: Infinity,// setting inertia to infinty will prevent rotation upon collision
currentRotation: 0,
rotationSpeed: 1
});
World.add(world, [hexagon]);
function updateRotation() {
hexagon.currentRotation += 1*hexagon.rotationSpeed;
Matter.Body.setAngle(hexagon, hexagon.currentRotation);
requestAnimationFrame(updateRotation);
}
window.requestAnimationFrame(updateRotation);
完整的代码在这里:
// module aliases
var Engine = Matter.Engine,
Render = Matter.Render,
World = Matter.World,
Body = Matter.Body,
Bodies = Matter.Bodies;
Composites = Matter.Composites;
// create an engine
var engine = Engine.create();
// create a renderer
var render = Render.create({
element: document.body,
engine: engine
});
var hexagon = Bodies.polygon(375, 300, 6, 200, {
isStatic: true,
inertia: Infinity,// setting inertia to infinty will prevent rotation upon collision
currentRotation: 0,
rotationSpeed: 1
});
// add all of the bodies to the world
World.add(engine.world, [hexagon]);
// run the engine
Engine.run(engine);
function updateRotation() {
hexagon.currentRotation += 1*hexagon.rotationSpeed;
Body.setAngle(hexagon, hexagon.currentRotation);
requestAnimationFrame(updateRotation);
}
window.requestAnimationFrame(updateRotation);
// run the renderer
Render.run(render);
<!DOCTYPE html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/p5.js"></script>
<!-- <script src="matter.js" type="text/javascript"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.12.0/matter.js"></script>
<!-- <script async src="hexagondwana.js"></script> -->
<script async src="hex_no_p5.js"></script>
</head>
<body>
</body>
</html>