Long time ago I did something like this:
[lua]display.setStatusBar( display.HiddenStatusBar )
local physics = require(“physics”)
physics.start()
physics.setScale( 21 ) – a value that seems good for small objects (based on playtesting)
physics.setGravity( 0, 0 ) – overhead view, therefore no gravity vector
–physics.setDrawMode( “hybrid” )
local localGroup = display.newGroup()
local PI = math.pi
local PI2 = 2*PI
local Abs = math.abs
local Cos = math.cos
local Sin = math.sin
local Rnd = math.random
local Ceil = math.ceil
local Atan2 = math.atan2
local Sqrt = math.sqrt
local gScrW = display.contentWidth
local gScrH = display.contentHeight
local linePoints = {}
local moved = false
local line
–[[local loadingImage = display.newImage( “background.png”)
loadingImage.x = 240; loadingImage.y = 160
localGroup:insert ( loadingImage ) ]]–
local Images = {}
– PRELOAD USED IMAGES TO AVOID SLOW DOWNS IMAGES WILL STAY
– INVISIBLE ON STAGE AND WILL BE REMOVED WHEN YOU CALL THE
– EFFECT LIBRARIES CLEANUP FUNCTION.
Images[1] = “ball_black.png”
Images[2] = “ball_blue.png”
Images[3] = “ball_green.png”
Images[4] = “ball_orange.png”
Images[5] = “ball_pink.png”
Images[6] = “ball_purple.png”
Images[7] = “ball_red.png”
Images[8] = “ball_yellow.png”
local startBallPoinst = {}
startBallPoinst[1] = {x = -100, y = -100}
startBallPoinst[2] = {x = (gScrW*0.5), y = -100}
startBallPoinst[3] = {x = (gScrW + 100), y = -100}
startBallPoinst[4] = {x = (gScrW + 100), y = (gScrH*0.5)}
startBallPoinst[5] = {x = (gScrW + 100), y = (gScrH+100)}
startBallPoinst[6] = {x = (gScrW*0.5), y = (gScrH+100)}
startBallPoinst[7] = {x = -100, y = (gScrH+100)}
startBallPoinst[8] = {x = -100, y = (gScrH*0.5)}
– mainBall declaration
local mainBall
local borderCollisionFilter = { categoryBits = 1, maskBits = 26 } – collides with () only
– Boki —
local BumperShape = { 0,0, 0,340}
local BumperShape2 = { 0,0, 480 - display.screenOriginX*2,0}
local BumperShape3 = { 0,0, 0,340}
local BumperShape4 = { -480 + display.screenOriginX*2,0, 0,0}
local bumperBody = { friction=0.5, bounce=0.6, shape=BumperShape, filter=borderCollisionFilter }
local bumperBody2 = { friction=0.5, bounce=0.6, shape=BumperShape2, filter=borderCollisionFilter }
local bumperBody3 = { friction=0.5, bounce=0.6, shape=BumperShape3, filter=borderCollisionFilter }
local bumperBody4 = { friction=0.5, bounce=0.6, shape=BumperShape4, filter=borderCollisionFilter }
local firstLine1 = display.newLine(0 + display.screenOriginX,0, 0 + display.screenOriginX, display.contentHeight)
physics.addBody( firstLine1, “static”, bumperBody )
firstLine1:setColor( 255, 77, 28, 0 )
firstLine1.width = 2
localGroup:insert ( firstLine1 )
local firstLine2 = display.newLine( 0 + display.screenOriginX, 0, display.contentWidth - display.screenOriginX, 0)
physics.addBody( firstLine2, “static”, bumperBody2 )
firstLine2:setColor( 255, 77, 28, 0 )
firstLine2.width = 2
localGroup:insert ( firstLine2 )
local firstLine3 = display.newLine( display.contentWidth - display.screenOriginX, 0, display.contentWidth - display.screenOriginX, display.contentHeight)
physics.addBody( firstLine3, “static”, bumperBody3 )
firstLine3:setColor( 255, 77, 28, 0 )
firstLine3.width = 2
localGroup:insert ( firstLine3 )
local firstLine4 = display.newLine( display.contentWidth - display.screenOriginX, display.contentHeight, 0 + display.screenOriginX, display.contentHeight)
physics.addBody( firstLine4, “static”, bumperBody4 )
firstLine4:setColor( 255, 77, 28, 0 )
firstLine4.width = 2
localGroup:insert ( firstLine4 )
local myRoundedRect = display.newRoundedRect(display.contentWidth*0.5 - 100, display.contentHeight*0.5 - 100, 200, 200, 100)
myRoundedRect.strokeWidth = 3
myRoundedRect:setFillColor(0, 0, 0, 0)
myRoundedRect:setStrokeColor(255, 77, 28, 180)
localGroup:insert( myRoundedRect )
local centralBallCollisionFilter = { categoryBits = 16, maskBits = 5 } – collides with (4 & 1) only
local centralBallPhysics = { density = 3, friction = 0.8, bounce = 0.8, radius=32.0, filter=centralBallCollisionFilter }
local centralBall = display.newImage( “centralBall.png”)
centralBall.x = 240
centralBall.y = 160
physics.addBody( centralBall, centralBallPhysics )
centralBall.isFixedRotation = false
centralBall.linearDamping = 0.5
localGroup:insert( centralBall )
local blue = {}
local blueCollisionFilter = { categoryBits = 4, maskBits = 28 } – collides with () only
local blueBody = { density=0.5, friction=0, bounce=0.5, radius=15.0, filter=blueCollisionFilter }
local ballMarkTime = os.time()
local spawnBall = function()
local coordinates = math.random(1, 8)
local currentBall = display.newImage( Images[math.random(1, 8)], startBallPoinst[coordinates].x, startBallPoinst[coordinates].y )
physics.addBody( currentBall, blueBody )
localGroup:insert ( currentBall )
currentBall:applyForce( (centralBall.x - startBallPoinst[coordinates].x), (centralBall.y - startBallPoinst[coordinates].y), currentBall.x, currentBall.y )
currentBall.linearDamping = 0.45
currentBall.angularDamping = 0.1
currentBall.isFixedRotation = false
currentBall.isBullet = true
– RE-MARK THE TIME
ballMarkTime = os.time()
end
local lecaKulki = function( event )
local ballSpawnCounter = os.time() - ballMarkTime
– check if 1 second has passed since last spawn
if ballSpawnCounter >= 3 then
spawnBall()
end
end
local shootBall = timer.performWithDelay( 3000, lecaKulki, 20 )
– main ball physics
local mainBallCollisionFilter = { categoryBits = 8, maskBits = 5 } – collides with (4 & 1) only
local mainBallPhysics = { density=0.8, friction=0.8, bounce=0.2, radius=10.0, filter=mainBallCollisionFilter }
local mainBall = display.newImage( “ball_white.png” )
physics.addBody( mainBall, mainBallPhysics )
mainBall.isFixedRotation = true
mainBall.isFocus = true
mainBall.alpha = 0
localGroup:insert( mainBall )
– A general function for dragging physics bodies
local function dragBody( event )
local phase = event.phase
local stage = display.getCurrentStage()
local dx, dy, dst, power, speed
if “began” == phase then
mainBall.alpha = 1
mainBall.x = event.x
mainBall.y = event.y
stage:setFocus( mainBall, event.id )
elseif mainBall.isFocus then
if “moved” == phase then
dx = Abs( event.x - mainBall.x )
dy = Abs( event.y - mainBall.y )
dst = (dx*dx + dy*dy)^0.5
power = Abs((1.0-(dst/40)) * 0.7)
mainBall:applyForce( (event.x-mainBall.x)*power, (event.y-mainBall.y)*power , mainBall.x,mainBall.y )
mainBall.angularDamping = 2
mainBall.linearDamping = 2
local pt = {}
pt.x = event.x
pt.y = event.y
table.insert(linePoints,pt)
moved = true
elseif “ended” == phase or “cancelled” == phase then
stage:setFocus( body, nil )
moved = false
linePoints = {}
if ( line ) then line.alpha = 0 end
end
end
– Stop further propagation of touch event
return true
end
–
– PRIVATE: Rysuj lini? z ostatniej zmiennej z tablicy
local function drawLine()
if ( line and #linePoints > 2 ) then line:removeSelf() end
if #linePoints > 2 then
line = display.newLine(mainBall.x,mainBall.y, linePoints[#linePoints].x,linePoints[#linePoints].y)
line:setColor(255, 77, 28, 180)
line.alpha = 1
line.width=5
localGroup:insert( line )
mainBall:toFront()
end
end
–
– PRIVATE: Rysuj scene
local function draw (event )
if ( moved == true ) then
drawLine()
return true
end
end
–
– PRIVATE: Czy wciaz w okregu?
local function isInMiddle (event )
– Dystans!!!
local x = math.pow( ( centralBall.x - (display.contentWidth*0.5)), 2)
local y = math.pow( ( centralBall.y - (display.contentHeight*0.5 )), 2)
local distance = Sqrt( x+y )
if ( distance > 130 ) then
print (“alert”)
end
end
– Make object draggable
Runtime:addEventListener( “touch”, dragBody )
Runtime:addEventListener( “enterFrame” , draw)
Runtime:addEventListener( “enterFrame” , isInMiddle)[/lua] [import]uid: 12704 topic_id: 16204 reply_id: 60408[/import]