PhysicsJS - 如何创建一个身体,围绕固定点旋转时撞击? [英] PhysicsJS - how to create a body which rotates about a fixed point when struck?

查看:190
本文介绍了PhysicsJS - 如何创建一个身体,围绕固定点旋转时撞击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验优秀的PhysicsJS库,特别是约束和碰撞。我想实现的是一个多边形,可以绕固定的旋转点旋转。作为一个现实世界的例子,想象一个宽的书架钉在墙上,只有一个长钉通过正中心,所以当它偏离中心从上面落下时,它旋转一半或一个革命。

I've been experimenting with the excellent PhysicsJS library, especially constraints and collisions. What I'm trying to achieve is a polygon which can spin about a fixed rotation point. As a real world example, imagine a wide shelf nailed to a wall with just one long nail through the very center, so that it spins round maybe half or one revolution when something off-center drops on it from above.

以下是一个示例: http://jsfiddle.net/2HRGW/ 41 /

和代码:

Physics(function (world) {
var renderer = Physics.renderer('canvas', {
    el: 'viewport',
    width: 500,
    height: 400
});
world.add(renderer);

var nail = Physics.body('circle', {
    x: 250,
    y: 200,
    radius: 5,
    mass: 1,
    fixed: true
});
world.add(nail);

var shelf = Physics.body('convex-polygon', {
    x: 250,
    y: 200,
    vertices: [{
        x: -100,
        y: -10
    }, {
        x: 100,
        y: -10
    }, {
        x: 100,
        y: 10
    }, {
        x: -100,
        y: 10
    }],
    mass: 100,
    restitution: 0.5
});
world.add(shelf);

var ball = Physics.body('circle', {
    x: 175,
    y: 50,
    radius: 20,
    mass: 10
});
world.add(ball);

world.add(Physics.integrator('verlet', {
    drag: 0.003
}));

var verletConstraints = Physics.behavior('verlet-constraints', {
    iterations: 2
});
verletConstraints.distanceConstraint(shelf, nail, 1);
world.add(verletConstraints);

world.add(Physics.behavior('constant-acceleration'));
world.add(Physics.behavior('body-collision-detection'));
world.add(Physics.behavior('body-impulse-response'));
world.add(Physics.behavior('sweep-prune'));
world.add(Physics.behavior('verlet-constraints'));

var bounds = Physics.aabb(0, 0, 500, 400);

world.add(Physics.behavior('edge-collision-detection', {
    aabb: bounds,
    restitution: 0.01
}));

Physics.util.ticker.subscribe(function (time, dt) {
    world.step(time);
});

world.render();

Physics.util.ticker.start();

world.subscribe('step', function () {
    world.render();
});
});

我为钉子定义了一个固定圆,并为搁板定义了一个非固定多边形,约束将多边形链接到圆。正如你可以看到,有2个问题。首先,搁架立即略微下降,直到其顶部边缘与指甲的顶部齐平,而不是保持围绕指甲均匀地定位。其次,当球落在架子上时,架子不断地疯狂旋转,尽管已经尝试了质量和各种恢复设置。

I define a fixed circle for the nail and a non-fixed polygon for the shelf and add a distance constraint linking the polygon to the circle. As you can see, there are 2 problems. Firstly, the shelf immediately drops down slightly until its top edge is flush with the top of the nail, rather than remaining evenly positioned around the nail. Secondly, when the ball drops onto the shelf, the shelf goes crazy spinning round endlessly, despite having mass and various restitution settings tried. Adjusting its position slightly, sometimes it can even detach completely and fly off.

我是在正确的轨道上使用这种方式的约束,还是有一个更简单的解决方案? / p>

Am I on the right track using constraints in this way, or is there a simpler solution?

推荐答案

正如其他用户提到的,这是PhysicsJS的当前限制。正在制定:

As other users have mentioned, this is a current limitation of PhysicsJS. It's being worked out:

  • scoping of collisions https://github.com/wellcaffeinated/PhysicsJS/issues/30
  • constraints https://github.com/wellcaffeinated/PhysicsJS/issues/5

时间,而不是修补库,为什么不创建自定义pin约束行为。对于将主体质心固定到目标位置的简单针约束行为,这是很容易的。这是你的例子的一个jsFiddle,在开头定义了一个自定义pin约束管理器。

In the mean time, instead of patching the library, why not create a custom pin constraint behavior. For a simple pin constraint behavior that pins the body centroid to a target position, it's quite easy. Here's a jsFiddle of your example with a custom pin constraint manager defined at the beginning.

http://jsfiddle.net/wellcaffeinated/2HRGW/50/

// create a behavior to handle pin constraints
Physics.behavior('pin-constraints', function( parent ){
    return {
        init: function( opts ){
            parent.init.call( this, opts );
            this.pins = [];
        },

        add: function( body, targetPos ){
            this.pins.push({
                body: body,
                target: Physics.vector( targetPos )
            });
        },

        behave: function( data ){

            var pins = this.pins
                ,pin
                ;

            for (var i = 0, l = pins.length; i < l; i++){
                pin = pins[ i ];
                // move body to correct position
                pin.body.state.pos.clone( pin.target );
            }
        }
    };
});

这篇关于PhysicsJS - 如何创建一个身体,围绕固定点旋转时撞击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆