为什么物体不固定在棱柱形接头上

时间:2013-03-27 05:20:53

标签: javascript box2d box2dweb

我正在创建一堆随机对象并将它们约束到具有棱柱关节的固定体(圆)。目标是让它们向固定的圆圈滑动。

问题是 - 物体从关节的边缘掉落,因为它们似乎没有锚定。

我做错了什么?

js fiddle:http://jsfiddle.net/Jk5VD/7/

js :(直接到Physics.prototype.connectToCenterWithJoint获取联合代码)

var Physics = (function () {
    function Physics() {
        this.bodies = [];
        this.testObjectCount = 30;
        this.w = new Box2D.Dynamics.b2World(new Box2D.Common.Math.b2Vec2(0, 10), true);
        var fixture = new Box2D.Dynamics.b2FixtureDef();
        this.bodyDef = new Box2D.Dynamics.b2BodyDef();
        this.bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
        this.bodyDef.position.x = 9;
        this.bodyDef.position.y = 13;
        fixture.shape = Box2D.Collision.Shapes.b2PolygonShape.AsBox(10, .5);
        this.w.CreateBody(this.bodyDef).CreateFixture(fixture);
        this.bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
        this.bodyDef = new Box2D.Dynamics.b2BodyDef();
        this.bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
        this.bodyDef.position.x = 10;
        this.bodyDef.position.y = 6;
        fixture.shape = new Box2D.Collision.Shapes.b2CircleShape(1);
        var circle = this.w.CreateBody(this.bodyDef);
        circle.CreateFixture(fixture);
        this.c = circle;
        this.bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
        this.populate();
        this.testJoints();
    }
    Physics.prototype.populate = function () {
        var fixture = new Box2D.Dynamics.b2FixtureDef();
        for(var i = 0; i < this.testObjectCount; ++i) {
            var rnd = Math.random();
            if(rnd > 0.7) {
                fixture.shape = Box2D.Collision.Shapes.b2PolygonShape.AsBox(Math.random() + 0.1, Math.random() + 0.1);
            } else if(rnd < .7 && rnd > .3) {
                fixture.shape = new Box2D.Collision.Shapes.b2CircleShape(Math.random() + 0.1);
            } else {
                var vertices = [
                    new Box2D.Common.Math.b2Vec2(0.0, 0.0), 
                    new Box2D.Common.Math.b2Vec2(1.0, 0.0), 
                    new Box2D.Common.Math.b2Vec2(0.0, 1.0)
                ];
                fixture.shape = Box2D.Collision.Shapes.b2PolygonShape.AsArray(vertices);
            }
            this.bodyDef.position.x = Math.random() * 20;
            this.bodyDef.position.y = Math.random() * 10;
            var body = this.w.CreateBody(this.bodyDef);
            body.CreateFixture(fixture);
            this.bodies.push(body);
        }
    };
    Physics.prototype.bindToCanvas = function (canvasId) {
        var _this = this;
        var debugDraw = new Box2D.Dynamics.b2DebugDraw();
        debugDraw.SetSprite((document.getElementById(canvasId)).getContext("2d"));
        debugDraw.SetDrawScale(30.0);
        debugDraw.SetFillAlpha(0.3);
        debugDraw.SetLineThickness(1.0);
        debugDraw.SetFlags(Box2D.Dynamics.b2DebugDraw.e_shapeBit | Box2D.Dynamics.b2DebugDraw.e_jointBit);
        this.w.SetDebugDraw(debugDraw);
        window.setInterval(function () {
            _this.update();
        }, 1000 / 60);
    };
    Physics.prototype.connectToCenterWithJoint = function (b) {
        var jd = new Box2D.Dynamics.Joints.b2PrismaticJointDef();
        var axis = Box2D.Common.Math.b2Math.SubtractVV(b.GetWorldCenter(), this.c.GetWorldCenter());
        axis.Normalize();
        jd.Initialize(this.c, b, b.GetWorldCenter(), axis);
        jd.lowerTranslation = -1;
        jd.upperTranslation = 0;
        jd.enableLimit = true;
        this.w.CreateJoint(jd);
    };
    Physics.prototype.testJoints = function () {
        for(var i = 0; i < this.testObjectCount; ++i) {
            this.connectToCenterWithJoint(this.bodies[i]);
        }
    };
    Physics.prototype.applyTestFoce = function () {
        for(var i = 0; i < this.testObjectCount; ++i) {
            this.bodies[i].ApplyForce(new Box2D.Common.Math.b2Vec2(5000, 0), this.bodies[i].GetWorldCenter());
        }
    };
    Physics.prototype.update = function () {
        this.w.Step(1 / 60, 10, 10);
        this.w.DrawDebugData();
        this.w.ClearForces();
    };
    return Physics;
})();

1 个答案:

答案 0 :(得分:0)

问题是需要设置密度以使棱柱形接头工作。这将教我信任默认值。

修正:

fixture.density = .5;

在此工作:http://jsfiddle.net/Jk5VD/9/