box2djsの作法 その11

# 概要
box2djsの作法、調べてみた。
エレベーター、やってみた。

# 写真


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/a53a40da-ff23-6329-8c6a-72d4db1589b1.png)

# サンプルコード

```

var b2Vec2 = Box2D.Common.Math.b2Vec2,
    b2BodyDef = Box2D.Dynamics.b2BodyDef,
    b2Body = Box2D.Dynamics.b2Body,
    b2FixtureDef = Box2D.Dynamics.b2FixtureDef,
    b2World = Box2D.Dynamics.b2World,
    b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
    b2CircleShape = Box2D.Collision.Shapes.b2CircleShape,
    b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef,
    b2DistanceJointDef = Box2D.Dynamics.Joints.b2DistanceJointDef,
    b2RopeJointDef = Box2D.Dynamics.Joints.b2RopeJointDef,
    b2MouseJointDef =  Box2D.Dynamics.Joints.b2MouseJointDef,
    b2DebugDraw = Box2D.Dynamics.b2DebugDraw,
    b2Fixture = Box2D.Dynamics.b2Fixture,
    b2AABB = Box2D.Collision.b2AABB,
    b2MassData = Box2D.Collision.Shapes.b2MassData,
    b2Color = Box2D.Common.b2Color;

var canvas = document.getElementById("c");
canvas.width = 456;
canvas.height = 456;
var world = new b2World(new b2Vec2(0, 10), true);
var TWO_PI = 6.283185307179586;
var elevator = [];
var i;
function createrect(angle, x, y, width, height) {
    var box1Def = new b2BodyDef;
    box1Def.type = b2Body.b2_staticBody;
    box1Def.position.Set(x / 10, y / 10);
    var fixDef1 = new b2FixtureDef;
    fixDef1.shape = new b2PolygonShape;
    fixDef1.shape.SetAsBox(width / 10, height / 10);
    var box1 = world.CreateBody(box1Def);
    box1.CreateFixture(fixDef1);
    box1.SetAngle(angle * 3.14 / 180);
    return box1;
}
function createcircle(x, y, r) {
    var box1Def = new b2BodyDef;
    box1Def.type = b2Body.b2_dynamicBody;
    box1Def.position.Set(x / 10, y / 10);
    var fixDef1 = new b2FixtureDef;
    fixDef1.shape = new b2CircleShape(r / 10);
    fixDef1.density = 0.01;
	fixDef1.friction = 1.5;
	fixDef1.restitution = 0.2;
    var box1 = world.CreateBody(box1Def);
    box1.CreateFixture(fixDef1);
    return box1;
}
function createele(angle, x, y, width, height) {
    var box1Def = new b2BodyDef;
    box1Def.type = b2Body.b2_dynamicBody;
    box1Def.position.Set(x / 10, y / 10);
    var fixDef1 = new b2FixtureDef;
    fixDef1.shape = new b2PolygonShape;
    fixDef1.shape.SetAsBox(width / 10, height / 10);
    var box1 = world.CreateBody(box1Def);
    box1.CreateFixture(fixDef1);
    box1.SetAngle(angle * 3.14 / 180);
    return box1;
}
createrect(0, 485, 0, 10, 550);
createrect(10, 50, 420, 320, 10);
//createrect(-20, 260, 200, 80, 10);
for (i = 0; i < 7; i++)
{
	elevator.push(createele(0, 420, i * 80 + 120, 50, 10));
}
for (i = 0; i < 7; i++)
{
	createcircle(40 + i, 110, 12);
}
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(document.getElementById("c").getContext("2d"));
debugDraw.SetDrawScale(7);  
debugDraw.SetFillAlpha(0.9); 
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
window.setInterval(loop, 1000 / 60);
function loop() {
    var i;
    for (i = 0; i < 7; i++)
	{
		var body = elevator[i];
        var p = body.GetPosition();
	    body.SetLinearVelocity(new b2Vec2(0, -1));        
		if (p.y <  50 / 10)
        {
            body.SetAngle(-10 * 3.14 / 180);
        }
        if (p.y < -5 / 10)
		{
			body.SetAngle(0 * 3.14 / 180);
			body.SetPosition(new b2Vec2(p.x, 550 / 10));
		}        
	}
    world.Step(1 / 60, 10, 10);
    world.DrawDebugData();
    world.ClearForces();
};





```

# 成果物

https://embed.plnkr.co/plunk/jDyp2JpW6Yu0tYlC


以上。