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: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Box2d help - setFilterData [SOLVED]

Post by Jeeper »

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: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: Box2d help - setFilterData

Post by Jeeper »

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: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: Box2d help - setFilterData

Post by Jeeper »

Ok time to post the solution. 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 category 1 and 2, and collides with (mask) group 2. Object B then must be in category 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 Mar 03, 2022 10:08 pm, edited 3 times 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 »

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.
agaetis
Prole
Posts: 3
Joined: Sun Jan 07, 2018 9:12 pm

Re: Box2d help - setFilterData [SOLVED]

Post by agaetis »

Thanks too for sharing this.
Both objects have too agree to collide, I will remember this now :)
Post Reply

Who is online

Users browsing this forum: No registered users and 83 guests