Checking for collision between two scene objects in the code.
by John Bura · in Torque X 2D · 10/14/2010 (5:06 pm) · 10 replies
So in a delegate for a powerup I would like to check which player has collided with the power up. I can't seem to find anything in the documentaion about collideswith. Am I even using the right code?
T2DSceneObject player1 = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("playa");
T2DSceneObject powerup = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("powerup");
//What do I put here?
if(player1.Collision.CollidesWith ???
Game.Instance.WeaponsInventoryPlayer1 = 6;Thanks again :)
#2
10/15/2010 (3:21 am)
The problem I am having is that I want 1 powerup to power up both players. But you can only put one delegate in the collision box. I need to be able to put some code into an if statement. After that it should be no problem. I did take a look at the blaster tutorial, but I couldn't find anything to help my current predicament out.
#3
10/15/2010 (6:02 am)
Getting your players should be as easy as finding them in the torqueobject data base, or using the get player method of your game.
#4
10/15/2010 (3:06 pm)
Im so close. I can't figure out how to check if there is a collision within the code. I know this isn't the right way but I am close :)ourObject = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("PowerUp");
theirObject = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("playa");
if (ourObject.Collision == theirObject.Collision)
Game.Instance.WeaponsInventoryPlayer1 = 6;
#5
it sounds weird, but the power up should be colliding with your player. This will stop you from having to worry about the player having to check collision against every thing in the engine. IE: If bullet do this, if ground do this, if wall do this, if bad guy do this.
Just do stuff if the Powerup collides with the player...
In other words, put a delegate on the power-up (go to the blaster tutorial, and get one of those bullet delegates, and use that).
Then inside the power up delegate's body, put this code...
I think your confusion came from trying to make the player collide with a power up, (which is how we would think of it normally, but not how the code should work). Just make the power up collide with the player, and as long as its not colliding with other stuff too, you should be out of "confusion territory".
10/15/2010 (5:00 pm)
Ok its a power up right?it sounds weird, but the power up should be colliding with your player. This will stop you from having to worry about the player having to check collision against every thing in the engine. IE: If bullet do this, if ground do this, if wall do this, if bad guy do this.
Just do stuff if the Powerup collides with the player...
In other words, put a delegate on the power-up (go to the blaster tutorial, and get one of those bullet delegates, and use that).
Then inside the power up delegate's body, put this code...
Game.Instance.WeaponsInventoryPlayer1 = 6; Game.Instance.WeaponsInventoryPlayer2 = 6; //if in fact you are still trying to do both players with one power up.
I think your confusion came from trying to make the player collide with a power up, (which is how we would think of it normally, but not how the code should work). Just make the power up collide with the player, and as long as its not colliding with other stuff too, you should be out of "confusion territory".
#6
The problem I am having is that I want 1 powerup to power up both players.
You need to check inside the delegate which player you are colliding with... here you go..
10/15/2010 (5:06 pm)
Ok wait... duh... The problem I am having is that I want 1 powerup to power up both players.
You need to check inside the delegate which player you are colliding with... here you go..
if (theirObject.Name == "playa")
Game.Instance.WeaponsInventoryPlayer1 = 6;
else if (theirObject.Name == "playa2")
Game.Instance.WeaponsInventoryPlayer2 = 6;
#7
If it is done this way then if player 1 hits the power up, player 1 + player 2 gets the same inventory. Here is my pseudo code
10/15/2010 (5:07 pm)
What I need to do is differentiate which player collides with the powerup.If it is done this way then if player 1 hits the power up, player 1 + player 2 gets the same inventory. Here is my pseudo code
if ( player 1 collides with power up) Game.Instance.WeaponsInventoryPlayer1 = 6; if ( player 2 collides with power up) Game.Instance.WeaponsInventoryPlayer1 = 6;
#8
I knew it was something simple :)
I actually really appreciate this Thanks will :-). Traditionally I have set up my own collision boxes.
10/15/2010 (5:13 pm)
Haha, Maybe I should have paid more attention in English class. What I should have wrote was. I need The problem I am having is that I want 1 powerup to power up both players INDEPENDENTLY.I knew it was something simple :)
I actually really appreciate this Thanks will :-). Traditionally I have set up my own collision boxes.
#9
No problem!
10/15/2010 (5:21 pm)
Ok.. then post #6 is better... but I still don't see how you are going to get around using a delegate. Just put the #6 code inside the delegate for the power up, you should be fine.No problem!
#10
10/15/2010 (5:22 pm)
It worked just fine. :)
Torque 3D Owner Will O-Reagan
Modern Intrigues
That collide's with property you are looking at is something different, used by the collision detection engine. Just use the blaster tutorial to create a collision delegate, that will help you alot.