Hi Brent
Thank you, I am sorry to bother you again, but is my code right? I have put the code that checks if the shapes are stable inside the loop of the Shapes table, is that correct? or do I need to move it, to the check function? If I move it to the check function, I get an error in the console, saying that it can’t break the loop …
?
Here is my code.
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 9.8 )
local boundaryHits = 0
local oneMoving = false
local gameUI = require(“gameUI”)
system.activate( “multitouch” )
–physics.setDrawMode( “hybrid” )
display.setStatusBar( display.HiddenStatusBar ) – Hide Status Bar
Background = display.newRect(0, 0, 480, 320)
Background.x = 240
Background.y = 160
ground = display.newRect(0, 0, 480, 50)
ground:setFillColor(0, 0, 0)
ground.x = 240
ground.y = 300
physics.addBody(ground, “static”, {bounce=0, friction=100})
local Walls = {}
Walls[1] = display.newRect( 0, 0, display.contentWidth, 1)
Walls[1].alpha = 0
Walls[1].myName = “topWall”
Walls[2] = display.newRect( 0, 0, 1, display.contentHeight )
Walls[2].alpha = 0
Walls[2].myName = “leftWall”
Walls[3] = display.newRect( display.contentWidth - 1, 0, 1, display.contentHeight )
Walls[3].alpha = 0
Walls[3].myName = “rightWall”
for i = 1, #Walls do
physics.addBody(Walls[i], “static”, {density = 0.1, friction = 0, bounce = 0.1, isSensor = false})
end
local function CheckFunction (event)
if (boundaryHits == 0 and oneMoving == false )then
print(“Shapes Are Inside The Boundaries”)
end
end
timer.performWithDelay(1000, CheckFunction, 0 )
local function onLocalCollision(event)
if ( event.phase == “began” ) then
if event.object1.myName == “Shapes” and event.object2.myName == “Boundaries” then
print(“Began”)
print(boundaryHits)
boundaryHits = boundaryHits + 1 --an object started collision with a boundary sensor
end
elseif ( event.phase == “ended” ) then
if event.object1.myName == “Shapes” and event.object2.myName == “Boundaries” then
print(“Ended”)
boundaryHits = boundaryHits - 1 --an object exited a boundary sensor
print(boundaryHits)
end
end
end
local function dragBody( event )
return gameUI.dragBody( event )
– Substitute one of these lines for the line above to see what happens!
–gameUI.dragBody( event, { maxForce=400, frequency=5, dampingRatio=0.2 } ) – slow, elastic dragging
–gameUI.dragBody( event, { maxForce=20000, frequency=1000, dampingRatio=1.0, center=true } ) – very tight dragging, snaps to object center
end
local Shapes = {}
Shapes[1] = display.newRect(0, 0, 50, 50)
Shapes[1]:setFillColor(205, 20, 300)
Shapes[1].x = 50
Shapes[1].y = 40
Shapes[2] = display.newRect(0, 0, 50, 50)
Shapes[2]:setFillColor(205, 20, 300)
Shapes[2].x = 50
Shapes[2].y = 90
for i = 1, #Shapes do
–physics.addBody(Shapes[i], “Dynamic”, { density=0.3, friction=50.6 } )
physics.addBody(Shapes[i], “Dynamic”, { density=10, friction=0.95} )
Shapes[i].linearDamping = 0.4
Shapes[i].angularDamping = 0.6
Shapes[i]:addEventListener( “touch”, dragBody )
Shapes[i].myName = “Shapes”
local thisOne = Shapes[i] --set reference to the current object in loop cycle
local vx, vy = thisOne:getLinearVelocity() --get the linear velocities of that object
local av = thisOne.angularVelocity --get the angular velocity of that object
–check all conditions for “stability”
–adjust these integer values for how “fussy” you want to be… smaller means less movement.
if ( vx > 4 or vx < -4 or vy > 4 or vy < -4 or av > 4 or av < -4 ) then
oneMoving = true
break --if any object is “unstable”, break the loop! No reason to continue checking!
end
end
local Boundaries = {}
Boundaries[1] = display.newRect(0, 0, 3, 105)
Boundaries[1].x = 210; Boundaries[1].y =222
Boundaries[2] = display.newRect(0, 0, 3, 105)
Boundaries[2].x = 269; Boundaries[2].y =222
Boundaries[3] = display.newRect(0, 0, 57, 3)
Boundaries[3].x = 240; Boundaries[3].y =171
Boundaries[4] = display.newRect(0, 0, 57, 3)
Boundaries[4].x = 240; Boundaries[4].y =275
for i = 1, #Boundaries do
Boundaries[i]:setFillColor(0, 0, 0)
physics.addBody(Boundaries[i], “static”, {isSensor=true })
Boundaries[i].myName = “Boundaries”
end
local PG = {}
PG[1] = display.newRect(0, 0, 222, 105)
PG[1].x = 380; PG[1].y =222
PG[2] = display.newRect(0, 0, 222, 105)
PG[2].x = 100; PG[2].y =222
PG[3] = display.newRect(0, 0, 755, 165)
PG[3].x = 100; PG[3].y = 85
for i = 1, #PG do
PG[i].alpha = 0
physics.addBody(PG[i], “static”, {isSensor=true })
PG[i].myName = “Boundaries”
end
Runtime:addEventListener( “collision”, onLocalCollision )[/lua] [import]uid: 122802 topic_id: 25055 reply_id: 104000[/import]