noob here: collision categories question

Topics: User Forum
Feb 16, 2009 at 4:38 PM
I've just started working on a game in the genre of fantastic contraption.  If you've noticed on that game when you draw a stick connected to a stick which is connected to a third stick, in a chain... the first and third sticks collide with each other, but the middle stick does not collide with either of the sticks.  I don't yet see a simple way to do this with collision categories or groups.

In our case, where a body is connected by a joint to another body those two should not collide with each other, but they should collide with everything that's not directly connected to them with joints.  I can do this using collision categories, but it seems there are a limited number of categories, so what would be the best work around to allow an infinite number of connected sticks?

Using collision groups... if I gave each stick a different number is there a way to just disable collisions with bodies to which they're are connected?


Thanks for helpin' out a noob
Coordinator
Feb 16, 2009 at 7:23 PM
There is indeed a way of doing that.

You can extend the build-in collisiongroup support by subscribing to the OnCollision event and check with your own logic (code) there.
If you have a list of connected bodies or a property on the bodies to check on, you would do something like this.


Foreach (Stick stick in Sticks)
{
    stick.Geom.OnCollision += OnCollision;
    stick.Tag = stick;
}
private bool OnCollision(Geom geomA, Geom geomB, ContactList contactList)
{
    //If the two sticks colliding is both connected to a chain, cancel the collision.
    if (((Stick)geomA.Tag).IsConnectedToStick && ((Stick)geomB.Tag).IsConnectedToStick)
    {
         return false
    }

//Make the collision happen if the above condition is not met.
return true;
}

Or something like that...
Feb 16, 2009 at 7:39 PM
That looks like just what I'm looking for.
Thanks
Feb 17, 2009 at 4:07 AM
Edited Feb 17, 2009 at 4:08 AM
Hey brotzge: i'm working on a similar game, with the intention of releasing it to XNA Community games. The way  i did it was to modify the engine so that you can tell the engine to not collide two specific geoms. Check out this patch and use it like this (if you want):
(in the code that connects 2 game objects like a circle or square or stick):
geom1.AddDNC(geom2);
geom2.AddDNC(geom1);
(replace geom1 and 2 with the appropriate variables, and if this isnt the right function search in the Geom.cs file for DNC to find the function if i am wrong)
then they will not collide with each other.
However, this method has a bug: if you try and connect more than one stick to a connection point, one of them might collide with another at least in my game due to my lackluster method of connecting the two together (i am planning to fix that).
Experiment with my code; you never know what it might be useful for!
i'm also wondering if you have the same plans to release yours... if you do then i've got some competition! although i'm planning to make more stuf for the game like bridges and, well, you'll find out more later.
Feb 17, 2009 at 1:57 PM
Hey Rogue,
Thanks for the code
That's great to see some others are working on this type of game.  I wouldn't consider mine competition just yet as I'm doing it more for the programming experience than to actually release a usable product (I'm going really slow! but learning lots). Also I think my game will have a bit of a different spin on it from FC. I'm betting we see quite a few of these type games come out in the next year or so... at least I hope so, I'm still addicted to FC.
Feb 18, 2009 at 1:55 AM
You're welcome. This is a pretty good learning experience so far (i'm currently trying to implement a zooming and moving camera without having to resort to a real "3D" one) and i'll try and help with any problems you have. I don't think that there will be many FC clones popping up but on the other hand there appear to be some experienced programmers working on community games and a couple might decide to release theirs.