Page 1 of 1

Next, a question about collisions

Posted: Mon Feb 17, 2014 1:08 am
by Imerion
Next problem I have encountered is about collisions. I want to have an object do different things depending on which object it collides with.
For example, if the player hits an enemy, I want the player to be removed, but if it hits a score item I want to increase a score variable. Problem is, whichever happens, the game now registers a collision and both triggers. I can't move the collision code to the enemy or score models, since then the problem would just be moved one step. (Since player still has collided with something.) It's a bit hard to explain, but I hope I make sense. :)

Also, can collisions be local somehow. For example, set for each model under Definitions? Or do they have to be set at OnLoaded? I tried the former, but it didn't seem to work.

Any idea on how to go about this?

Posted: Mon Feb 17, 2014 1:55 am
by Kjell
Hi Imerion,

It's pretty straight-forward actually ( once you get the hang of it ). Start by setting the Category property of each Model to a unique value. So for instance, set the Player Model to Category 1, Enemy to 2 and Item to 3. Then in App.Onloaded you add two DefineCollision components .. one enabling collision between Category 1 and 2, and the other between 1 and 3. Then in your Player.OnCollision event you can use something like this ..

Code: Select all

switch(CurrentModel.CollidedWith.Category)
{
  case 2: // Enemy
    PlayerEnergy--;  // Your enemy collision code goes here
    break;

  case 3: // Item
    PlayerScore++; // Your item collision code goes here
    break;
}
The CollidedWith property is basically a reference to the Model or Clone that triggered the OnCollision event .. so you could alternatively check the ClassId property, or even something you've defined yourself.

Hope this helps :wink:

K

Posted: Mon Feb 17, 2014 7:16 pm
by Imerion
Ah, you can use it like that! Smart! That solves my problem, thanks for the help!