Below is my main file. essentially what is happening is that the collision is working when the "rect" object is not moving but as soon as i move the rect object, the collision stops taking affect. I think that it knows it is colliding but it does not do anything (like interaction with the objects at all). does anyone have any thoughts, why this is happening? and how to fix it?
local physics = require("physics") ; physics.start() ; physics.setGravity( 0,0 ) ;
physics.setDrawMode( "debug" )
display.setStatusBar( display.HiddenStatusBar )
math.randomseed( os.time() )
physics.setVelocityIterations(10);
physics.setPositionIterations(20);
--set up some references and other variables
local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY)
local cw, ch = display.contentWidth, display.contentHeight
--set up collision filters
local screenFilter = { categoryBits=2, maskBits=1 }
local objFilter = { categoryBits=1, maskBits=14 }
local fieldFilter = { categoryBits=4, maskBits=1 }
local magnetFilter = { categoryBits=8, maskBits=1 }
--set initial magnet pull
local magnetPull = 0.25
--set up world and background
local screenBounds = display.newRect( -ox, -oy, display.contentWidth+ox+ox, display.contentHeight+oy+oy )
screenBounds.name = "screenBounds"
screenBounds.isVisible = false ; physics.addBody( screenBounds, "static", { isSensor=true, filter=screenFilter } )
local function newPositionVelocity( object )
local math_random = math.random
local side = math_random( 1,4 ) ; local posX ; local posY ; local velX ; local velY
if ( side == 1 or side == 3 ) then
posX = math_random(0,display.pixelHeight)
velX = math_random( -10,10 ) * 5
if ( side == 1 ) then posY = -oy-40 ; velY = math_random( 8,18 ) * 16
else posY = display.contentHeight+oy+40 ; velY = math_random( 8,16 ) * -16
end
else
posY = math_random(0,display.pixelWidth)
velY = math_random( -10,10 ) * 5
if ( side == 4 ) then posX = -ox-40 ; velX = math_random( 8,16 ) * 16
else posX = display.contentWidth+ox+40 ; velX = math_random( 8,16 ) * -16
end
end
object.x = posX ; object.y = posY
object:setLinearVelocity( velX, velY )
object.angularVelocity = math_random( -3,3 ) * 40
object.alpha = 1
end
local rect = display.newRect(100,100,100,60);
local offset = 250
rect.x = cw/2;
rect.y = ch/2-offset;
--rect.xReference = 40;
rect.yReference = offset;
local function objectCollide( self, event )
local otherName = event.other.name
print(event.other.name);
local function onDelay( event )
local action = ""
if ( event.source ) then action = event.source.action ; timer.cancel( event.source ) end
if ( action == "makeJoint" ) then
self.hasJoint = true
self.touchJoint = physics.newJoint( "touch", self, self.x, self.y )
self.touchJoint.frequency = magnetPull
self.touchJoint.dampingRatio = 0.0
self.touchJoint:setTarget( 512, 384 )
elseif ( action == "leftField" ) then
self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil
else
if ( self.hasJoint == true ) then self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil end
newPositionVelocity( self )
end
end
if ( event.phase == "ended" and otherName == "screenBounds" ) then
local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "leftScreen"
elseif ( event.phase == "began" and otherName == "magnet" ) then
transition.to( self, { time=400, alpha=0, onComplete=onDelay } )
elseif ( event.phase == "began" and otherName == "field" and self.hasJoint == false ) then
local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "makeJoint"
elseif ( event.phase == "ended" and otherName == "field" and self.hasJoint == true ) then
local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "leftField"
end
end
local function setupWorld()
for i=1, 20 do
local obj = display.newCircle(0,0, 12 )
physics.addBody( obj, "dynamic", { bounce=.3, radius=12, density = .2 --[[,filter=objFilter]] } )
obj. isBullet = true;
newPositionVelocity( obj )
obj.hasJoint = false
obj.collision = objectCollide ; obj:addEventListener( "collision", obj )
end
local field = display.newCircle(cw/2, ch/2, 320);
field.alpha = 0.2;
field.name = "field";
field.x = display.contentCenterX ; field.y = display.contentCenterY;
physics.addBody( field, "static", { isSensor=true, radius=320, filter=fieldFilter });
magnet = display.newCircle(cw/2, ch/2, 40 )
magnet.name = "magnet"
magnet.x = display.contentCenterX ; magnet.y = display.contentCenterY
physics.addBody( magnet, "static", { bounce=0, radius=40, filter=magnetFilter } )
end
setupWorld()
physics.addBody(rect,"kinematic", { bounce = 1, density = 1})
deg = 0.0;
prevY = 0;
local function onTouch(event)
if event.phase == "began" then
prevY = event.y
end
if (deg<=360) then
deg=0;
end
if(event.y >= prevY) then
deg = deg + 10;
elseif(event.y <= prevY) then
deg = deg -10;
end
prevY = event.y
rect:rotate(deg);
end
Runtime:addEventListener("touch", onTouch);
kinematic bodies don't generate collisions. It needs to be dynamic.
It sounds like your body is going to sleep.
http://developer.coronalabs.com/content/game-edition-physics-bodies#body.isSleepingAllowed
Try:
rect.isSleepingAllowed = false
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.