scenes.js revision f2477e01787aa58f445919b809d89e252beef54f
1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5function randomCubeScene(numObjects, zwidth) {
6  var worldDescription = {};
7  worldDescription.shapes = [];
8  worldDescription.shapes.push({
9    name: 'box',
10    type: 'cube',
11    wx: 1,
12    wy: 1,
13    wz: 1
14  });
15  worldDescription.bodies = [];
16  for ( var i = 0; i < numObjects; i ++ ) {
17    var body = {};
18    body.shape = 'box';
19    body.position = {};
20    body.position.x = Math.random() * 30 - 15;
21    body.position.y = Math.random() * 50 + 10;
22    body.position.z = Math.random() * zwidth - (zwidth/2);
23    body.rotation = {};
24    body.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
25    body.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
26    body.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
27    body.mass = 1.0;
28    body.friction = 0.8;
29    worldDescription.bodies.push(body);
30  }
31  return worldDescription;
32}
33
34function randomCylinderScene(numObjects, zwidth) {
35  var worldDescription = {};
36  worldDescription.shapes = [];
37  worldDescription.shapes.push({
38    name: 'box',
39    type: 'cylinder',
40    height: 1.0,
41    radius: 0.5
42  });
43  worldDescription.bodies = [];
44  for ( var i = 0; i < numObjects; i ++ ) {
45    var body = {};
46    body.shape = 'box';
47    body.position = {};
48    body.position.x = Math.random() * 30 - 15;
49    body.position.y = Math.random() * 50 + 10;
50    body.position.z = Math.random() * zwidth - (zwidth/2);
51    body.rotation = {};
52    body.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
53    body.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
54    body.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
55    body.mass = 1.0;
56    body.friction = 0.2;
57    worldDescription.bodies.push(body);
58  }
59  return worldDescription;
60}
61
62function jengaScene(height) {
63  var worldDescription = {};
64  worldDescription.shapes = [];
65  worldDescription.shapes.push({
66    name: 'boxX',
67    type: 'cube',
68    wx: 5,
69    wy: 1,
70    wz: 1
71  });
72  worldDescription.shapes.push({
73    name: 'boxZ',
74    type: 'cube',
75    wx: 1,
76    wy: 1,
77    wz: 5
78  });
79  worldDescription.bodies = [];
80  var baseHeight = 0.55;
81  for (var i = 0; i < height; i++) {
82    var y = i * 1.0 + baseHeight;
83    if (i % 2 == 0) {
84      for (var j = 0; j < 5; j++) {
85        var z = j * 1.0;
86        var x = 2.5;
87
88        var body = {};
89        body.shape = 'boxX';
90        body.position = {};
91        body.position.x = x;
92        body.position.y = y;
93        body.position.z = z;
94        body.rotation = {};
95        body.rotation.x = 0.0;
96        body.rotation.y = 0.0;
97        body.rotation.z = 0.0;
98        body.mass = 1.0;
99        body.friction = 0.5;
100        worldDescription.bodies.push(body);
101      }
102    } else {
103      for (var j = 0; j < 5; j++) {
104        var z = 2.5;
105        var x = j * 1.0;
106
107        var body = {};
108        body.shape = 'boxZ';
109        body.position = {};
110        body.position.x = x;
111        body.position.y = y;
112        body.position.z = z;
113        body.rotation = {};
114        body.rotation.x = 0.0;
115        body.rotation.y = 0.0;
116        body.rotation.z = 0.0;
117        body.mass = 1.0;
118        body.friction = 0.5;
119        worldDescription.bodies.push(body);
120      }
121    }
122  }
123  return worldDescription;
124}
125
126function randomShapeScene(numObjects) {
127  var worldDescription = {};
128  worldDescription.shapes = [];
129  worldDescription.shapes.push({
130    name: 'stick',
131    type: 'cube',
132    wx: 1,
133    wy: 1,
134    wz: 5
135  });
136  worldDescription.shapes.push({
137    name: 'tube',
138    type: 'cylinder',
139    radius: 1.0,
140    height: 2.0
141  });
142  worldDescription.shapes.push({
143    name: 'sphere',
144    type: 'sphere',
145    radius: 3.0
146  });
147  worldDescription.shapes.push({
148    name: 'tri',
149    type: 'convex',
150    points: [
151    [0.0, 0.0, 0.0],
152    [0.0, 1.0, 0.0],
153    [0.0, 0.0, 1.0],
154    [0.0, 0.0, 1.0],
155    [2.0, 5.0, 1.0],
156    [1.0, 1.0, 1.0]
157    ]
158  });
159
160  var numShapes = 4;
161  worldDescription.bodies = [];
162  for (var i = 0; i < numObjects; i++) {
163    var body = {};
164    if (i % numShapes == 0) {
165      body.shape = 'stick';
166    } else if (i % numShapes == 1) {
167      body.shape = 'tube';
168    } else if (i % numShapes == 2) {
169      body.shape = 'sphere';
170    } else if (i % numShapes == 3) {
171      body.shape = 'tri';
172    }
173    body.position = {};
174    body.position.x = Math.random() * 30 - 15;
175    body.position.y = Math.random() * 50 + 1;
176    body.position.z = Math.random() * 10 - 5;
177    body.rotation = {};
178    body.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
179    body.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
180    body.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
181    body.mass = 1.0;
182    body.friction = 0.4;
183    worldDescription.bodies.push(body);
184  }
185  return worldDescription;
186}
187
188function loadJenga10() {
189  loadWorld(jengaScene(10));
190}
191
192function loadJenga20() {
193  loadWorld(jengaScene(20));
194}
195
196function loadRandomShapes() {
197  loadWorld(randomShapeScene(100));
198}
199
200function load250RandomCubes() {
201  loadWorld(randomCubeScene(250, 10));
202}
203
204function load500RandomCylinders() {
205  loadWorld(randomCylinderScene(500, 15));
206}
207
208function load1000RandomCubes() {
209  loadWorld(randomCubeScene(1000, 20));
210}
211
212function load2000RandomCubes() {
213  loadWorld(randomCubeScene(2000, 20));
214}
215
216function loadTextScene(evt) {
217  var txt = evt.target.result;
218  if (txt == undefined) {
219    alert('Could not load file.');
220    return;
221  }
222  var sceneDescription;
223
224  try {
225    sceneDescription = JSON.parse(txt);
226  } catch(e) {
227    alert(e);
228    return;
229  }
230
231  loadWorld(sceneDescription);
232}
233