'Rolling Non Circular Objects Matter.js
I am pretty new to Matter.js so forgive me if the answer to this is obvious.
I am trying to rotate a body on top of another (static) body and have it roll along that surface.
I need something similar to the car demo but I need there to be zero acceleration to the roll. i.e. every iteration of the loop I should be able to set a rotation amount and have the body rotate that much. I also need this to able to work with non-eliptical bodies.
I have some code that rotates the body as I would like but I am struggling to make the body roll along the surface based on its rotation.
Any help would be much appreciated. Please see my current code below.
// module aliases
var Engine = Matter.Engine,
Render = Matter.Render,
World = Matter.World,
Bodies = Matter.Bodies,
Body = Matter.Body;
// create an engine
var engine = Engine.create();
// create a renderer
var render = Render.create({
element: document.body,
engine: engine
});
var boxA = Bodies.rectangle(400, 200, 80, 80);
var ground = Bodies.rectangle(400, 610, 810, 60, { isStatic: true });
// add all of the bodies to the world
World.add(engine.world, [boxA, ground]);
// run the renderer
Render.run(render);
engine.world.gravity = {x: 0, y:0, scale: 0}; //disable gravity
window.setInterval(function () {
Body.rotate(boxA, 0.01);
Body.translate(boxA, { x: 0, y: 10 });
Engine.update(engine, 1000 / 60);
}, 1000 / 60)
<script src="https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.17.1/matter.min.js"></script>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
