Box2d help - setFilterData [SOLVED]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Jeeper
Party member
Posts: 543
Joined: Tue Mar 12, 2013 7:11 pm

Box2d help - setFilterData [SOLVED]

Post by Jeeper » Thu Jul 31, 2014 10:09 pm

http://www.love2d.org/wiki/Fixture:setFilterData

I am having some trouble understanding how to use the filterData properly. You have to set 3 arguments, but I am having a hard time understanding the difference between them. What I want to do is this:

I have a player and a monster. Both of them will be shooting projectiles.
Player should not collide with monster.
Player and monster should not collide with own bullet.
Players bullet should collide with monster and vice versa.

Anyone who can shed some light on this?
Last edited by Jeeper on Wed Aug 06, 2014 10:59 pm, edited 2 times in total.

User avatar
Jeeper
Party member
Posts: 543
Joined: Tue Mar 12, 2013 7:11 pm

Re: Box2d help - setFilterData

Post by Jeeper » Mon Aug 04, 2014 4:31 pm

Still looking for help :)

Edit; have made some progress together with a fellow coder. Will post the solution and a better explanation in the following days.

User avatar
Jeeper
Party member
Posts: 543
Joined: Tue Mar 12, 2013 7:11 pm

Re: Box2d help - setFilterData

Post by Jeeper » Wed Aug 06, 2014 10:59 pm

Ok time to post the solution. I don't know if anyone actually cares (judging by the 0 responses), but hopefully at least someone will find it useful at some point. Either way this will allow me to come back and help me remember how it works if I would ever need it in the future. It is very useful if you are going to make a game where not all objects will collide with eachother, such as the player not colliding with his own bullets (now you can spawn them inside the player). Or for making ghosts that can go through walls.

So we have 3 arguments in Fixture:setFilterData( categories, mask, group )

Group
The first thing that is checked is the group. This can be any number between -32768 to 32767. If two objects collide and both of them are in the same group, one of two thing will happen; If the group is negative then there will never be no collision. If the group is positive then they will always collide. If they do not match or the group number is 0, then it will move on and check the categories and mask.

Categories and mask
Each object can be assigned multiple categories out of the pool of 16 available categories. To understand this, you need to understand binary code, check below.

In the mask, you let the object know what categories it will collide with. Both of the objects have to "agree" to collide in order to collide. Lets say object A is in group 1 and 2, and collides with (mask) group 2. Object B then must be in group 2 and must have a mask of either 1 or 2.

Binary
Imagine 16 "0" in a row like this: 0000 0000 0000 0000. Each of them are representing a boolean of "true" or "false". 0 meaning false and 1 meaning true. The first 0 represents the number 1, each following 0 will be double of the following value. It goes like this: 1,2,4,8 16,32,64,128 and so on. Lets say that you want the object to belong to the first category, then you would simply write "1". In binary that would represent "0000 0000 0000 0001"

Lets say that we want it to belong in the third category, then you would write "4". In binary that would represent "0000 0000 0000 0100"

Now for the cool part, if you want it to belong to multiple categories, lets say the first and the third (like in previous examples) then you would write the combined value of "1" and "4", in other words "5". In binary that would represent "0000 0000 0000 0101"

Both the mask and the category follows the same method.
Last edited by Jeeper on Thu Aug 07, 2014 10:05 am, edited 1 time in total.

User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Box2d help - setFilterData [SOLVED]

Post by Ranguna259 » Wed Aug 06, 2014 11:22 pm

Wow.. Totaly missed this, I was unsure of how to do this too, thanks for sharing your solution :)
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.

Post Reply

Who is online

Users browsing this forum: Exabot [Bot] and 8 guests