Share Your Code

Soft physics body

Posted by mightE, Posted on October 28, 2011

I thought soft bodies looked cool and thought of a way to make one myself. This is probably not a good way to do it. Crashes corona if the body starts spinning too wildly.

This requires
http://developer.anscamobile.com/code/make-outside-edge-walls-module
if you can't be bothered to get that module, delete:

"walls = require("walls")
walls.make() "
from the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
display.setStatusBar( display.HiddenStatusBar )
_W = display.contentWidth
_H = display.contentHeight
 
local W = _W
local H = _H
 
 
 
_G.physics = require( "physics" )
physics.start()
 
--physics.setGravity( 0, 0)
--physics.setDrawMode( "hybrid" )
 
 
walls = require("walls")
walls.make()
 
ball = display.newCircle(400,500,200)
ball.alpha = 0.2
physics.addBody(ball, {radius=10})
---[[
local points = {}
radius = 10
--top right
points[1] = display.newCircle(400,300,radius)
points[2] = display.newCircle(480,315,radius)
points[3] = display.newCircle(550,365,radius)
points[4] = display.newCircle(585,420,radius)
points[5] = display.newCircle(600,500,radius)
--bottom right
points[6] = display.newCircle(585,580,radius)
points[7] = display.newCircle(550,635,radius)
points[8] = display.newCircle(480,680,radius)
points[9] = display.newCircle(400,700,radius)
--bottom left
points[10] = display.newCircle(300,680,radius)
points[11] = display.newCircle(250,635,radius)
points[12] = display.newCircle(220,580,radius)
points[13] = display.newCircle(200,500,radius)
--top left
points[14] = display.newCircle(215,420,radius)
points[15] = display.newCircle(250,365,radius)
points[16] = display.newCircle(325,315,radius)
 
 
local joint = {}
local joint2 = {}
for i = 1, 16 do 
physics.addBody(points[i], {radius=1})
end
--]]
---[[
for i = 1, 16 do 
        if i == 16 then
                joint[i] = physics.newJoint( "pivot", points[i], points[1], points[i].x, points[i].y)
        else
                joint[i] = physics.newJoint( "pivot", points[i], points[i+1], points[i].x, points[i].y)
        end
        
        joint[i].isLimitEnabled = true -- (boolean)
 
joint[i]:setRotationLimits( -1, 1 )
        
end
--]]
 
for i = 1, 16 do
        joint2[i] = physics.newJoint( "distance", points[i], ball, points[i].x,points[i].y, ball.x,ball.y)
        --points[i].alpha = 0
end
 
 
Runtime:addEventListener("touch", function(event) print(event.x, event.y) end)
        
-- A general function for dragging physics bodies
local function dragBody( event )
        local body = event.target
        local phase = event.phase
        local stage = display.getCurrentStage()
 
        if "began" == phase then
                stage:setFocus( body, event.id )
                body.isFocus = true
 
                -- Create a temporary touch joint and store it in the object for later reference
                body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )
 
        elseif body.isFocus then
                if "moved" == phase then
                
                        -- Update the joint to track the touch
                        body.tempJoint:setTarget( event.x, event.y )
 
                elseif "ended" == phase or "cancelled" == phase then
                        stage:setFocus( body, nil )
                        body.isFocus = false
                        
                        -- Remove the joint when the touch ends                 
                        body.tempJoint:removeSelf()
                        
                end
        end
 
        -- Stop further propagation of touch event
        return true
end
 
-- Make object draggable
ball:addEventListener( "touch", dragBody )