Game Development Community

How do i make an Object collide with some objects and not with others?

by Somesh Nanda · in Technical Issues · 04/03/2013 (1:16 am) · 1 replies

Hi all,
I'm using TGB .I know, I can turn off certain collision layers, but is there some other way? im checking in the onCollision() callback of the source object (that is constantly traveling forward) whether the destination object is "monkey". If so , i want it to just pass through it without any effect. But otherwise bounce around. Can it be done through script?

#1
04/03/2013 (11:52 am)
Are you using a licensed version of Torque Game Builder or are you using the open-source Torque2D MIT?

If your answer is the latter, simply place your source object in a specific SceneGroup or SceneLayer and tell it speficially which objects it can collide with.

To specify an object's SceneGroup or SceneLayer :
%sourceobject.SceneLayer = 1; //places the object on scene layer 1
%sourceobject.SceneGroup = 1; //adds the object to scenegroup 1.

Take note that the scenegroup won't affect rendering (unless you want it to) but the SceneLayer will draw objects in front of all other layers.

SceneLayers are ordered from 0 (closest to you) to 31 (farthest from you). Objects in layer 0 will be drawn over objects in layer 1, which in turn will be drawn in front of objects in layer 2, etc.

To specify what you object can collide against :

%sourceobject.setCollisionMasks("1 2 3 4", "0 1");

In the above example, your %sourceobject would only collide with objects in SceneGroups 1, 2, 3 and 4.

The second parameter "0 1" is optional and would make your %sourceobject collide only with objects on SceneLayer 0 and 1.

Combining both uses as in the example would mean that your sceneobject would only collide with objects located on SceneLayer 0 and 1, belonging to SceneGroups 1, 2, 3 and 4.