Collision for Tubes
by Daniel Thornton · in Torque Game Builder · 06/27/2006 (9:36 am) · 2 replies
Thanks for any help everybody.
I'm wanting to have particle effects fall down straight and curved, modular tubing, if that makes sense. The tubing would be in a cut away view, so I would need the effect of collision on the left and right side for each piece.
The few ways I've thought how to go about it are
-Look into multiple collision poly's for sprites/tiles
-Paths (I'm looking for a certain amount of input control for the speed and the possibility to detach from and attatch to the path at times, so paths might cause more problems)
-Look into a sort of "inverse" collision box for each piece where when inside it controls movement. (is that just a trigger?)
-Suck it up and split my pieces into two sides (adding a lot more work both in managing art resources, and in the level builder)
Does anybody know from experience what I should do or avoid in this?
Thanks again
I'm wanting to have particle effects fall down straight and curved, modular tubing, if that makes sense. The tubing would be in a cut away view, so I would need the effect of collision on the left and right side for each piece.
The few ways I've thought how to go about it are
-Look into multiple collision poly's for sprites/tiles
-Paths (I'm looking for a certain amount of input control for the speed and the possibility to detach from and attatch to the path at times, so paths might cause more problems)
-Look into a sort of "inverse" collision box for each piece where when inside it controls movement. (is that just a trigger?)
-Suck it up and split my pieces into two sides (adding a lot more work both in managing art resources, and in the level builder)
Does anybody know from experience what I should do or avoid in this?
Thanks again
About the author
Torque Owner Thomas Buscaglia
One way to get this done is to create a datablock for your collision sceneobjects so they all spawn with the same collision properties, then create different classes for corner and straight pieces of tube. Heres a quick example off the top of my head.
Your collision object datablock could look something like this:
datablock t2dSceneObjectDatablock(collisionObjectDatablock) { CollisionActiveSend = "0"; CollisionActiveReceive = "1"; CollisionPhysicsSend = "0"; CollisionPhysicsReceive = "1"; CollisionDetectionMode = "POLYGON"; CollisionResponseMode = "CLAMP"; CollisionMaxIterations = "4"; CollisionPolyList = "-1 -1 1 -1 1 1 -1 1"; Immovable = "1"; };And a straight tube class might have something like this in its onLevelLoaded callback (note that this code assumes that your pipe image is vertical by default, though it can be rotated in the level):
function StraightTube::onLevelLoaded(%this, %scenegraph) { ... %this.setupColl(); ... } function StraightTube::setupColl(%this) { %thisHeight = %this.getHeight(); %this.leftColl = new t2dSceneObject() { config = "collisionObjectDatablock"; size = "0.5" SPC %thisHeight; } %this.rightColl = new t2dSceneObject() { config = "collisionObjectDatablock"; size = "0.5" SPC %thisHeight; } %mountOffset = %this.getWidth() / 2; %this.leftColl.mount(%this, -%mountOffset SPC 0, 0, true, true, true, true); %this.rightColl.mount(%this, %mountOffset SPC 0, 0, true, true, true, true); }Now you should be able to even rotate/move your tubes and collision should still work. a corner piece would be exactly the same, except the size and mount point would be different for one of the collision objects, obviously.
There are a few things to note about this implementation:
The collision objects are just blank scene objects with collision set. The collision poly is set to all four corners, so it will be based on the actual size of the collision object, making it easy for us to change it. Setting immovable to true might be unneccesary because the object is going to be rigid mounted, but what the heck.
The setup of the collision will happen once for each StraightTube object and set up a right and left collision object with a width of 0.5 at an offset of the half the tube's width on either side. what this means is that in this case there is a 0.25 overlap of the collision object on either side of the side borders, so the artwork and level design should compensate for this. You could also just as easily subtract 0.25 from %mountOffset and make a 0.5 width wall in the tube artwork. Either way, it's important that your artwork reflects the actual collision and vice-versa.
A nice feature of this is that because we make the collision objects dynamic fields on the StraightTube objects we can optionally turn collision on and off if we feel like it. For specific collision event functionality you could also set a class in the datablock and handle the onCollision callback for that class. You could also set the collision layers/groups in the datablock to specify what you do/don't want to collide with your tube walls.
Note that I haven't actually run this script, I just typed it up so there may be a couple errors. *_*
For more info on mounting check out the TGB reference docs.
I hope this was helpful.
Edit: Broken link.. oops!
Edit 2: Changed the script a little.