LoginSignup
2
2

More than 5 years have passed since last update.

Box2dWebで滑車を作ってみる

Last updated at Posted at 2014-10-05

今回はbox2dwebで滑車ジョイントをやります。

今回のスクリーンショットです。
2つの衝突オブジェクトを滑車にぶら下げたような動きを実現します

スクリーンショット 2014-10-05 22.04.17.png

今回の全ソースコードです。

test.html
<canvas id="canvas" width="600px" height="420px" style="background-color:#333333;"></canvas>

<script type="text/javascript" src="Box2dWeb-2.1.a.3.min.js"></script>
<script type="text/javascript">

    // Box2Dオブジェクトを取得
    var b2Vec2 = Box2D.Common.Math.b2Vec2 // 2Dベクトル
     ,  b2BodyDef = Box2D.Dynamics.b2BodyDef // Body定義
     ,  b2Body = Box2D.Dynamics.b2Body // Body
     ,  b2FixtureDef = Box2D.Dynamics.b2FixtureDef // Fixture定義
     ,  b2Fixture = Box2D.Dynamics.b2Fixture // Fixture
     ,  b2World = Box2D.Dynamics.b2World // 物理世界
     ,  b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape // 衝突オブジェクトの形状(ポリゴン)
     ,  b2CircleShape = Box2D.Collision.Shapes.b2CircleShape  // 衝突オブジェクトの形状(円)
     ,  b2PulleyJointDef = Box2D.Dynamics.Joints.b2PulleyJointDef // 滑車ジョイント
     ,  b2DebugDraw = Box2D.Dynamics.b2DebugDraw // デバッグ描画


    // 世界を作る
    var world = new b2World(new b2Vec2(0,10), true);


    ///////////////////////////////////

    //box1      
    var bodyDef = new b2BodyDef;
    bodyDef.type = b2Body.b2_dynamicBody;
    bodyDef.position.Set(6,7);

    var fixDef = new b2FixtureDef;
    fixDef.density = 15.0;
    fixDef.friction = 0.5;
    fixDef.restitution = .5;

    fixDef.shape = new b2PolygonShape;
    fixDef.shape.SetAsBox(1,1);

    var box1 = world.CreateBody(bodyDef);
    box1.CreateFixture(fixDef);

    //box2      
    bodyDef.position.Set(16,7); 
    fixDef.density = 40;      
    var box2 = world.CreateBody(bodyDef);
    box2.CreateFixture(fixDef);

    // 地面
    bodyDef.type = b2Body.b2_staticBody;
    bodyDef.position.Set(10, 14);
    fixDef.shape = new b2PolygonShape;
    fixDef.shape.SetAsBox(10,1);

    var ground = world.CreateBody(bodyDef);
    ground.CreateFixture(fixDef);

    // 滑車ジョイント
    var myjoint = new b2PulleyJointDef();
    var worldAnchorOnBody1 = new b2Vec2(box1.GetPosition().x, box1.GetPosition().y);
    var worldAnchorOnBody2 = new b2Vec2(box2.GetPosition().x, box2.GetPosition().y);
    var worldAnchorGround1 = new b2Vec2(8,3);
    var worldAnchorGround2 = new b2Vec2(14,4);
    var ratio = 1;
    myjoint.Initialize(box1, box2, worldAnchorGround1, worldAnchorGround2, worldAnchorOnBody1, worldAnchorOnBody2, ratio);
    var pulleyjoint = world.CreateJoint(myjoint);

    ///////////////////////////////////


    // デバッグ描画の設定      
    var debugDraw = new b2DebugDraw();
    debugDraw.SetSprite ( document.getElementById ("canvas").getContext ("2d"));
    debugDraw.SetDrawScale(30);     //描画スケール
    debugDraw.SetFillAlpha(0.3);    //半透明値
    debugDraw.SetLineThickness(1.0);//線の太さ
    debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);// 何をデバッグ描画するか
    world.SetDebugDraw(debugDraw);

    window.setInterval(update,1000/60);

    function update() {
        world.Step(1 / 60, 10, 10); // 物理世界を更新する
        world.DrawDebugData(); // デバック描画
        world.ClearForces(); // 物理世界上の力をリセットする
    };

</script>

滑車ジョイント用のb2PulleyJointDefオブジェクトを追加しておきます。

 var b2PulleyJointDef = Box2D.Dynamics.Joints.b2PulleyJointDef // 滑車ジョイント

滑車ジョイントは次の箇所で行っています。
worldAnchorGround1とworldAnchorGround2は
2つの滑車の位置となります。

    // 滑車ジョイント
    var myjoint = new b2PulleyJointDef();
    var worldAnchorOnBody1 = new b2Vec2(box1.GetPosition().x, box1.GetPosition().y);
    var worldAnchorOnBody2 = new b2Vec2(box2.GetPosition().x, box2.GetPosition().y);
    var worldAnchorGround1 = new b2Vec2(8,3);
    var worldAnchorGround2 = new b2Vec2(14,4);
    var ratio = 1;
    myjoint.Initialize(box1, box2, worldAnchorGround1, worldAnchorGround2, worldAnchorOnBody1, worldAnchorOnBody2, ratio);
    var pulleyjoint = world.CreateJoint(myjoint);

ちなみに
worldAnchorGround1と
worldAnchorGround2を
同じ位置にすれば滑車を1つにできます

アクションゲームの空中床とかに使えそうです

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2