Corona/Box2D检测与静止不动物体的碰撞 [英] Corona/Box2D detect collision with non-moving static objects

查看:170
本文介绍了Corona/Box2D检测与静止不动物体的碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于发布原因,这是我要执行的操作的简单版本.

For posting reasons here's a simple version of what I'm trying to do.

在屏幕上,我有一个简单的圆形对象,该对象是静态的并且不会移动.然后,用户可以拖放直线.如果直线经过该圆圈,我希望触发碰撞事件.

On the screen I have a simple circle object that is static and doesn't move. The user can then drag and drop a straight line. If the line passes through that circle I'm hoping for the collision event to be triggered.

看来,除非检测到其中一个物体在移动,否则永远不会检测到碰撞.画线时可以检测到碰撞吗?

It appears that unless one of the objects are moving the collision is never detected. Can I detect the collision when the line is drawn?

碰撞事件

function onHit(e)
    print("hit");
end
Runtime:addEventListener("collision", onHit)

触摸事件

local startX = 0;
local startY = 0;
local endX = 0;
local endY = 0;

function onTouch(e)
    if(e.phase == "began") then
        startX = e.x
        startY = e.y
    elseif(e.phase == "moved") then
        endX = e.x
        endY = e.y
    elseif(e.phase == "ended") then
        local line = display.newLine(startX, startY, endX, endY)
        line:setColor(100, 100, 100)
        line.width = 2
        physics.addBody(line, "static", {   })
    end
end
Runtime:addEventListener("touch", onTouch)

创建圈子

local c = display.newCircle(50, 50, 24)
physics.addBody(c, "static", { radius = 24 })

推荐答案

此页面来自 Corona SDK文档描述了bodyType属性,该属性大约位于页面的一半位置.当描述静态"物体时,它说(我的重点):

This page from the Corona SDK docs describes the bodyType property about halfway down the page. When describing "static" bodies, it says (my emphasis):

静态物体不移动,并且彼此不相互作用; 静态物体的例子包括地面或墙壁 弹球机.

static bodies don't move, and don't interact with each other; examples of static objects would include the ground, or the walls of a pinball machine.

这意味着对象之一必须是static以外的其他东西.

That means that one of the objects has to be something other than static.

这是一个主意,尽管我自己还没有尝试过:(请参见下面的更新.)首次创建时,请在行dynamic上添加行.几毫秒后使用timer.performWithDelay函数将其设置为static.如果在此期间发生碰撞事件,您将知道您已经重叠,可以立即将bodyType设置回static.如果没有碰撞事件,则在延迟的例程中bodyType仍为dynamic,并且您会知道没有重叠.在这种情况下,您仍然需要在延迟的例程中将行设置为static.

Here's an idea, although I haven't tried it myself: (See update below.) Make the line dynamic when you first create it. Set it to static a few milliseconds later using a timer.performWithDelay function. If a collision event occurs in the meantime, you will know that you've got an overlap, and can set the bodyType back to static immediately. If you don't get a collision event, the bodyType will still be dynamic in the delayed routine, and you'll know you didn't have an overlap. In this case, you'll still need to set the line to static in the delayed routine.

更新:以您的代码为起点对此进行了测试

我将碰撞事件更改为始终将两个对象的bodyType都设置为静态:

I changed the collision event to always set both objects' bodyType to static:

function onHit(e)
    print("hit")
    e.object1.bodyType = "static"
    e.object2.bodyType = "static"
end

然后,我更改了addBody调用,将其添加为dynamic正文,并使用新代码设置了timer.PerformWithDelay函数以在短时间后进行检查:

Then I changed the addBody call for the line to add it as a dynamic body, with new code to setup a timer.PerformWithDelay function to check after a short time:

physics.addBody(line, "dynamic", {   })

timer.performWithDelay(10, 
    function()
        if line.bodyType == "dynamic" then
            print ("NO OVERLAP")
            line.bodyType = "static"
        end
    end)

不幸的是,结果好坏参半.它在大多数情况下都有效,可能是95%,但在画一条从圆外开始并在圆内结束的线时偶尔会失败,这应该是重叠的,但有时会报告为不重叠.我不知道为什么会这样.无论如何,我都在发布此消息,希望它能助您一臂之力,并且还认为有人可以找出不一致的行为并对我们俩进行教育.

The results were, unfortunately, mixed. It works most of the time, perhaps 95%, but fails occasionally when drawing a line that starts outside the circle and ends inside, which should be an overlap, but is sometimes reported as no overlap. I wasn't able to figure out why this was happening. I'm posting this anyway, hoping that it gets you going, and also thinking that someone may be able to figure out the inconsistent behavior and educate us both.

如果失败,则可以为无重叠"情况添加其他检查,以检查直线的任一端点是否比远离中心的圆的半径小.那将使一切正常,但我想它错过了让物理引擎投入工作的全部要点.

Failing that, you could add an additional check for the "no overlap" case to check if either endpoint of the line is closer than the radius of the circle away from the center. That would be make things work, but I suppose it misses the whole point of letting the physics engine to the work.

无论如何,祝你好运!

这篇关于Corona/Box2D检测与静止不动物体的碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆