Layers of bodies in scene

General discussion about the R.U.B.E editor
Post Reply
dagondev
Posts: 6
Joined: Sat Jul 12, 2014 8:01 pm

Layers of bodies in scene

Post by dagondev »

I am wondering how make layers work in my scene in a simplest way, before iforce2d deliver "Display layers" feature.

My idea is basically:
1. All of bodies belonging to the specific layer name with layerX_* prefix. Preferably write a script that can name/rename/change layer to all selected bodies with provided prefix.
2. Write scripts hooked to the 0-9 keys on keyboard, that can show specific layer (ex. layer1_* for 1 key) and hide rest of the bodies from layers (bodies without layerX_ prefix would be immune to this script).
3. Write a script that color bodies by specific layer name
4. Write a script that set collision filtering between layers so layers doesn`t collide with themselves and bodies without layer colliding only with layer1.

That should (in theory) make working with layers much simpler. What I need to do to write some scripts.
What do you think about this? Do you have your own system to work with layers? What do you do differently?
Do you have some scripts to share?

P.S. Can I make some scripts run automatic? It would be very helpful for script 3.
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Layers of bodies in scene

Post by iforce2d »

It's a bit hard to make suggestions without knowing what you want these layers for. The "Display layers" I'm thinking of implementing would be mainly, as the name suggests, about display. That is, you would be able to show and hide things depending on what layer they are in, so that it's easier to see and pick them with the mouse. They would also affect the renderOrder of images, so that images would be sorted ascending within each layer.

Ideally I'm thinking that a layer will be an item type that can be given custom properties, so that users could attach extra information to them to use in their game (eg. parallax depth etc).

However, it sounds like you are thinking of layers as sets of fixtures that should not collide with each other. I'll go over each point you made with some comments:

1. Rather than pollute the names of your bodies, you might be better using a custom property to say which layer a body belongs to. It could be a string or int I guess, depending on what suits you better. Then you can change that custom property independently of the name.

2. Yes, good idea. However, it's not possible to show or hide anything right now. What I usually do is move everything say, 10 units upward, to get it out of the way. So far I have just used the Ctrl key movement to get an exact position, but if I was doing it a lot I would probably set up some scripts to make it easier. In v1.6 there was a new set of script functions like "body[] getBodiesByCustomString" (and same for all other item types and custom property types) which would let you get all the bodies in a layer. For example, if you used custom properties as mentioned in 1. above, you could have a script like this mapped to the 0-9 keys.

Code: Select all

float x = 0;
float y = isCtrlKeyDown() ? -10 : 10;
translate( getBodiesByCustomString("layer","one"), x, y );
3. Bodies cannot be colored (remember a body does not have any visible substance, only fixtures and images do). You can set the color tint of images, which would let you set their alpha to zero, but the fixtures will still be visible so I don't think that would help much.

4. To prevent all bodies in a layer (well, fixtures really) from colliding with each other you could set their group index to the same negative number. Unfortunately the collision filter category and mask bits are not very helpful for separating things into layers. You might have to use the SetContactFilter of the world in your program to get more detailed behavior.

Apart from the 0-9 keypresses, there are a few other events you can use as script hooks. You can see these in the script hooks setup dialog (mouse over for tool-tip or search in the built-in help for more info).
scriptHookEvents.png
scriptHookEvents.png (6.07 KiB) Viewed 9571 times
dagondev
Posts: 6
Joined: Sat Jul 12, 2014 8:01 pm

Re: Layers of bodies in scene

Post by dagondev »

Your idea of "Display layers" feature sounds good.

I have in mind mostly same idea as your feature..
I thought that collision between bodies in layers is something that should be considered, especially because I want to use those layers in my game (parallax depth).

1. Oh, great idea. I didn`t look at custom properties tutorial before. I will use those.

2. Kind of hacky with moving up by x units, but it should work.

3. Yeah, you are right. I don`t use images, fixtures are only representation of bodies. I will try with changing alpha color of these.

4. Sure, no problem.

Thanks!
Post Reply