Difference between OnCollision and OnEnter?
by Faisal Zahid · in Torque X 3D · 07/07/2009 (2:25 am) · 4 replies
T3DTriggerComponent class has two ways of detecting collisions OnEnter and OnCollision delegates. What is the major difference between both.
OnEnter works fine , but i cant some how make the OnCollision delegate to work up till now.
OnEnter works fine , but i cant some how make the OnCollision delegate to work up till now.
About the author
#2
While trying john`s code samples on triggers and pickup items. The examples work for onEnter() delegate but fail to detect collision for onCollision.
Eg
Any idea what am i doing wrong ?
07/09/2009 (7:40 am)
Ok thanks for the reply.While trying john`s code samples on triggers and pickup items. The examples work for onEnter() delegate but fail to detect collision for onCollision.
Eg
public void CreatePowerup()
{
//find the physics resolver
RigidCollisionManager rigidManager = TorqueObjectDatabase.
Instance.FindObject<RigidCollisionManager>("RigidManager");
//get the Player object type
TorqueObjectType typePlayer = TorqueObjectDatabase.Instance.GetObjectType("Player");
// TorqueObjectType typeProjectile = TorqueObjectDatabase.Instance.GetObjectType("Projectile");
TorqueObject objPowerup = new TorqueObject();
objPowerup.Name = "Powerup";
//add the scene component
T3DSceneComponent componentScene = new T3DSceneComponent();
componentScene.SceneGroup = "Powerup";
componentScene.Position = new Vector3(300.254f, -180.434f, 245f);
objPowerup.Components.AddComponent(componentScene);
T3DTSRenderComponent componentRender = new T3DTSRenderComponent();
componentRender.ShapeName = @"data\shapes\items\Crates\MetalCrate.dts";
componentRender.SceneGroupName = "Powerup";
objPowerup.Components.AddComponent(componentRender);
//create the trigger collision box
CollisionBoxShape boxCollision = new CollisionBoxShape();
boxCollision.Size = componentRender.ObjectBox.Max;
//add the trigger component
T3DTriggerComponent componentTrigger = new T3DTriggerComponent();
componentTrigger.SceneGroupName = "Powerup";
componentTrigger.CollisionBody.AddCollisionShape(boxCollision);
componentTrigger.CollidesWith = typePlayer;
componentTrigger.OnCollision = ProcessCollision;
componentTrigger.OnEnter = ProcessEnter;
componentTrigger.RigidManager = rigidManager;
componentTrigger.Immovable = true;
objPowerup.Components.AddComponent(componentTrigger);
TorqueObjectDatabase.Instance.Register(objPowerup);
}Any idea what am i doing wrong ?
#3
I have never gotten that property to work correctly. Try taking that out and see if helps.
Also can you post the ProcessCollision function code, and is the example of john's you are following in the book or online?
Alan
Senior Software Engineer
Perfect Dork Studios
07/09/2009 (8:17 am)
I am not in front of my computer with a compile but one thing that mite be an issue is this: componentTrigger.CollidesWith = typePlayer; I have never gotten that property to work correctly. Try taking that out and see if helps.
Also can you post the ProcessCollision function code, and is the example of john's you are following in the book or online?
Alan
Senior Software Engineer
Perfect Dork Studios
#4
So here is the complete code
07/09/2009 (9:45 pm)
No success even without componentTrigger.CollidesWith = typePlayer;So here is the complete code
public static void ProcessCollision(T3DRigidComponent us, T3DRigidComponent them,
RigidContactConstraint.Contact[] contacts, int contactCount, float dt)
{
//get the current health level
float currentHealth;
PlayerManager.Instance.GetPlayer(0).GetData("HealthLevel", out currentHealth);
//increase and store the new health level
currentHealth -= 15;
PlayerManager.Instance.GetPlayer(0).SetData("HealthLevel", currentHealth);
//remove the power-up shape
them.Owner.MarkForDelete = true;
}
// For Pickup
public static void ProcessEnter(T3DTriggerComponent trigger, T3DRigidComponent obj)
{
//get the current health level
float currentHealth;
PlayerManager.Instance.GetPlayer(0).GetData("HealthLevel", out currentHealth);
//increase and store the new health level
currentHealth -= 15;
PlayerManager.Instance.GetPlayer(0).SetData("HealthLevel", currentHealth);
//remove the power-up shape
// obj.Owner.MarkForDelete = true;
String name;
// name = obj.Owner.Name;
name = trigger.Owner.Name;
// trigger.Owner.Components.FindComponent<WeaponComponent3D>().FireWeapon();
TorqueObject pickup = TorqueObjectDatabase.Instance.FindObject<TorqueObject>("Powerup");
pickup.MarkForDelete = true;
}
Torque Owner Alan Uthoff
The difference is OnCollision is repeatedly called as long as something remains inside that trigger space and it is part of T3DRigidComponent to determine if any other collisions shapes have collided with you. OnEnter is call only once when on object collide for the first time with you triggers collision shape. The trigger class has its own update function OnStay that you should use.
Alan
Senior Software Engineer
Perfect Dork Studios