Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/box2d-js/sample03-07.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 63 - (show annotations) (download) (as text)
Mon Feb 24 04:55:29 2014 UTC (10 years, 1 month ago) by syun77
File MIME type: application/x-javascript
File size: 2429 byte(s)
直動ジョイントによるモーターのサンプルを追加
1 // ■ワールドを初期化
2 var worldAABB = new b2AABB();
3 // ▼矩形境界(AABB)の指定
4 {
5 // 矩形の左上座標
6 worldAABB.minVertex.Set(-1000, -1000);
7 // 矩形の右下座標
8 worldAABB.maxVertex.Set(1000, 1000);
9 }
10 // ▼重力の設定
11 var gravity = new b2Vec2(0, 300);
12
13 // ▼動きが止まった物体の動きの計算を省略する
14 var isSleep = true;
15
16 // ワールドを生成
17 var world = new b2World(worldAABB, gravity, isSleep);
18
19 // --------------------------------------
20 // ■アンカーとなる円
21 var anchorSd = new b2CircleDef();
22 // 半径
23 anchorSd.radius = 10;
24 // 剛体定義
25 var anchorBd = new b2BodyDef();
26 // 図形を登録
27 anchorBd.AddShape(anchorSd);
28 // 座標を設定
29 anchorBd.position.Set(50, 50);
30 // 剛体を生成
31 var anchor = world.CreateBody(anchorBd);
32
33 // --------------------------------------
34 // ■矩形
35 var boxSd = new b2BoxDef();
36 // サイズ
37 boxSd.extents.Set(30, 30);
38 // 質量
39 boxSd.density = 1.0;
40 // 剛体生成
41 var boxBd = new b2BodyDef();
42 // 図形を登録
43 boxBd.AddShape(boxSd);
44 // 配置
45 boxBd.position.Set(250, 250);
46 // 剛体生成
47 var box = world.CreateBody(boxBd);
48
49 // --------------------------------------
50 // ■直動ジョイントを作成
51 var jd = new b2PrismaticJointDef();
52 // 剛体を設定
53 jd.body1 = anchor;
54 jd.body2 = box;
55 // アンカーポイントを設定
56 jd.anchorPoint = anchor.GetCenterPosition();
57 // 可動軸を設定
58 jd.axis.Set(1, 1);
59 // モータを設定
60 jd.enableMotor = true; // モーターを有効にする
61 jd.motorForce = 10000000; // 回転する力の大きさ
62 jd.motorSpeed = -100; // 回転する速さ
63 // ジョイントの可動範囲を設定
64 {
65 // ある程度登ると止まる
66 jd.enableLimit = true;
67 jd.lowerTranslation = -100;
68 }
69 var joint = world.CreateJoint(jd);
70
71 // --------------------------------------
72 // ■地面用に固定された四角をワールドに追加
73 {
74 // ▼矩形図形を作成
75 var groundSd = new b2BoxDef();
76 // 大きさ (半径)
77 groundSd.extents.Set(1000, 50);
78 // 反発係数
79 groundSd.restitution = 0.2;
80 // 重さを「0」にして動かないようにする
81 groundSd.density = 0.0;
82
83 // ▼剛体を作成
84 var groundBd = new b2BodyDef();
85 // 図形を剛体に登録
86 groundBd.AddShape(groundSd);
87 // 配置座標を設定 (左上座標)
88 groundBd.position.Set(250, 500);
89 // ・ワールドに追加
90 var ground = world.CreateBody(groundBd);
91 }
92

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26