|
53
|
1 |
(function ($) { |
|
|
2 |
$.fn._vs.token = { |
|
|
3 |
|
|
|
4 |
// alias for d3 color scale D3 |
|
|
5 |
colorRange:function(){}, |
|
|
6 |
|
|
|
7 |
init:function(_this){ |
|
|
8 |
// Color scale import form d3 |
|
|
9 |
// todo shape management |
|
|
10 |
this.colorRange = _this.settings.chart.colorRange |
|
|
11 |
}, |
|
|
12 |
ID:function(_this){ |
|
|
13 |
_this.settings.data.tokenPast+=1 |
|
|
14 |
return _this.settings.data.tokenPast |
|
|
15 |
}, |
|
|
16 |
selectAll:function(_this,key,value){ |
|
|
17 |
// DRAFT VERSION writing select All ...... |
|
|
18 |
var result = [] |
|
|
19 |
var all = false |
|
|
20 |
result.flocculate = function(){ |
|
|
21 |
var r=[] |
|
|
22 |
result.forEach(function(i){ |
|
|
23 |
q = i.flocculate() |
|
|
24 |
r.push(q) |
|
|
25 |
}) |
|
|
26 |
return r |
|
|
27 |
} |
|
|
28 |
result.attr = function(key,value,param){ |
|
|
29 |
var r=[] |
|
|
30 |
result.forEach(function(i){ |
|
|
31 |
//console.log(key,value,param) |
|
|
32 |
q = i.attr(key,value,param) |
|
|
33 |
//console.log("q",q) |
|
|
34 |
r.push(q) |
|
|
35 |
}) |
|
|
36 |
return r |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
result.b2dObj = function(key,value,param){ |
|
|
40 |
var r=[] |
|
|
41 |
result.forEach(function(i){ |
|
|
42 |
//console.log(key,value,param) |
|
|
43 |
q = i.myobj |
|
|
44 |
//console.log("q",q) |
|
|
45 |
r.push(q) |
|
|
46 |
}) |
|
|
47 |
return r |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
if(typeof(value) == "undefined" && typeof(key) == "undefined"){ |
|
|
51 |
all =true |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
for (var i = _this.tokens.length - 1; i >= 0; i--) { |
|
|
55 |
if(_this.tokens[i].attr(key) == value || all==true){ |
|
|
56 |
result.push(_this.tokens[i]) |
|
|
57 |
} |
|
|
58 |
} |
|
|
59 |
return result; |
|
|
60 |
}, |
|
|
61 |
|
|
|
62 |
select:function(_this,key,value){ |
|
|
63 |
result = [] |
|
|
64 |
if(typeof(value) == "undefined" && typeof(key) == "undefined"){ |
|
|
65 |
return _this.tokens |
|
|
66 |
}else{ |
|
|
67 |
for (var i = _this.tokens.length - 1; i >= 0; i--) { |
|
|
68 |
if(_this.tokens[i].attr(key) == value){ |
|
|
69 |
result.push(_this.tokens[i]) |
|
|
70 |
break; |
|
|
71 |
} |
|
|
72 |
} |
|
|
73 |
} |
|
|
74 |
if(typeof(result[0])=="undefined"){ |
|
|
75 |
return false |
|
|
76 |
}else{ |
|
|
77 |
return result[0]; |
|
|
78 |
} |
|
|
79 |
}, |
|
|
80 |
|
|
|
81 |
|
|
|
82 |
addToken:function (_this,element){ |
|
|
83 |
//default token setting |
|
|
84 |
var defaultTokenSetting ={ |
|
|
85 |
x:50,y:50, // positions |
|
|
86 |
t:null, // time |
|
|
87 |
category:1,// data category |
|
|
88 |
state:0, // state |
|
|
89 |
/* |
|
|
90 |
0 = Not Enter in the stage, |
|
|
91 |
1 = suspension, |
|
|
92 |
2 = floculation, |
|
|
93 |
2 + n = _this.strata.list[n] |
|
|
94 |
*/ |
|
|
95 |
|
|
|
96 |
// Graphic Parameter |
|
|
97 |
size:10, fillStyle:'###', strokeStyle:'rgba(0,0,0,0)', lineWidth:0, texture:undefined, |
|
|
98 |
shape:{type:'round'}, // vertice, box, round, ?? svg path with json serialisation {} |
|
|
99 |
userdata:{}, |
|
|
100 |
|
|
|
101 |
// Interactions callbacks |
|
|
102 |
callback:{}, |
|
|
103 |
|
|
|
104 |
// Physical parameters |
|
|
105 |
phy:{ density:10,friction:0,restitution:0}, |
|
|
106 |
targets:[/*{x:null,y:null}*/], |
|
|
107 |
elbow:{/*x:null,y:null*/} |
|
|
108 |
} |
|
|
109 |
|
|
|
110 |
var result = null; |
|
|
111 |
var myobj = null |
|
|
112 |
var token = {} |
|
|
113 |
|
|
|
114 |
token.toString = function() { |
|
|
115 |
return "Token ID="+this.setting.ID; |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
//console.log(element) |
|
|
119 |
if(typeof(element)=='undefined'){ |
|
|
120 |
token.setting = defaultTokenSetting |
|
|
121 |
token.setting.ID = this.ID(_this) |
|
|
122 |
} else { |
|
|
123 |
token.setting = element |
|
|
124 |
if(typeof(token.setting.phy) =='undefined') {token.setting.phy = defaultTokenSetting.phy} |
|
|
125 |
if(typeof(token.setting.t) =='undefined') {token.setting.t = _this.settings.stream.now} |
|
|
126 |
if(typeof(token.setting.x) =='undefined') {token.setting.x = _this.settings.sedimentation.incoming.point[element.category].x+(Math.random()*2)} |
|
|
127 |
if(typeof(token.setting.y) =='undefined') {token.setting.y = _this.settings.sedimentation.incoming.point[element.category].y+(Math.random()*2)} |
|
|
128 |
if(typeof(token.setting.size) =='undefined') {token.setting.size = _this.settings.sedimentation.token.size.original} |
|
|
129 |
if(typeof(token.setting.targets)=='undefined') {token.setting.targets=[]} |
|
|
130 |
token.setting.ID = token.setting.ID = this.ID(_this) |
|
|
131 |
if(typeof(token.setting.state) =='undefined') {token.setting.state = 0} |
|
|
132 |
if(typeof(token.setting.shape) =='undefined') {token.setting.shape = defaultTokenSetting.shape } |
|
|
133 |
} |
|
|
134 |
|
|
|
135 |
token.myobj = this.create(_this,token.setting) |
|
|
136 |
//console.log("token.myobj",token.myobj) |
|
|
137 |
|
|
|
138 |
token.flocculate = function(){ |
|
|
139 |
_this.tokens.indexOf(this) |
|
|
140 |
_this.flocculate.destroyIt(_this,this) |
|
|
141 |
return this |
|
|
142 |
} |
|
|
143 |
|
|
|
144 |
token.attr = function(key,value,param){ |
|
|
145 |
//console.log("attr",this.myobj) |
|
|
146 |
if(typeof(value) == "undefined"){ |
|
|
147 |
if(typeof(this[key])!="undefined"){ |
|
|
148 |
return this[key]() |
|
|
149 |
}else{ |
|
|
150 |
return this.myobj.m_userData[key] |
|
|
151 |
} |
|
|
152 |
}else{ |
|
|
153 |
if(typeof(this[key])!="undefined"){ |
|
|
154 |
this[key](value,param) |
|
|
155 |
}else{ |
|
|
156 |
this.myobj.m_userData[key]=value |
|
|
157 |
} |
|
|
158 |
} |
|
|
159 |
return this |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
token.callback = function(value,param){ |
|
|
163 |
if (!arguments.length){return this.myobj.m_userData.callback} |
|
|
164 |
if (typeof(this.myobj.m_userData.callback[value])=="function"){ |
|
|
165 |
return this.myobj.m_userData.callback[value](param) |
|
|
166 |
} else { |
|
|
167 |
return function(param){console.log("callback undefined")} |
|
|
168 |
} |
|
|
169 |
} |
|
|
170 |
|
|
|
171 |
token.size = function(value){ |
|
|
172 |
//console.log(this.attr('state')) |
|
|
173 |
if(this.myobj!=null && this.attr('state')<2){ |
|
|
174 |
if (!arguments.length){return this.myobj.m_shape.m_radius*this.myobj.m_userData.scale;} |
|
|
175 |
this.myobj.m_shape.m_radius = value/this.myobj.m_userData.scale |
|
|
176 |
} |
|
|
177 |
} |
|
|
178 |
token.b2dObj = function(){ |
|
|
179 |
if(this.myobj!=null && this.attr('state')<2){ |
|
|
180 |
return this.myobj |
|
|
181 |
} |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
token.texture = function(value){ |
|
|
185 |
if (!arguments.length){return this.myobj.m_userData.texture.img.src;} |
|
|
186 |
console.log("texture",value) |
|
|
187 |
this.myobj.m_userData.texture={} |
|
|
188 |
this.myobj.m_userData.texture.img = new Image(); |
|
|
189 |
this.myobj.m_userData.texture.img.src = value |
|
|
190 |
} |
|
|
191 |
|
|
|
192 |
//console.log("token",token) |
|
|
193 |
_this.tokens.push(token) |
|
|
194 |
_this.decay.tokens.push(token) |
|
|
195 |
|
|
|
196 |
// Execute suspension callback |
|
|
197 |
if(typeof(this.myobj.m_userData.callback)!="undefined"){ |
|
|
198 |
if(typeof(this.myobj.m_userData.callback.suspension)=="function"){ |
|
|
199 |
var t = _this.select('ID',token.setting.ID) |
|
|
200 |
this.myobj.m_userData.callback.suspension(t) |
|
|
201 |
} |
|
|
202 |
} |
|
|
203 |
|
|
|
204 |
return token |
|
|
205 |
}, |
|
|
206 |
|
|
|
207 |
// CREATE IS A TRY TO UNIFY TOKEN PRODUCTION |
|
|
208 |
create:function(_this,token) { |
|
|
209 |
//targetX,targetY, x, y,size,family |
|
|
210 |
//console.log("create",token) |
|
|
211 |
token.scale = scale = _this.settings.options.scale |
|
|
212 |
|
|
|
213 |
//console.log("DBT") |
|
|
214 |
var xPos = token.x/scale+(Math.random()*0.1); |
|
|
215 |
var yPos = token.y/scale+(Math.random()*0.1); |
|
|
216 |
|
|
|
217 |
// CREATE BALL |
|
|
218 |
var fixDef = new Box2D.Dynamics.b2FixtureDef; |
|
|
219 |
fixDef.density = 0.1; |
|
|
220 |
fixDef.friction = 0.0; |
|
|
221 |
fixDef.restitution = 0.0; |
|
|
222 |
//console.log(token) |
|
|
223 |
|
|
|
224 |
// round |
|
|
225 |
if(token.shape.type == "round"){ |
|
|
226 |
fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(token.size/scale); |
|
|
227 |
// or polygon |
|
|
228 |
}else if(token.shape.type == "polygons"){ |
|
|
229 |
//fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; |
|
|
230 |
fixDef = this.setPolygons(_this,token,fixDef) |
|
|
231 |
}else if(token.shape.type == "box"){ |
|
|
232 |
fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; |
|
|
233 |
//console.log(fixDef) |
|
|
234 |
fixDef.shape.SetAsBox(token.shape.width/scale,token.shape.height/scale) |
|
|
235 |
} |
|
|
236 |
|
|
|
237 |
var bodyDef = new Box2D.Dynamics.b2BodyDef; |
|
|
238 |
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; |
|
|
239 |
bodyDef.position.x = token.x/scale;//+(Math.random()) |
|
|
240 |
bodyDef.position.y = token.y/scale; |
|
|
241 |
|
|
|
242 |
this.myobj = _this.world.CreateBody(bodyDef).CreateFixture(fixDef) |
|
|
243 |
|
|
|
244 |
if(typeof(token.texture)!="undefined"){ |
|
|
245 |
token.texture.img = new Image(); |
|
|
246 |
token.texture.img.src = token.texture.src |
|
|
247 |
} |
|
|
248 |
|
|
|
249 |
if(typeof(token.impulse)!="undefined"){ |
|
|
250 |
this.applyImpulse(this.myobj,token.impulse.angle,token.impulse.power); |
|
|
251 |
} |
|
|
252 |
|
|
|
253 |
if(typeof(token.fillStyle) =="undefined"){ token.fillStyle = this.colorRange(token.category) } |
|
|
254 |
//if(typeof(token.stokeStyle)=="undefined"){ token.stokeStyle = "#000"}//"rgba(0,0,0,0.5)" } |
|
|
255 |
if(typeof(token.lineWidth) =="undefined"){ token.lineWidth = 0 } |
|
|
256 |
if(typeof(token.type) =="undefined"){ token.type="token" } |
|
|
257 |
if(typeof(token.callback) =="undefined"){ |
|
|
258 |
token.callback = {} |
|
|
259 |
// { |
|
|
260 |
// suspension:undefined, |
|
|
261 |
// flocculation:undefined, |
|
|
262 |
// draw:undefined, |
|
|
263 |
// mouseover:undefined, |
|
|
264 |
// mouseout:undefined, |
|
|
265 |
// click:undefined |
|
|
266 |
// } |
|
|
267 |
} |
|
|
268 |
|
|
|
269 |
this.myobj.m_userData = token |
|
|
270 |
this.myobj.attr = this.attr// function (){console.log(this)} |
|
|
271 |
this.myobj.m_userData.mouse = {} |
|
|
272 |
this.myobj.m_userData.mouse.over = false; |
|
|
273 |
this.myobj.m_userData.mouse.down = false; |
|
|
274 |
this.myobj.m_userData.mouse.dragging = false; |
|
|
275 |
this.myobj.m_userData.mouse.statebefore = false; |
|
|
276 |
this.myobj.m_userData.state = 1; // now in the world |
|
|
277 |
|
|
|
278 |
if(token.targets.length==0 && _this.settings.chart.type=="CircleLayout"){ |
|
|
279 |
token.targets[0]={ |
|
|
280 |
x: _this.settings.sedimentation.incoming.target[token.category].x, |
|
|
281 |
y: _this.settings.sedimentation.incoming.target[token.category].y |
|
|
282 |
} |
|
|
283 |
} |
|
|
284 |
|
|
|
285 |
if(token.targets.length>0){ |
|
|
286 |
//console.log() |
|
|
287 |
//CREATE JOIN MOUVEMENT TO TARGET |
|
|
288 |
var md = new _this.phy.b2MouseJointDef(); |
|
|
289 |
md.bodyA = _this.world.GetGroundBody(); |
|
|
290 |
md.bodyB = this.myobj.GetBody(); |
|
|
291 |
md.target.Set(xPos,yPos); |
|
|
292 |
md.collideConnected = true; |
|
|
293 |
md.maxForce = 50 * this.myobj.GetBody().GetMass(); |
|
|
294 |
mouseJoint = _this.world.CreateJoint(md); |
|
|
295 |
mouseJoint.SetTarget(new _this.phy.b2Vec2(token.targets[0].x/scale, token.targets[0].y/scale)); |
|
|
296 |
} |
|
|
297 |
|
|
|
298 |
// ADD INFO IN OBJECT |
|
|
299 |
//categorys[family].value+=1; |
|
|
300 |
//setTimeout(function(){mouseJoint.SetTarget(chart.position.x/scale, chart.position.y/scale)},1000); |
|
|
301 |
//categorys[family].joins.push(mouseJoint); |
|
|
302 |
|
|
|
303 |
return this.myobj; |
|
|
304 |
}, |
|
|
305 |
|
|
|
306 |
applyImpulse:function(bodyId, degrees, power) { |
|
|
307 |
var body = bodyId.GetBody(); |
|
|
308 |
body.ApplyImpulse(new Box2D.Common.Math.b2Vec2(Math.cos(degrees * (Math.PI / 180)) * power, |
|
|
309 |
Math.sin(degrees * (Math.PI / 180)) * power), |
|
|
310 |
body.GetWorldCenter()); |
|
|
311 |
}, |
|
|
312 |
|
|
|
313 |
|
|
|
314 |
setPolygons:function (_this,token,fixDef){ |
|
|
315 |
|
|
|
316 |
fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; |
|
|
317 |
|
|
|
318 |
if(token.shape.points==null){ |
|
|
319 |
token.shape.points = [{x: -1, y: -1}, {x: 1, y: -1}, {x: -1, y:-1},{x: 1, y:-1}] |
|
|
320 |
}; |
|
|
321 |
|
|
|
322 |
for (var i = 0; i < token.shape.points.length; i++) { |
|
|
323 |
var vec = new Box2D.Common.Math.b2Vec2(); |
|
|
324 |
vec.Set(token.shape.points[i].x/scale, token.shape.points[i].y/scale); |
|
|
325 |
token.shape.points[i] = vec; |
|
|
326 |
} |
|
|
327 |
|
|
|
328 |
fixDef.shape.SetAsArray(token.shape.points, token.shape.points.length); |
|
|
329 |
return fixDef; |
|
|
330 |
}, |
|
|
331 |
|
|
|
332 |
|
|
|
333 |
createDataBarBall:function (_this, x, y,size,family) { |
|
|
334 |
//console.log(Math.round(family)) ; |
|
|
335 |
var fixDef = new Box2D.Dynamics.b2FixtureDef; |
|
|
336 |
fixDef.density = 10.0; |
|
|
337 |
fixDef.friction = 0.5; |
|
|
338 |
fixDef.restitution = 0.2; |
|
|
339 |
fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(size/_this.settings.options.scale); |
|
|
340 |
|
|
|
341 |
var bodyDef = new Box2D.Dynamics.b2BodyDef; |
|
|
342 |
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; |
|
|
343 |
bodyDef.position.x = x/_this.settings.options.scale;//+(Math.random()) |
|
|
344 |
bodyDef.position.y = y/_this.settings.options.scale; |
|
|
345 |
|
|
|
346 |
var myobj = _this.world.CreateBody(bodyDef).CreateFixture(fixDef) |
|
|
347 |
myobj.m_userData={type:"BarChartBall", |
|
|
348 |
familyID:'family', |
|
|
349 |
fillColor:this.colorRange(family)} //dynamiq |
|
|
350 |
//console.log(myobj); |
|
|
351 |
return myobj; |
|
|
352 |
}, |
|
|
353 |
|
|
|
354 |
|
|
|
355 |
|
|
|
356 |
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
|
|
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
|
|
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
|
|
|
370 |
// .................................................................... |
|
|
371 |
// OLD OLD OLD Physicals elements |
|
|
372 |
// !!!!!!!!!!!!!!!!!! Have to clean |
|
|
373 |
// .................................................................... |
|
|
374 |
|
|
|
375 |
// !!!!!!!!!! To mix withe the one in piechart and bar chart ::: createBox |
|
|
376 |
createBox:function (world, x, y, width, height, angle, fillColor, fixed) { |
|
|
377 |
if (typeof(fixed) == 'undefined') fixed = true; |
|
|
378 |
var fixDef = new b2FixtureDef; |
|
|
379 |
if (!fixed) fixDef.density = 100.0; |
|
|
380 |
fixDef.friction = 0.6; |
|
|
381 |
fixDef.restitution = 0.3; |
|
|
382 |
|
|
|
383 |
var bodyDef = new b2BodyDef; |
|
|
384 |
bodyDef.type = b2Body.b2_staticBody; |
|
|
385 |
bodyDef.angle = angle ;//* b2Settings.b2_pi; |
|
|
386 |
fixDef.shape = new b2PolygonShape; |
|
|
387 |
fixDef.shape.SetAsBox(width/scale, height/scale); |
|
|
388 |
bodyDef.position.Set(x/scale, y/scale); |
|
|
389 |
var myobj = world.CreateBody(bodyDef).CreateFixture(fixDef) |
|
|
390 |
myobj.m_userData = {type:"Wall",fillColor:fillColor} |
|
|
391 |
console.log(myobj.m_userData) |
|
|
392 |
return myobj; |
|
|
393 |
|
|
|
394 |
}, |
|
|
395 |
createBoxPie:function (world,axis, x, y, width, height, angle, fillColor) { |
|
|
396 |
var bodyDef = new b2BodyDef; |
|
|
397 |
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; |
|
|
398 |
var fixDef = new Box2D.Dynamics.b2FixtureDef; |
|
|
399 |
fixDef.shape = new b2PolygonShape; |
|
|
400 |
fixDef.shape.SetAsBox(width/scale, height/scale); |
|
|
401 |
fixDef.density = 1000000.0; |
|
|
402 |
fixDef.friction = 0.5; |
|
|
403 |
fixDef.restitution = 0.2; |
|
|
404 |
bodyDef.position.Set(x/scale, y/scale); |
|
|
405 |
bodyDef.angle = 0; |
|
|
406 |
var myobj = world.CreateBody(bodyDef).CreateFixture(fixDef); |
|
|
407 |
myobj.m_userData = {type:"Wall",fillColor:fillColor} |
|
|
408 |
|
|
|
409 |
return myobj; |
|
|
410 |
|
|
|
411 |
}, |
|
|
412 |
createBox0D:function (world, x, y, width, height, fixed) { |
|
|
413 |
if (typeof(fixed) == 'undefined') fixed = true; |
|
|
414 |
var boxSd = new b2BoxDef(); |
|
|
415 |
boxSd.restitution = -0.6; |
|
|
416 |
boxSd.friction = .3; |
|
|
417 |
if (!fixed) boxSd.density = 0.01; |
|
|
418 |
boxSd.extents.Set(width, height); |
|
|
419 |
var boxBd = new b2BodyDef(); |
|
|
420 |
boxBd.AddShape(boxSd); |
|
|
421 |
boxBd.position.Set(x,y); |
|
|
422 |
return world.CreateBody(boxBd) |
|
|
423 |
}, |
|
|
424 |
createHiddenBox:function (world, x, y, width, height, fixed) { |
|
|
425 |
if (typeof(fixed) == 'undefined') fixed = true; |
|
|
426 |
var boxSd = new b2BoxDef(); |
|
|
427 |
boxSd.restitution = 0.6; |
|
|
428 |
boxSd.friction = .3; |
|
|
429 |
if (!fixed) boxSd.density = 1.0; |
|
|
430 |
boxSd.extents.Set(width, height); |
|
|
431 |
var boxBd = new b2BodyDef(); |
|
|
432 |
boxBd.AddShape(boxSd); |
|
|
433 |
boxBd.position.Set(x,y); |
|
|
434 |
var myObject = world.CreateBody(boxBd) |
|
|
435 |
myObject.m_shapeList.visibility = 'hidden'; |
|
|
436 |
console.log(myObject); |
|
|
437 |
return myObject |
|
|
438 |
|
|
|
439 |
}, |
|
|
440 |
createBigBall:function (world, x, y) { |
|
|
441 |
var fixDef = new Box2D.Dynamics.b2FixtureDef; |
|
|
442 |
fixDef.density = 1000000.0; |
|
|
443 |
fixDef.friction = 0.5; |
|
|
444 |
fixDef.restitution = 0.2; |
|
|
445 |
fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(20/30); |
|
|
446 |
|
|
|
447 |
var bodyDef = new Box2D.Dynamics.b2BodyDef; |
|
|
448 |
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; |
|
|
449 |
bodyDef.position.x = x; |
|
|
450 |
bodyDef.position.y = y; |
|
|
451 |
var myobj = world.CreateBody(bodyDef).CreateFixture(fixDef) |
|
|
452 |
//console.log(myobj) |
|
|
453 |
return myobj; |
|
|
454 |
}, |
|
|
455 |
|
|
|
456 |
/* |
|
|
457 |
286 |
|
|
458 |
Create standard boxes of given height , width at x,y |
|
|
459 |
287 |
|
|
460 |
*/ |
|
|
461 |
|
|
|
462 |
createPieBox:function (world, x, y, width, height,rotation,color, options){ |
|
|
463 |
|
|
|
464 |
//default setting |
|
|
465 |
|
|
|
466 |
options = $.extend(true, { |
|
|
467 |
'density':10000000.0 , |
|
|
468 |
'friction':1.0 , |
|
|
469 |
'restitution':0.2 , |
|
|
470 |
'linearDamping':0.0 , |
|
|
471 |
'angularDamping':0.0 , |
|
|
472 |
'gravityScale':0.0 , |
|
|
473 |
'type':b2Body.b2_dynamicBody |
|
|
474 |
}, options); |
|
|
475 |
|
|
|
476 |
|
|
|
477 |
var body_def = new b2BodyDef(); |
|
|
478 |
var fix_def = new b2FixtureDef; |
|
|
479 |
fix_def.density = options.density; |
|
|
480 |
fix_def.friction = options.friction; |
|
|
481 |
fix_def.restitution = options.restitution; |
|
|
482 |
fix_def.shape = new b2PolygonShape(); |
|
|
483 |
fix_def.shape.SetAsBox( width/scale , height/scale ); |
|
|
484 |
body_def.position.Set(x/scale , y/scale); |
|
|
485 |
body_def.linearDamping = options.linearDamping; |
|
|
486 |
body_def.angularDamping = options.angularDamping; |
|
|
487 |
body_def.angle = rotation; |
|
|
488 |
|
|
|
489 |
body_def.type = options.type; |
|
|
490 |
var b = world.CreateBody( body_def ); |
|
|
491 |
var f = b.CreateFixture(fix_def); |
|
|
492 |
f.m_userData = {type:"box",familyID:null,fillColor:color} |
|
|
493 |
|
|
|
494 |
return b; |
|
|
495 |
|
|
|
496 |
}, |
|
|
497 |
|
|
|
498 |
createDataBallTarget:function (world,targetX,targetY, x, y,size,family) { |
|
|
499 |
//console.log("DBT") |
|
|
500 |
var xPos = x/scale+(Math.random()*0.1); |
|
|
501 |
var yPos = y/scale+(Math.random()*0.1); |
|
|
502 |
|
|
|
503 |
// CREATE BALL |
|
|
504 |
var fixDef = new Box2D.Dynamics.b2FixtureDef; |
|
|
505 |
fixDef.density = 0.1; |
|
|
506 |
fixDef.friction = 0.0; |
|
|
507 |
fixDef.restitution = 0.0; |
|
|
508 |
fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(size/scale); |
|
|
509 |
var bodyDef = new Box2D.Dynamics.b2BodyDef; |
|
|
510 |
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; |
|
|
511 |
bodyDef.position.x = xPos; |
|
|
512 |
bodyDef.position.y = yPos; |
|
|
513 |
var myobj = world.CreateBody(bodyDef).CreateFixture(fixDef); |
|
|
514 |
|
|
|
515 |
//CREATE JOIN MOUVEMENT TO TARGET |
|
|
516 |
var md = new b2MouseJointDef(); |
|
|
517 |
md.bodyA = world.GetGroundBody(); |
|
|
518 |
md.bodyB = myobj.GetBody(); |
|
|
519 |
md.target.Set(xPos,yPos); |
|
|
520 |
md.collideConnected = true; |
|
|
521 |
md.maxForce = 50* myobj.GetBody().GetMass(); |
|
|
522 |
mouseJoint = world.CreateJoint(md); |
|
|
523 |
mouseJoint.SetTarget(new b2Vec2(targetX/scale, targetY/scale)); |
|
|
524 |
|
|
|
525 |
// ADD INFO IN OBJECT |
|
|
526 |
myobj.m_userData = {type:"PieBall",familyID:family,fillColor:colorScale(family)} |
|
|
527 |
categorys[family].value+=1; |
|
|
528 |
//setTimeout(function(){mouseJoint.SetTarget(chart.position.x/scale, chart.position.y/scale)},1000); |
|
|
529 |
categorys[family].joins.push(mouseJoint); |
|
|
530 |
|
|
|
531 |
return myobj; |
|
|
532 |
}, |
|
|
533 |
|
|
|
534 |
createDataBallPie:function (world,target, x, y,size,family) { |
|
|
535 |
console.log(target) |
|
|
536 |
var xPos = categorys[family].incomingPoint.x/scale+(Math.random()*2/scale); |
|
|
537 |
var yPos = categorys[family].incomingPoint.y/scale; |
|
|
538 |
|
|
|
539 |
// CREATE BALL |
|
|
540 |
var fixDef = new Box2D.Dynamics.b2FixtureDef; |
|
|
541 |
fixDef.density = 0.1; |
|
|
542 |
fixDef.friction = 0.0; |
|
|
543 |
fixDef.restitution = 0.0; |
|
|
544 |
fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(size/scale); |
|
|
545 |
|
|
|
546 |
var bodyDef = new Box2D.Dynamics.b2BodyDef; |
|
|
547 |
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; |
|
|
548 |
|
|
|
549 |
bodyDef.position.x = xPos; |
|
|
550 |
bodyDef.position.y = yPos; |
|
|
551 |
|
|
|
552 |
// I need a distinct list with bodies (and not fixtures) |
|
|
553 |
var myo = world.CreateBody(bodyDef); |
|
|
554 |
myo.m_userData={type:"PieBall",familyID:family,fillColor:categorys[family].color} |
|
|
555 |
|
|
|
556 |
listBodies.push(myo); |
|
|
557 |
var myobj = myo.CreateFixture(fixDef); |
|
|
558 |
|
|
|
559 |
//CREATE JOIN MOUVEMENT TO TARGET |
|
|
560 |
var md = new b2MouseJointDef(); |
|
|
561 |
md.bodyA = world.GetGroundBody(); |
|
|
562 |
md.bodyB = myobj.GetBody(); |
|
|
563 |
md.target.Set(xPos,yPos); |
|
|
564 |
|
|
|
565 |
md.collideConnected = true; |
|
|
566 |
md.maxForce = 100* myobj.GetBody().GetMass(); |
|
|
567 |
mouseJoint = world.CreateJoint(md); |
|
|
568 |
mouseJoint.SetTarget(new b2Vec2(target.position.x/scale, target.position.y/scale)); |
|
|
569 |
|
|
|
570 |
// ADD INFO IN OBJECT |
|
|
571 |
myobj.m_userData={type:"PieBall",familyID:family,fillColor:colorScale(family)} |
|
|
572 |
categorys[family].value+=1; |
|
|
573 |
//setTimeout(function(){mouseJoint.SetTarget(chart.position.x/scale, chart.position.y/scale)},1000); |
|
|
574 |
|
|
|
575 |
return myobj; |
|
|
576 |
}, |
|
|
577 |
|
|
|
578 |
createDataBall:function (_this, x, y,size) { |
|
|
579 |
var fixDef = new Box2D.Dynamics.b2FixtureDef; |
|
|
580 |
fixDef.density = 1.0; |
|
|
581 |
fixDef.friction = 0.5; |
|
|
582 |
fixDef.restitution = 0.2; |
|
|
583 |
fixDef.shape = new Box2D.Collision.Shapes.b2CircleShape(size/_this.settings.options.scale); |
|
|
584 |
|
|
|
585 |
var bodyDef = new Box2D.Dynamics.b2BodyDef; |
|
|
586 |
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; |
|
|
587 |
bodyDef.position.x = x;//+(Math.random()) |
|
|
588 |
bodyDef.position.y = y; |
|
|
589 |
|
|
|
590 |
var myobj = _this.world.CreateBody(bodyDef).CreateFixture(fixDef) |
|
|
591 |
myobj.m_userData={type:"PieBall", |
|
|
592 |
familyID:'family',// add family /// |
|
|
593 |
fillColor:"rgb(200,0,0)"}// add color |
|
|
594 |
|
|
|
595 |
//console.log(myobj) |
|
|
596 |
return myobj; |
|
|
597 |
}, |
|
|
598 |
|
|
|
599 |
} |
|
|
600 |
|
|
|
601 |
})(jQuery); |