Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a very basic problem with Box2D.

For a arenatype game where you can throw scriptable "missiles" at other players I decided to use Box2D for the collision detection between the players and the missiles. Players and missiles have their own circular shape with a specific size (varying). But I don´t want to use dynamic bodies because the missiles need to move themselve in any way they want to (defined in the script) and shouldnt be resolved unless the script wants it. The behavior I look for is as following (for each time step):

  • velocity of missiles is set by the specific missile script
  • each missile is moved according to that velocity
  • if a collision accurs now, I want to get the exact position of impact, and now I need a mechanism to decide if the missile should just ignore the collision (for example collision between two fireballs which shouldnt interact) or take it (so they are resolved and dont overlap anymore)

So is there a way in Box2D to create Ghost bodies and listen to collisions from them, then deciding if they should ignore the collision or should take them and resolve their position? I hope I was clear enough and would be happy about any help!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Using a physics engine like Box2D, there is no way to do precisely what you want to do, which is decide at collision time whether the collision should be ignored or whether impulses should be applied. However, you problem is still perfectly solvable.

As far as ensuring certain objects don't collide with each other, what you're looking for are called collision masks (bitmasks), or, as Box2D calls them, filters. That link will bring you to the Box2D manual. Navigate to Chapter 6, Fixtures, and scroll down a page or two to find the Filtering section.

In a nutshell, you specify the category bits for a particular fixture, and then you specify its mask bits. Before Box2D checks for a collision between two objects, it first checks if a collision is allowed by ANDing the category and mask bits of each object together. If the results are non-zero, the objects are allowed to collide. So if you set the category and mask bits of your fireball fixtures to different values, they will not collide with each other.

Now, if the collision masks alone are not enough to do what you want (I'm pretty sure they should be), then Box2D fixtures also have a flag on them indicating whether that fixture is a sensor or not. There is more information available in the manual, but essentially a sensor is a fixture that will cause collisions, but not act as a solid physical structure in the world; other objects will simply pass right through it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.