开发者

Corona SDK collision detection isn't working

I have a (working) function that is generating bullets:

local function shootBullets ( event ) -- It's called through a timer
    local bullet = display.newImageRect("images/Bullet.png", 12, 12) --Make a bullet


    local convertedGunRotation = tankGun.rotation*(math.pi/180) --Don't worry about this just some math for figure out where to shoot the bullet
    local orientation = math.pi

    bullet.x = tankBody.x
    bullet.y = tankBody.y

    physics.addBody(bullet, "kinematic", {bounce = 0}) --Make it a body so I can give it a velocity with the function setLinearVelocity
    bullets:insert( bullet ) --Add it to my bullets group

    bullet.name = "bullet" --Used later for collision detection
    bullet.collision = onCollision --These two lines are what I am doing for collision, isn't working.
    bullet:addEventListener("collision", bullet)


    --This is how I am moving the bullet
    bullet:setLinearVelocity(0*math.cos(convertedGunRotation+orientation)-100*math.sin(convertedGunRotation+orientation), 0*math.sin(convertedGunRotation+orientation)+100*math.cos(convertedGunRotation+orientation))
end

And I have 4 rectangles that surround the screen:

local leftWall = display.newRect(0, 0, 10, _H) --Make the rectangle
leftWall.strokeWidth = 3 --Give its edges a width
leftWall:setFillColor(192, 255, 255) --Fill it with a color to make it visible
leftWall:setStrokeColor(192, 255, 255)
leftWall.name = "wall" --Give it a name for reference in collision detection 
physics.addBody( leftWall, "static") --Make sure it can't move and make it a body.

--Same hold true for the other 3 walls, they surround the player so you can't not hit one with a bullet.
local rightWall = display.newRect(_W-10, 0, 10, _H)
rightWall.strokeWidth = 3
rightWall:setFillColor(192, 255, 255)
rightWall:setStrokeColor(192, 255, 255)
rightWall.name = "wall"
physics.addBody( rightWall, "static")

local bottomWall = display.newRect(0, _H-10, _W, 10)
bottomWall.strokeWidth = 3
bottomWall:setFillColor(192, 255, 255)
bottomWall:setStrokeColor(192, 255, 255)
bottomWall.name = "wall"
physics.addBody( bottomWall, "static")

local topWall = display.newRect(0, 0, _W, 10)
topWall.strokeWidth = 3
topWall:setFillColor(192, 255, 255)
topWall:setStrokeColor(192, 255, 255)
topWall.name = "wall"
physics.addBody( topWall, "static")

And a collision detection function:

local function onCollision( event )
    print("Here") -- Never gets here.
    -- Bullet hit something
    if event.object1.name == "bullet" then
        if event.object2.name == "enemy" then -- I'm going to worry about enemies later
                    --I will do stuff later
        elseif event.object2.name == "wall" then -- It should be hitting a wall
                    --TODO
        end
    end
end

But, the print("Here") function isn't even being called in onC开发者_开发问答ollision function when I run it in Corona Terminal. Any help would be appreciated.


Well, the solution I ended up using (and note this will only work if your game doesn't have any gravity) is that I made the bullet "dynamic" when I added it's body to physics and its isSensor property equal true. That for some reason worked for me. But I will still leave this open till someone finds a better answer (one that works fine with a bullet which has the property of "kinematic". (Worked for me though...)


Check this:

http://developer.anscamobile.com/reference/index/physicsaddbody

At remarks they say:

Note: This API should not be used in a Collision Event handler.

Good luck =)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜