瓷砖引擎与汽车的碰撞检测Box 2D

瓷砖引擎与汽车的碰撞检测Box 2D

问题描述:

问题的后续操作,情节提要瓷砖引擎和碰撞检测技术仍然是一个谜.这是代码:

Follow up of this question, Storyboard with Ceramic Tile Engine, and with Collision Detection is still a mystery. Here is the code:

-- hide status bar
display.setStatusBar(display.HiddenStatusBar)
local storyboard = require("storyboard")
--Set up the physics world
local physics = require("physics")
physics.start()
physics.setGravity(0, 0)
physics.setDrawMode('hybrid')
local scene = storyboard.newScene()
local widget = require("widget")

-- Add Hero to Physics
local hero = display.newImage("images/man.png")
hero.x = 40
hero.y = 80
local heroCollisionFilter = { categoryBits = 4, maskBits = 2 }
local heroBody = { filter=heroCollisionFilter, isSensor=true }
physics.addBody(hero, "dynamic", heroBody)

function scene:createScene( event )
    local group = self.view
    local ceramic = require("Ceramic")
    ceramic.showPrints = false
    local map = ceramic.buildMap("maps/map.lua")
    -- collisionLayer = map.layer['Collision']
    -- collisionLayer.ccName = "map"
    -- physics.addBody(collisionLayer, "static", { friction=0.5, bounce=0.3 } )
    map.y = 0
    map.setCameraDamping(10)
    map.layer['World']:insert(hero)
end

function onGlobalCollision(event)
    if(event.phase == "began") then
        print( "Global report: " .. event.object1.ccName .. " & " .. event.object2.ccName .. " collision began" )
    elseif(event.phase == "ended") then
        print( "Global report: " .. event.object1.ccName .. " & " .. event.object2.ccName .. " collision ended" )
    end
    print( "**** " .. event.element1 .. " -- " .. event.element2 )
end

Runtime:addEventListener("collision", onGlobalCollision)
scene:addEventListener( "createScene", scene )

return scene

屏幕截图如下:

但是,冲突不会触发,因为print消息根本不会出现在终端中.

However, collision never triggers, as the print message does not appear in Terminal at all.

我正在使用:

  • Corona SDK
  • 陶瓷砖引擎
  • Corona模块:情节提要,物理学

如何启用碰撞检测?参数正确吗?

How can I enable Collision Detection ? Are the parameters correct ?

编辑2013/10/27

平铺地图设置如下:

在Mac OS X中运行时,不会发生冲突(只有混合图层会更改颜色).

When running in Mac OS X, the collision does not happen ( only the hybrid layer changes color ).

在Windows 7中运行时,代码在以下行崩溃:

When running in Windows 7, the code crashes on this line:

ceramic.buildMap("maps/map.lua")

有错误:

尝试在Ceramic.lua中调用全局"reversePolygon"(nil值): 617

attempt to call global 'reversePolygon' (a nil value) in Ceramic.lua: 617

我注释掉以下几行后,错误消失了:

After I comment out the following lines, the error is gone:

collisionLayer = map.layer['Collision']
collisionLayer.ccName = "map"
physics.addBody(collisionLayer, "static", { friction=0.5, bounce=0.3 } )

但是没有调用碰撞函数.

but the collision function does not get called.

针对那些在使用平铺和瓷砖引擎的Corona SDK中陷入冲突检测的未来人们

在进一步的测试中,我发现碰撞事件未触发的问题是我使用了错误的碰撞事件集.正在发生的碰撞事件是:

In further testing, I found out that the issue of collision event not firing is I used a wrong set of collision events. The working collision events are :

local function onLocalCollision(self, event)
    print("collision")
    if event.phase == "began" then
        print("Collision began")
    elseif event.phase == "ended" then
        print("Collision ended")
    end
end

function onGlobalCollision(event)
    if(event.phase == "began") then
        print( "Global report: " .. event.object1.ccName .. " & " .. event.object2.ccName .. " collision began" )
    elseif(event.phase == "ended") then
        print( "Global report: " .. event.object1.ccName .. " & " .. event.object2.ccName .. " collision ended" )
    end
    print( "**** " .. event.element1 .. " -- " .. event.element2 )
end

function onPostCollision(event)
    print("postCollision")
end

-- Local Collision
hero.collision = onLocalCollision
hero:addEventListener("collision", hero)
-- Global Collision
Runtime:addEventListener("collision", onGlobalCollision)
Runtime:addEventListener("postCollision", onPostCollision)

,每个碰撞对象都必须有一个名称(属性名称ccName,您可以选择任意名称,但必须在Tiled的对象列表中进行设置).

and each collision object has to have a name ( the property name ccName , you can pick any name you want , but it has to be set in Tiled's object list ).

此外,我删除了categoryBitsmaskBits,似乎它们使碰撞检测无效.

Also, I removed the categoryBits and maskBits, seems they make the collision detection invalid.

注意事项:

  • 碰撞层不必通过编程添加到场景中(它将自动添加)
  • 仅需要1组碰撞检测方法(本地/全局)(但可以并行运行2组)
  • 在不需要时关闭混合显示模式,以获得更好的性能
  • 图层格式是什么都没关系(Base64/CSV可以正常工作)
  • 请记住在碰撞层属性中添加physics:enabled(根据@CalebP的评论,physics:frictionphysics:bounce是可选的)
  • Collision layer does not have to add to scene by programming ( it will be added automatically )
  • Only 1 set of collision detection methods ( Local / Global ) is needed ( but 2 sets can be run in parallel )
  • Turn off hybrid display mode when not needed, for better performance
  • It doesn't matter what the Layer format is ( Base64 / CSV works fine )
  • Remember to add physics:enabled in Collision Layer properties ( physics:friction and physics:bounce are optional , as per @CalebP's comment )