Collision Detection Tutorial

While Box2D handles all the collision detection and resolution of the physics, it would also be useful for us to be able to determine when and what objects collide. So this will be what we will learn today.

In this tutorial we will create a simple Box2D world and populate it with a red and blue ball that drops down from the sky. When each ball collides with the ground we will reposition them at their original location from which they fell.

Once again we will extend MouseJointTutorial to save us from coding from scratch.

To be able to detect when collisions happen requires use of a b2ContactListener. The b2ContactListener can be considered an abstract class (even though AS3 doesn’t actually support them) as we do not instantiate it directly but have to extend it first. It would probably be a good idea to have a look at the b2ContactListener class yourself. You will observe the following functions that we can override.

We will only be overriding BeginContact today since that is all we need for determining when the balls hit the ground. So lets get started.
First, we will create the b2Body balls and textures. You should know this process pretty well by now so I won’t go into lengths to explain it.

Now we can get to the actual collision detection code. Create a class and call it BallContactListener, have it extend b2ContactListener and override the BeginContact function .

In our setup function we will create an instance of this BallContactListener and pass it to our world via the SetContactListener function.

At the moment our BeginContact function in the BallContactListener is empty. We know that it takes in a b2Contact as a parameter and we can take at a guess that it will supply us with the information we need to determine who collided with who.

So lets look what is available. We can see that we can call GetFixtureA and GetFixtureB to return a b2Fixture. From those fixtures we can retrieve a b2Body. The problem is, while we have the b2Bodies that have collided, how do we determine which bodies they correspond with? The answer is at the moment we can’t. Fortunately b2Bodies have two functions that will solve this problem for us, those being GetUserData and SetUserData which allow us to set any data we want as a property. We can leverage this to pass in a string to the body. So in our setup function lets assign the names to the relevant bodies. For best practise I am going to make a class called BodyType and have it contain public static const strings of the names.

We can now go back into your BallContactListener since we have all the data we need. The one remaining question unanswered is which body will be in which fixture, fixture A or B? We can’t really be sure, so we have to test for both cases.

Now that we have the logic in our BeginContact function for determining if a ball collides with the ground we need a way of handling this in our main class. For now we will just use a event dispatcher.

After adding the listeners in the CollisionDetectionTutorial class it would be fair to assume that we could add in the code for repositioning the bodies in them. However, if you try to do that you will see that this does not work the way intended. The problem is when the bContactListener is called, it is happening at some stage during which all the physics calculations are happening and so modifying the Box2D world causes problems. We need to wait until it is safe to update items in the world. A good place that is safe is in the update function. So our listeners instead will set a Boolean flag so that in our update function can check those values and then know what to do.

 

 

原文地址:http://blog.allanbishop.com/box2d-2-1a-tutorial-%E2%80%93-part-4-collision-detection/

 

 

Collision Point


In my last Box2D tutorial, Collision detection , the goal was to be able to detect when a ball made contact with the ground.

In the comments section John asked a good question and that was how could we figure out the specific point of the collision on the ball/ground? While I did give a brief answer I thought I would make this information a little more accessible by creating small demo/tutorial covering it.

If you remember in our last tutorial we created our ballContactListener’s BeginFunction function. This function had a b2Contact listener from which we could determine the two b2Bodies involved in the collision. It is in this function that we can also get list of points describing the point of contact that we are after.

This information is stored in the manifold of the b2Contact. We have to convert the manifold into a world manifold and only then we can retrieve the points as a Vector of b2Vec2.

From my experiments it seems that only the first item in the vector is of use so I just grab it rather than looping over the list. The other thing I noticed was the contact point was not per pixel accurate, but not far off, so bare that in mind. I have included a zip of the demo project if you want to have a look at all the code.

 CollisionPointDemo.zip

 

原文地址:http://blog.allanbishop.com/box-2d-2-1a-tutorial-part-4-extended-contact-point/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值