so I'm building a game with Corona (in lua) where there are multiple objects that include Physic attributes, and currently trying to store, manipulate them in a way where I can make collision handling easier... the idea is that no 2 out of the 3 objects collide.. the Objects being 1/ a Ball (of many that can be on the screen) 2/ the Table (that has some Physic attributes) and 3/ Pockets (Which end up just being some invisible rectangles)
out of 2 weeks of processing the mess I've come to this conclusion
- Ball hits off other Ball
- Ball hits off Table
- Table does NOT hit off Pocket (static body)
- Pocket do NOT hit off Ball (static body)
- Ball hits off Pocket
a sample of the code structure create the 100 Balls
balls = {}
local i = 1
function createBalls ()
for i = 1,100,1 do -- create the 100 and hide them?
ball = display.newImage("ball.png")
ball.isVisible = false
ball.id = tostring( "ball " ..i )
balls[#ball+1] = ball
sceneGroup:insert(ball[#ball])
end
end
declare 6 pockets (using the id as the flag for collision handling)
local Pocket1 = display.newRect( screenWidth * .28, screenHeight * .5, 15, 18) --Left Pocket 60pts
physics.addBody( Pocket1, "kinematic", {isSensor = true})
Pocket1:addEventListener( "collision", Pocket1 ) Pocket1.isVisible = true Pocket1.alpha = 0.5
Pocket1.id ="Left Pocket 60pts"
local Pocket2 = display.newRect( screenWidth * .3945, screenHeight * .556, 15, 18) -- Left Pocket 30pts
physics.addBody( Pocket2, "kinematic", {isSensor = true} )
Pocket2:addEventListener( "collision", Pocket2 ) Pocket2.isVisible = false
Pocket2.id ="Left Pocket 30pts"
local Pocket3 = display.newRect( screenWidth * .512, screenHeight * .490, 15, 18) -- Center Pocket 10 pts
physics.addBody( Pocket3, "kinematic", {isSensor = true} )
Pocket3:addEventListener( "collision", Pocket3 ) Pocket3.isVisible = false
Pocket3.id ="Center Pocket 10pts"
local Pocket4 = display.newRect( screenWidth * .613, screenHeight * .556, 15, 18) -- Right Pocket 30 pts
physics.addBody( Pocket4, "kinematic", {isSensor = true} )
Pocket4:addEventListener( "collision", Pocket4 ) Pocket4.isVisible = false
Pocket4.id ="Right Pocket 30pts"
local Pocket5 = display.newRect( screenWidth * .736, screenHeight * .5, 15, 18) -- Far Right Pocket 60pts
physics.addBody( Pocket5, "kinematic", {isSensor = true} )
Pocket5:addEventListener( "collision", Pocket5 ) Pocket5.isVisible = false
Pocket5.id ="Right Pocket 60pts"
local Pocket6 = display.newRect( screenWidth * .512, screenHeight * .638, 50, 10) -- Kill-z Pocketphysics.addBody( Pocket6, "kinematic", {isSensor = true} )
Pocket6:addEventListener( "collision", Pocket6 ) Pocket6.isVisible = false
Pocket6.id ="Kill Pocket"
My problem, collision detection only works on the last ball that was loaded,
(in case anyone asks for the loader function)
--handle loader
function shootBall()
createBalls() --obviously this will now spawn 100 balls
if ballLoaded == true then
r1 = -687.5 r2 = -668
r3 = 595 r4 = 645
ball[k].x = screenWidth * .378 ball[k].y = screenHeight * .638
ball[k]:scale(0.16,0.16)
ball[k].isVisible = true
physics.addBody( ball[k], "dynamic", {radius = 5.5, density=15.0, friction=0.8, bounce=0.0 } )
ball[k]:setLinearVelocity( math.random(r1,r2), math.random(r3,r4))
--and here it will have done nothing productive other than fill the screen with balls
end
a Rollback Edit this was originally what would be called but as you can see isn't organised well at all, given the more recent code is a mess..
local i = 1
local balls = {}
local function spawnBall()
if i > 0 or i < 100 then
balls[i] = display.newImage("ball.png")
balls[i].id = "ball "..i
balls[i].x = screenWidth * .378
balls[i].y = screenHeight * .638
balls[i]:scale(0.16,0.16)
balls[i].isVisible = true
physics.addBody( balls[i], "dynamic", {radius = 5.5, density=15.0, friction=0.8, bounce=0.0 } )
balls[i]:setLinearVelocity(math.random(-687.5,-668), math.random(595,645))
sceneGroup:insert(balls[i])
print(balls[i].id.. " shot")
i = i + 1
end
end`
#ball + 1
will evaluate to 1 for ak
bigger than 1). In your loader function, what is k? If it is a global variable, don't do this. Trust me, you don't want to do this this way. Furthermore, you should be careful with indentation, it also helps spotting errors. - pschulz