首页 > 解决方案 > 空心圆盘的角速度 MatterJS

问题描述

我想在 MatterJS 中围绕它的中心旋转一个空心的圆盘状圆圈。我使用下面的代码创建了这样一个空心体,基本上组合了无数个矩形。它看起来像这样:

在此处输入图像描述

function containerPolygon(
    x, y, sides, radius, color
) {
    const width = 2;
    const extraLength = 1.15;
    const initialRotation = 0;
    const theta = (2 * Math.PI) / sides;
    const sideLength = ((2 * radius * theta) / 2) * extraLength;
    const parts = [];

    for (let i = 0; i < sides; i++) {
        // We'll build thin sides and then translate + rotate them appropriately.
        const body = Bodies.rectangle(0, 0, sideLength, width, {
            render: {
                fillStyle: color,
            },

        });
        Body.rotate(body, i * theta);
        Body.translate(body, {
            x: radius * Math.sin(i * theta),
            y: -radius * Math.cos(i * theta),
        });
        parts.push(body);
    }

    const ret = Body.create({
        isStatic: true,
        // frictionAir: 0,
        friction: 1,
        // // frictionStatic: 0,
        // inertia: Infinity, // setting inertia to infinty will prevent rotation upon collision
        rotationSpeed: -2.5,
        restitution: ball_restitution,
    });

    Body.setParts(ret, parts);
    Body.translate(ret, { x: x, y: y });
    return ret;
}

我使用下面的代码调用该函数:

let rotating_circle = containerPolygon(
    window.innerWidth / 2,
    window.innerHeight / 2,
    50,
    radius + object_radius,
    outter_circle_color
);

我想设置它的角速度,使其围绕其中心旋转,但我尝试使用下面的这种方法,它甚至没有旋转。

Body.setAngularVelocity(rotating_cicle, Math.PI/6);

标签: javascriptphysics-enginematter.js

解决方案


推荐阅读