Você está na página 1de 10

/Users/angelaferraiolo/Desktop/ragdoll.

lua
Saved: 8/20/13 11:57:55 AM
1
2
3
4
5
6
7
8
9
10
11

Page 1 of 10
Printed For: Angela Ferraiolo

-- Abstract: Ragdoll sample project


-- A port of the ragdoll from http://www.box2dflash.org/
--- Version: 1.0
--- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/lic
-- Copyright (C) 2010 Corona Labs Inc. All Rights Reserved.
--- History
-- 1.0!!
6/7/12! !
Initial version
----------------------------------------------------------------------------

12
13

local ragdoll = {}

14
15

local gameUI = require("gameUI")

16
17
18
19
20
21
22
23
24
25
26
27
28

ragdoll.createWalls = function()
!
--> Create Walls
!
!
local walls = display.newGroup()
!
!
local leftWall = display.newRect
!
local rightWall = display.newRect
!
local ceiling
= display.newRect
!
local floor
= display.newRect
!
!
local b = 0.3 -- 0.0
!
local f = 0.1 -- 10

(-100, 0, 101, display.contentHeight)


(display.contentWidth, 0, 100, display
(0, -100, display.contentWidth, 101)
(0, display.contentHeight, display.con

29
30
31
32
33
34
35
36
37
38
39
40
41

!
!
!
!
!
!
!
!
!
!
!
end

physics.addBody
physics.addBody
physics.addBody
physics.addBody

(leftWall, "static", {bounce = b, friction = f})


(rightWall, "static", {bounce = b, friction = f})
(ceiling, "static", {bounce = b, friction = f})
(floor, "static", {bounce = b, friction = f})

walls:insert(leftWall)
walls:insert(rightWall)
walls:insert(ceiling)
walls:insert(floor)
return walls

42
43
44
45

local function setFill(ob, param)


!
ob:setFillColor(param[1], param[2], param[3], param[4] or 255)
end

/Users/angelaferraiolo/Desktop/ragdoll.lua
Saved: 8/20/13 11:57:55 AM

Page 2 of 10
Printed For: Angela Ferraiolo

46
47

ragdoll.newRagDoll = function(originX, originY, colorTable)

48
49
50
51
52
53
54
55
56

!
!
!
!
!
!
!
!

--> Create Ragdoll Group

!
!
!
!
!
!
!
!
!
!
!
!
!

--[[
var bd:b2BodyDef;
var circ:b2CircleDef = new b2CircleDef();
var box:b2PolygonDef = new b2PolygonDef();
var jd:b2RevoluteJointDef = new b2RevoluteJointDef();
--]]

!
!
!
!
!
!
!
!
!
!
!
!
!

--[[
circ.density = 1.0;
circ.friction = 0.4;
circ.restitution = 0.3;
bd = new b2BodyDef();
bd.position.Set(startX, startY);
var head:b2Body = m_world.CreateBody(bd);
head.CreateShape(circ);
head.SetMassFromShapes();
//if (i == 0){
!
head.ApplyImpulse(new b2Vec2(Math.random() * 100 - 50, Math.random()
//}
--]]

local w,h = 2*15,2*10

!
!
!

-- Torso1
local torsoA = display.newRect( -w*0.5, -h*0.5, w, h )
torsoA:translate( startX, (startY + 28) )

local spacing = 1
local ragdoll = display.newGroup ()
local startX = originX+100
local startY = originY+50

57
58
59
60
61
62
63
64
65
66
67
68
69
70

-- Head
local head = display.newCircle( 0, 0, 12.5 )
head.x = startX
head.y = startY
setFill(head, colorTable)
ragdoll:insert (head)

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

/Users/angelaferraiolo/Desktop/ragdoll.lua
Saved: 8/20/13 11:57:55 AM
91
92
93
94
95
96
97
98
99
100
101
102
103

Page 3 of 10
Printed For: Angela Ferraiolo

!
!
!
!
!
!
!
!
!
!
!
!
!

setFill(torsoA, colorTable)
ragdoll:insert (torsoA)
--[[
box.SetAsBox(15, 10);
box.density = 1.0;
box.friction = 0.4;
box.restitution = 0.1;
bd = new b2BodyDef();
bd.position.Set(startX, (startY + 28));
var torso1:b2Body = m_world.CreateBody(bd);
torso1.CreateShape(box);
torso1.SetMassFromShapes();
--]]

!
!
!
!
!
!
!
!
!
!
!
!

-- Torso2
local torsoB = display.newRect( -w*0.5, -h*0.5, w, h )
torsoB:translate( startX, (startY + 43) )
setFill(torsoB, colorTable)
ragdoll:insert (torsoB)
--[[
bd = new b2BodyDef();
bd.position.Set(startX, (startY + 43));
var torso2:b2Body = m_world.CreateBody(bd);
torso2.CreateShape(box);
torso2.SetMassFromShapes();
--]]

!
!
!
!
!
!
!
!
!
!
!
!

-- Torso3
local torsoC = display.newRect( -w*0.5, -h*0.5, w, h )
torsoC:translate( startX, (startY + 58) )
setFill(torsoC, colorTable)
ragdoll:insert (torsoC)
--[[
bd = new b2BodyDef();
bd.position.Set(startX, (startY + 58));
var torso3:b2Body = m_world.CreateBody(bd);
torso3.CreateShape(box);
torso3.SetMassFromShapes();
--]]

!
!

-- UpperArm
local w,h = 2*18,2*6.5

!
!

-- L
local upperArmL = display.newRect( -w*0.5, -h*0.5, w, h )

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

/Users/angelaferraiolo/Desktop/ragdoll.lua
Saved: 8/20/13 11:57:55 AM
136
137
138

Page 4 of 10
Printed For: Angela Ferraiolo

!
!
!

upperArmL:translate( (startX - 30), (startY + 20) )


setFill(upperArmL, colorTable)
ragdoll:insert (upperArmL)

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!

-- R
local upperArmR = display.newRect( -w*0.5, -h*0.5, w, h )
upperArmR:translate( (startX + 30), (startY + 20) )
setFill(upperArmR, colorTable)
ragdoll:insert (upperArmR)
--[[
box.SetAsBox(18, 6.5);
box.density = 1.0;
box.friction = 0.4;
box.restitution = 0.1;
bd = new b2BodyDef();
// L
bd.position.Set((startX - 30), (startY + 20));
var upperArmL:b2Body = m_world.CreateBody(bd);
upperArmL.CreateShape(box);
upperArmL.SetMassFromShapes();
// R
bd.position.Set((startX + 30), (startY + 20));
var upperArmR:b2Body = m_world.CreateBody(bd);
upperArmR.CreateShape(box);
upperArmR.SetMassFromShapes();
--]]

!
!
!
!
!

-- L
local lowerArmL = display.newRect( -w*0.5, -h*0.5, w, h )
lowerArmL:translate( (startX - 57), (startY + 20) )
setFill(lowerArmL, colorTable)
ragdoll:insert (lowerArmL)

!
!
!
!
!
!
!
!
!

-- R
local lowerArmR = display.newRect( -w*0.5, -h*0.5, w, h )
lowerArmR:translate( (startX + 57), (startY + 20) )
setFill(lowerArmR, colorTable)
ragdoll:insert (lowerArmR)
--[[
box.SetAsBox(17, 6);
box.density = 1.0;
box.friction = 0.4;

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

-- LowerArm
local w,h = 2*17,2*6

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

/Users/angelaferraiolo/Desktop/ragdoll.lua
Saved: 8/20/13 11:57:55 AM
181
182
183
184
185
186
187
188
189
190
191
192
193

Page 5 of 10
Printed For: Angela Ferraiolo

!
!
!
!
!
!
!
!
!
!
!
!
!

box.restitution = 0.1;
bd = new b2BodyDef();
// L
bd.position.Set((startX - 57), (startY + 20));
var lowerArmL:b2Body = m_world.CreateBody(bd);
lowerArmL.CreateShape(box);
lowerArmL.SetMassFromShapes();
// R
bd.position.Set((startX + 57), (startY + 20));
var lowerArmR:b2Body = m_world.CreateBody(bd);
lowerArmR.CreateShape(box);
lowerArmR.SetMassFromShapes();
--]]

!
!

-- UpperLeg
local w,h = 2*7.5,2*22

!
!
!
!
!

-- L
local upperLegL = display.newRect( -w*0.5, -h*0.5, w, h )
upperLegL:translate( (startX - 8), (startY + 85) )
setFill(upperLegL, colorTable)
ragdoll:insert (upperLegL)

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!

-- R
local upperLegR = display.newRect( -w*0.5, -h*0.5, w, h )
upperLegR:translate( (startX + 8), (startY + 85) )
setFill(upperLegR, colorTable)
ragdoll:insert (upperLegR)
--[[
box.SetAsBox(7.5, 22);
box.density = 1.0;
box.friction = 0.4;
box.restitution = 0.1;
bd = new b2BodyDef();
// L
bd.position.Set((startX - 8), (startY + 85));
var upperLegL:b2Body = m_world.CreateBody(bd);
upperLegL.CreateShape(box);
upperLegL.SetMassFromShapes();
// R
bd.position.Set((startX + 8), (startY + 85));
var upperLegR:b2Body = m_world.CreateBody(bd);
upperLegR.CreateShape(box);
upperLegR.SetMassFromShapes();
--]]

194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

/Users/angelaferraiolo/Desktop/ragdoll.lua
Saved: 8/20/13 11:57:55 AM

Page 6 of 10
Printed For: Angela Ferraiolo

226
227
228

!
!

-- LowerLeg
local w,h = 2*6,2*20

!
!
!
!
!

-- L
local lowerLegL = display.newRect( -w*0.5, -h*0.5, w, h )
lowerLegL:translate( (startX - 8), (startY + 120) )
setFill(lowerLegL, colorTable)
ragdoll:insert (lowerLegL)

229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

!
-- R
!
local lowerLegR = display.newRect( -w*0.5, -h*0.5, w, h )
!
lowerLegR:translate( (startX + 8), (startY + 120) )
!
setFill(lowerLegR, colorTable)
!
ragdoll:insert (lowerLegR)
!
--[[
!
box.SetAsBox(6, 20);
!
box.density = 1.0;
!
box.friction = 0.4;
!
box.restitution = 0.1;
!
bd = new b2BodyDef();
!
// L
!
bd.position.Set((startX - 8), (startY + 120));
!
var lowerLegL:b2Body = m_world.CreateBody(bd);
!
lowerLegL.CreateShape(box);
!
lowerLegL.SetMassFromShapes();
!
// R
!
bd.position.Set((startX + 8), (startY + 120));
!
var lowerLegR:b2Body = m_world.CreateBody(bd);
!
lowerLegR.CreateShape(box);
!
lowerLegR.SetMassFromShapes();
!
--]]
-local d=1
local e=0.1
local f=0.4
!
!
physics.addBody (head, {bounce = 0.3, density=1.0, friction=0.4, radius
!
physics.addBody (torsoA, {bounce = e, density=d, friction = f})
!
physics.addBody (torsoB, {bounce = e, density=d, friction = f})
!
physics.addBody (torsoC, {bounce = e, density=d, friction = f})
!
physics.addBody (upperArmL, {bounce = e, density=d, friction = f})
!
physics.addBody (upperArmR, {bounce = e, density=d, friction = f})
!
physics.addBody (lowerArmL, {bounce = e, density=d, friction = f})
!
physics.addBody (lowerArmR, {bounce = e, density=d, friction = f})

/Users/angelaferraiolo/Desktop/ragdoll.lua
Saved: 8/20/13 11:57:55 AM
271
272
273
274
275
276
277
278
279
280
281

Page 7 of 10
Printed For: Angela Ferraiolo

!
physics.addBody (upperLegL, {bounce = e, density=d, friction = f})
!
physics.addBody (upperLegR, {bounce = e, density=d, friction = f})
!
physics.addBody (lowerLegL, {bounce = e, density=d, friction = f})
!
physics.addBody (lowerLegR, {bounce = e, density=d, friction = f})
--]]
!
local function addFrictionJoint(a, b, posX, posY, lowerAngle, upperAngle
!
!
local j = physics.newJoint ( "pivot", a, b, posX, posY, rFrom, rTo)
!
!
j.isLimitEnabled = true
!
!
j:setRotationLimits (lowerAngle, upperAngle)
!
!
return j
!
end

282
283
284
285
286
287
288
289
290
291
292
293

!
!
!
!
!
!
!
!
!
!
!

-- JOINTS
--jd.enableLimit = true;
-- Head to shoulders
addFrictionJoint( torsoA, head, startX, (startY + 15), -40, 40 )
--[[
jd.lowerAngle = -40 / (180/Math.PI);
jd.upperAngle = 40 / (180/Math.PI);
jd.Initialize(torso1, head, new b2Vec2(startX, (startY + 15)));
m_world.CreateJoint(jd);
--]]

!
!
!

-- Upper arm to shoulders


-- L
addFrictionJoint( torsoA, upperArmL, (startX - 18), (startY + 20), -85,

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!

-- R
addFrictionJoint( torsoA, upperArmR, (startX + 18), (startY + 20), -130,
--[[
jd.lowerAngle = -85 / (180/Math.PI);
jd.upperAngle = 130 / (180/Math.PI);
jd.Initialize(torso1, upperArmL, new b2Vec2((startX - 18), (startY + 20)
m_world.CreateJoint(jd);
// R
jd.lowerAngle = -130 / (180/Math.PI);
jd.upperAngle = 85 / (180/Math.PI);
jd.Initialize(torso1, upperArmR, new b2Vec2((startX + 18), (startY + 20)
m_world.CreateJoint(jd);
--]]

294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

-- Lower arm to upper arm


-- L
addFrictionJoint( upperArmL, lowerArmL, (startX - 45), (startY + 20), -1

/Users/angelaferraiolo/Desktop/ragdoll.lua
Saved: 8/20/13 11:57:55 AM

Page 8 of 10
Printed For: Angela Ferraiolo

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
--!
--!
--!

-- R
addFrictionJoint( upperArmR, lowerArmR, (startX + 45), (startY + 20), -1
--[[
// L
jd.lowerAngle = -130 / (180/Math.PI);
jd.upperAngle = 10 / (180/Math.PI);
jd.Initialize(upperArmL, lowerArmL, new b2Vec2((startX - 45), (startY +
m_world.CreateJoint(jd);
// R
jd.lowerAngle = -10 / (180/Math.PI);
jd.upperAngle = 130 / (180/Math.PI);
jd.Initialize(upperArmR, lowerArmR, new b2Vec2((startX + 45), (startY +
m_world.CreateJoint(jd);
--]]

!
!
!
!
!
!

--[[
jd.lowerAngle = -15 / (180/Math.PI);
jd.upperAngle = 15 / (180/Math.PI);
jd.Initialize(torso1, torso2, new b2Vec2(startX, (startY + 35)));
m_world.CreateJoint(jd);
--]]

!
!
!
!
!
!
!
!
!
!

-- Stomach/hips
addFrictionJoint( torsoB, torsoC, (startX), (startY + 50), -15, 15 )
--[[
jd.Initialize(torso2, torso3, new b2Vec2(startX, (startY + 50)));
m_world.CreateJoint(jd);
--]]

!
!
!
!
!

-- R
addFrictionJoint( torsoC, upperLegR, (startX + 8), (startY + 72), -45, 2
--[[
// L
jd.lowerAngle = -25 / (180/Math.PI);

-- Shoulders/stomach
local j = addFrictionJoint( torsoA, torsoB, (startX), (startY + 35), -15
Runtime:addEventListener( "enterFrame", function()
!
print( j.jointAngle, j.isLimitEnabled, j.isMotorEnabled, j.motorSpee
end )

337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

-- Torso to upper leg


-- L
addFrictionJoint( torsoC, upperLegL, (startX - 8), (startY + 72), -25, 4

355
356
357
358
359
360

/Users/angelaferraiolo/Desktop/ragdoll.lua
Saved: 8/20/13 11:57:55 AM
361
362
363
364
365
366
367
368
369

Page 9 of 10
Printed For: Angela Ferraiolo

!
!
!
!
!
!
!
!
!

jd.upperAngle = 45 / (180/Math.PI);
jd.Initialize(torso3, upperLegL, new b2Vec2((startX - 8), (startY + 72))
m_world.CreateJoint(jd);
// R
jd.lowerAngle = -45 / (180/Math.PI);
jd.upperAngle = 25 / (180/Math.PI);
jd.Initialize(torso3, upperLegR, new b2Vec2((startX + 8), (startY + 72))
m_world.CreateJoint(jd);
--]]

!
!
!

-- Upper leg to lower leg


-- L
addFrictionJoint( upperLegL, lowerLegL, (startX - 8), (startY + 105), -2

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!

-- R
addFrictionJoint( upperLegR, lowerLegR, (startX + 8), (startY + 105), -1
--[[
// L
jd.lowerAngle = -25 / (180/Math.PI);
jd.upperAngle = 115 / (180/Math.PI);
jd.Initialize(upperLegL, lowerLegL, new b2Vec2((startX - 8), (startY + 1
m_world.CreateJoint(jd);
// R
jd.lowerAngle = -115 / (180/Math.PI);
jd.upperAngle = 25 / (180/Math.PI);
jd.Initialize(upperLegR, lowerLegR, new b2Vec2((startX + 8), (startY + 1
m_world.CreateJoint(jd);
--]]

370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405

--[[
// Add stairs on the left
for (var j:int = 1; j <= 10; j++){
!
box.SetAsBox((10*j), 10);
!
box.density = 0.0;
!
box.friction = 0.4;
!
box.restitution = 0.3;
!
bd = new b2BodyDef();
!
bd.position.Set((10*j), (150 + 20*j));
!
head = m_world.CreateBody(bd);
!
head.CreateShape(box);
!
head.SetMassFromShapes();
}
// Add stairs on the right
for (var k:int = 1; k <= 10; k++){

/Users/angelaferraiolo/Desktop/ragdoll.lua
Saved: 8/20/13 11:57:55 AM
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426

!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!

!
!
!
!
!
!
!
!
!
}

!
!
!
!
!
!
!
!
!
!
!
!
!

function ragdoll:touch( event )


!
gameUI.dragBody( event )
end

Page 10 of 10
Printed For: Angela Ferraiolo

box.SetAsBox((10*k), 10);
box.density = 0.0;
box.friction = 0.4;
box.restitution = 0.3;
bd = new b2BodyDef();
bd.position.Set((640-10*k), (150 + 20*k));
head = m_world.CreateBody(bd);
head.CreateShape(box);
head.SetMassFromShapes();

box.SetAsBox(30, 40);
box.density = 0.0;
box.friction = 0.4;
box.restitution = 0.3;
bd = new b2BodyDef();
bd.position.Set(320, 320);
head = m_world.CreateBody(bd);
head.CreateShape(box);
head.SetMassFromShapes();
--]]

427
428
429
430
431
432
433
434
435
436
437
438
439
440

head:addEventListener ( "touch", ragdoll )


upperLegL:addEventListener ( "touch", ragdoll
upperLegR:addEventListener ( "touch", ragdoll
lowerLegL:addEventListener ( "touch", ragdoll
lowerLegR:addEventListener ( "touch", ragdoll
upperArmL:addEventListener ( "touch", ragdoll
upperArmR:addEventListener ( "touch", ragdoll
lowerArmL:addEventListener ( "touch", ragdoll
lowerArmR:addEventListener ( "touch", ragdoll

441
442
443

!
return ragdoll
end

444
445

return ragdoll

)
)
)
)
)
)
)
)

Você também pode gostar