Farseer 3.3 检查未启用的物体在启用(生成)时是否会发生碰撞

发布于 2024-12-02 05:56:08 字数 199 浏览 2 评论 0原文

我正在使用 Farseer 3.3 和 XNA。

我有一个问题无法以好的方式解决。

我遇到的情况是,有一个世界,里面有很多人都在努力做那里的事情。

然后我有一个物体的主体被设置为未启用,有没有一种很好的方法来检查主体当前是否与任何物体发生碰撞?即想知道它是否是一个干净的生成,我应该启用它并将其设置为动态吗?

提前致谢。

I am using Farseer 3.3 and XNA.

I have a problem that i just cant solve in a nice way.

I have a situation where there is a world with bodys in it all working away doing there thing.

I then have an object thats body is set to not enabled, is there a nice way of checking if the body is currently colliding with anything? i.e. to know if it would be a clean spawn should I make it enabled and set it to dynamic?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2024-12-09 05:56:08

对于在尝试解决同类问题时发现这篇文章的其他人来说,我最终是如何做到的。

在此示例中,我使用“Item”一词来表示包含主体等的类,这可以是您的玩家/子弹/其他内容。

在您的 Item 类中订阅碰撞和分离事件的实体。

        this.Body.OnCollision += Body_OnCollision;
        this.Body.OnSeparation +=  Body_OnSeperation;

然后为该项目设置一个成员来保存碰撞和分离的计数。

私有 int _canBePlacedCounter = 0;

在事件处理程序方法上,增加或减少成员计数。在下面的代码中,对于我自己来说有额外的条件,我只想在将项目放入“世界”时执行这些操作。

    private bool Body_OnCollision(Fixture fixturea, Fixture fixtureb, Contact contact)
    {
        if(this.IsBeingPlaced)
        {
            _canBePlacedCounter++;
        }

        return true;
    }

    private void Body_OnSeperation(Fixture fixturea, Fixture fixtureb)
    {
        if (this.IsBeingPlaced)
        {
            _canBePlacedCounter--;
        }
    }

然后我们可以设置一个简单的公共属性(如果我们想进入编码标准,这确实应该是一个方法,但现在不是时间或地点)

    public bool CanBeDropped
    {
        get
        {
            if (_canBePlacedCounter == 0) return true;
            else return false;
        }
    }

这个实现的原因是,我最初有一个 bool,我将其设置为 true 或每当我收到其中一个事件时,都是 false。问题是...如果您的项目与项目 A 碰撞,然后与项目 B 碰撞,然后离开项目 A,您会得到未碰撞的读数。所以......使用这样的计数器,我们可以计算和计算所有的碰撞和分离。相信我,这绝对是一种魅力。

希望它对那里的人有用。

更新:我发现这只适用于基本矩形。如果你进入具有许多多边形的物体,例如从图像自动生成的物体,那么它非常不可靠,因为无论出于何种原因,farseer 定期引发的分离事件少于碰撞事件,这意味着计数器通常不会返回到 0。

For anyone else finding this post while trying to solve the same kind of problem here is how I did it in the end.

In this example I use the word Item to represent the class that contains a body etc, this could be your player / bullet / whatever.

In your Item class subscribe to the bodies on collision and on separation events.

        this.Body.OnCollision += Body_OnCollision;
        this.Body.OnSeparation +=  Body_OnSeperation;

then setup a member for the item to hold a count of collisions and separations.

private int _canBePlacedCounter = 0;

On the event handler methods, increase of decrease the member count. In the code below there are extra conditions as for myself, I only want to perform these operations when an item is being placed into the "world".

    private bool Body_OnCollision(Fixture fixturea, Fixture fixtureb, Contact contact)
    {
        if(this.IsBeingPlaced)
        {
            _canBePlacedCounter++;
        }

        return true;
    }

    private void Body_OnSeperation(Fixture fixturea, Fixture fixtureb)
    {
        if (this.IsBeingPlaced)
        {
            _canBePlacedCounter--;
        }
    }

We then can setup a simple public property (this really should be a method if we want to get into coding standards but this is not the time or place)

    public bool CanBeDropped
    {
        get
        {
            if (_canBePlacedCounter == 0) return true;
            else return false;
        }
    }

The reason for this implementation is, I originally had a bool which I set to true or false whenever I got one of the events. The problem is... If your item collides with item A, then collides with item B then leaves item A you get a reading of not colliding. So... using a counter like this we can count in and count out all of the collisions and separations. Belive me this works like an absolute charm.

Hope it is of use to someone out there.

UPDATE: I have found that this only works well with basic rectangles. If you get into bodies with many polygons such as those automatically generated from images its very unreliable as for whatever reason farseer regularly raises less separation events than it does collision events meaning the counter often does not return to 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文