What I’m trying to do is to have one kinetic body move and collide against another static body. And after it’s been colliding for a certain amount of time, then push through and remove the static body. It works for the most part, except there’s this strange bounce effect as if a sliver of the static body remains, and the kinetic body bounces back off of it.
I’m pretty much a newb and probably doing several things wrong, so please feel free to correct and suggest.
Hopefully it should be clearer after running the following code, which should be plug-n-playable:
main.lua
[lua]local Rocket = require( “rocket” )
local physics = require( “physics” )
score = 100
physics.start()
local ground = display.newRect( 60, 170, 60, 60 )
ground:setReferencePoint( display.TopLeftReferencePoint )
ground.myName = “ground-” … ground.x … “,” … ground.y
physics.addBody( ground, “static”, { density=3.0, friction=0.5, bounce=0.2 } )
local ground2 = display.newRect( 60, 230, 60, 60 )
ground2:setReferencePoint( display.TopLeftReferencePoint )
ground2.myName = “ground-” … ground2.x … “,” … ground2.y
physics.addBody( ground2, “static”, { density=3.0, friction=0.5, bounce=0.2 } )
local ground3 = display.newRect( 120, 230, 60, 60 )
ground3:setReferencePoint( display.TopLeftReferencePoint )
ground3.myName = “ground-” … ground3.x … “,” … ground3.y
physics.addBody( ground3, “static”, { density=3.0, friction=0.5, bounce=0.2 } )
rocket = Rocket.new(160, 110)
local upText = display.newText(“Up”,160, 300, “Arial”, 32)
upText:setTextColor(0, 0, 255)
local leftText = display.newText(“Left”,140, 330, “Arial”, 32)
leftText:setTextColor(0, 0, 255)
------------- my global variables -----------
thrustForceY = -100
thrustForceX = 150
applyThrust = false
holdingLeft = false
local function pressUp (event)
local t = event.target
local phase = event.phase
if( “began” == phase ) then
display.getCurrentStage():setFocus( t )
t.isFocus = true
if( not applyThrust ) then
rocket:addThrust()
end
rocket:applyForce(0, thrustForceY, rocket.x, rocket.y)
applyThrust = true
elseif “ended” == phase or “cancelled” == phase then
rocket:removeThrust()
display.getCurrentStage():setFocus( nil )
t.isFocus = false
applyThrust = false
end
return true
end
upText:addEventListener (“touch”, pressUp)
local function pressLeft (event)
local t = event.target
local phase = event.phase
if( “began” == phase ) then
display.getCurrentStage():setFocus( t )
t.isFocus = true
rocket:applyForce(-thrustForceX, 0, rocket.x, rocket.y)
holdingLeft = true
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
holdingLeft = false
end
return true
end
leftText:addEventListener (“touch”, pressLeft)
[/lua]
rocket.lua
[lua]module(…, package.seeall)
local physics = require( “physics” )
–constructor--------------------
function new(x, y)
local rocket = display.newCircle( x, y, 30 )
rocket.myName = “rocketBall”
physics.addBody( rocket, { density=1.5, friction=0.5, bounce=0.2 } )
rocket.isFixedRotation = true
local thrust = {}
local leftCollidingObject
local leftCollidingStartTime
local drillingLeft = false
function rocket:addThrust()
thrust = display.newRect( rocket.x, rocket.y + (rocket.height / 2) , 10, 10 )
thrust.y = rocket.y + (rocket.height / 2) + (thrust.height / 2)
–physics.addBody( thrust, { density=1.5, friction=0.5, bounce=0.2 } )
–rocket:insert(thrust)
end
function rocket:removeThrust()
–rocket:remove(thrust)
thrust:removeSelf()
end
–function rocket:applyForce( xForce, yForce, atPointX, atPointY )
– rocket:applyForce( xForce, yForce, atPointX, atPointY )
–end
– collision listener for rocket
local function onLocalCollision( rocket, event )
print(“in rocket.collision”)
if ( event.phase == “began” ) then
print( rocket.myName … ": collision began with " … event.other.myName )
print( "rocket.y = " … rocket.y … ", " … event.other.myName … ".y = " … event.other.y )
– check to see if it is roughly on the same row
if(math.abs(rocket.y - (event.other.y + event.other.height/2)) < 10) then
–print( “on same row” )
if( holdingLeft ) then
leftCollidingObject = event.other
leftCollidingStartTime = system.getTimer()
print( " setting leftCollidingObject to " … event.other.myName )
end
else
print(“not on same row”)
end
elseif ( event.phase == “ended” ) then
print( rocket.myName … ": collision ended with " … event.other.myName )
if(math.abs(rocket.y - (event.other.y + event.other.height/2)) < 10) then
–print( “on same row” )
if( holdingLeft ) then
leftCollidingObject = nil
leftCollidingStartTime = 0
print( " clearing leftCollidingObject " )
end
end
end
end
rocket.collision = onLocalCollision
rocket:addEventListener( “collision”, rocket )
local function resumePhysics()
physics.start()
end
local removeLeft
removeLeft = function ()
print ( " in remove Rock " )
if( leftCollidingObject ) then
–physics.removeBody( leftCollidingObject )
leftCollidingObject:removeSelf()
leftCollidingObject = nil
else
print( "leftCollidingObject was nil " )
end
–timer.performWithDelay(200, resumePhysics )
physics.start()
drillingLeft = false
end
– enterFrame listener for rocket
function rocket:enterFrame (event)
if( applyThrust ) then
rocket:applyForce(0, thrustForceY, rocket.x, rocket.y)
thrust.y = rocket.y + (rocket.height / 2) + (thrust.height / 2)
thrust.x = rocket.x
else
–print(“applyThrust is nil”)
end
if( drillingLeft ) then
print(“drilling left so not handling collisions”)
– do nothing while the transitions handle the drilling
else
if( holdingLeft ) then
print(“holding left”)
rocket:applyForce(-thrustForceX, 0, rocket.x, rocket.y)
if( leftCollidingObject ) then
local leftCollideTime = system.getTimer()
local leftCollideTimeElapsed = leftCollideTime - leftCollidingStartTime
print(" elapse time while holding left collide : " … leftCollideTimeElapsed )
– if held long enough, and still colliding with the object, then now go thru it
if( leftCollideTimeElapsed > 500 ) then
drillingLeft = true
physics.pause()
print("rocket current x : " … rocket.x)
print("leftCollidingObject.x = " … leftCollidingObject.x)
print("transition rocket x to : " … (leftCollidingObject.x + leftCollidingObject.width/2) )
–transition.to(rocket, { time=1000, x = (leftCollidingObject.x + leftCollidingObject.width/2), y = (leftCollidingObject.y + leftCollidingObject.height/2) } )
transition.to(rocket, { time=1000, x = (leftCollidingObject.x + leftCollidingObject.width/2) } )
– on the onComplete param, if you put the parenthesis on the function, it will call immediately
transition.to(leftCollidingObject, { time=1000, onComplete=removeLeft, width = 0, x=(leftCollidingObject.x - leftCollidingObject.width/2) } )
end
end
end
end
end
Runtime:addEventListener(“enterFrame”, rocket)
return rocket
end[/lua] [import]uid: 82378 topic_id: 19483 reply_id: 319483[/import]