0
\$\begingroup\$

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!

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .