Simple Collision Example
by Dave DAmico · in Torque X 2D · 09/03/2008 (11:50 am) · 7 replies
Does anyone have a simple example on how to do collision. I've looked at the FPS demo but have had no luck getting the results I want. I have two objects in my scene: 1 desk and 1 camera. All I want to do is not allow the camera to move through the desk.
Here is the desk object:
Here is the camera:
Here is the desk object:
<TorqueObject type="GarageGames.Torque.Core.TorqueObject" name="TrainingRoomDesk">
<Components>
<T3DSceneComponent type="GarageGames.Torque.T3D.T3DSceneComponent" name="roomSceneComponent">
<UseOwnerObjectType>true</UseOwnerObjectType>
<Visible>true</Visible>
<VisibilityLevel>1</VisibilityLevel>
<Position>
<X>-15.45307</X>
<Y>200</Y>
<Z>373.1856</Z>
</Position>
<Rotation>
<X>0.7071</X>
<Y>0</Y>
<Z>0</Z>
<W>0.7071</W>
</Rotation>
<Scale>
<X>1</X>
<Y>1</Y>
<Z>1</Z>
</Scale>
<Name>roomSceneComponent</Name>
</T3DSceneComponent>
<T3DXNARenderComponent type="GarageGames.Torque.T3D.T3DXNARenderComponent" name="compRender">
<Filename>data/shape/TrainingRoomDesk</Filename>
<ConvertToTorqueCoords>false</ConvertToTorqueCoords>
<Name>compRender</Name>
</T3DXNARenderComponent>
<T3DRigidComponent type="GarageGames.Torque.T3D.T3DRigidComponent" name="deskRigidComponent">
<GravityScale>9</GravityScale>
<Mass>0</Mass>
<Kinetic>true</Kinetic>
<Immovable>true</Immovable>
<Velocity>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
</Velocity>
<RotationScale>1</RotationScale>
<ResolveCollisions>true</ResolveCollisions>
<AnimateCollisionOffsets>false</AnimateCollisionOffsets>
<RenderCollisionBounds>true</RenderCollisionBounds>
<Name>deskRigidComponent</Name>
</T3DRigidComponent>
</Components>
<Pool>false</Pool>
<PoolWithComponents>false</PoolWithComponents>
<IsTemplate>false</IsTemplate>
<IsPersistent>false</IsPersistent>
<Notched>false</Notched>
</TorqueObject>Here is the camera:
<TorqueObject type="GarageGames.Torque.Core.TorqueObject" name="Camera">
<Components>
<CameraComponent type="GarageGames.Torque.T3D.FreeCameraComponent" name="CameraComponent">
<RigidManager nameRef="RigidManager" />
<FarDistance>1000</FarDistance>
<FOV>1.57</FOV>
</CameraComponent>
<T3DSceneComponent type="GarageGames.Torque.T3D.T3DSceneComponent" name="">
<UseOwnerObjectType>true</UseOwnerObjectType>
<Visible>true</Visible>
<VisibilityLevel>1</VisibilityLevel>
<Position>
<X>48.9337</X>
<Y>-167.0184</Y>
<Z>485.208</Z>
</Position>
<Rotation>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
<W>1</W>
</Rotation>
<Scale>
<X>1</X>
<Y>1</Y>
<Z>1</Z>
</Scale>
</T3DSceneComponent>
</Components>
<Pool>false</Pool>
<PoolWithComponents>false</PoolWithComponents>
<IsTemplate>false</IsTemplate>
<IsPersistent>false</IsPersistent>
<Notched>false</Notched>
</TorqueObject>About the author
#2
09/09/2008 (6:59 am)
Thanks, John. One other question... when defining collision shapes, is the Center property relative to the scene or the object. For instance, if I'm creating box around an object and that object is located at 100,100,100 would the center be 100,100,100 or 0,0,0?
#3
In this case, I'm using a Box collision shape. I believe that the FPSDemo uses a couple of stacked spheres, like a 'snowman' to simulate a more organic collision shape. Since they are stacking the spheres, they need to set the position offset.
John K.
09/10/2008 (12:27 am)
The position is relative to the SceneGroup - so it's usually Vector3.Zero... Here's how I do it...T3DRigidComponent componentPhysics = new T3DRigidComponent(); componentPhysics.SceneGroupName = "PlayerSceneGroup"; componentPhysics.GravityScale = 1.5f; componentPhysics.Mass = 10f; componentPhysics.RigidManager = rigidManager; //RigidManager for the scene componentPhysics.CollisionBody.AddCollisionShape(new CollisionBoxShape()); objPlayer.Components.Add(componentPhysics);
In this case, I'm using a Box collision shape. I believe that the FPSDemo uses a couple of stacked spheres, like a 'snowman' to simulate a more organic collision shape. Since they are stacking the spheres, they need to set the position offset.
John K.
#4
12/22/2008 (11:53 am)
Another follow-up question: I need to make a room, what would be the best way to make the camera collide with the walls? Would each wall need to be a model and have a rigid component?
#5
12/24/2008 (7:42 am)
You can add a CollisionMeshShape to a room model, so that the entire room has a single collision shape, or you can add any number of CollisionBoxShape shapes to the room model's RigidComponent, one for each wall. The second way will probably be more efficient and load much faster.
#6
12/29/2008 (8:11 am)
I see... how about the floor. lets say i didn't want a terrain since every thing is going to be contained in a hall and a few rooms with no windows. could i do the same thing?
#7
12/29/2008 (10:17 am)
I tested out using the bounding box on for the floor. It seems to work, however, I cannot control the player. I'm assuming because the player is not considered "on the ground". Is this a correct assumption? If so, how can I make the floor act as the ground?
Associate John Kanalakis
EnvyGames
John K.