CollisionGroups
by Jacob Vann · in Torque Game Builder · 09/22/2005 (10:06 pm) · 3 replies
I'm having problems understanding collision groups. I apologize for the very large post.
Now I have 3 object types that will be colliding: the player, a monster, and the collidable tiles.
I want all of my collisions to be handled by the player. So the playerSprite should "send" all of the collisions, and everyone else should "receive" them. However, monsters and other sprites still need to collide with walls, so they will have to send as well as receive.
According to the reference, the setCollisionMasks() sets the collision mask for "sent" collisions.
So by this, I would take it to understand that onCollision would only be called if a sprite sends a collision to a sprite or tile layer in the group(s) that are in the *sending* sprite's collision mask.
So I did, basically (just pseudo-code):
The collisions work between player and tiles, and monster and tiles. But for monster and player, things act up. It's apparent that onCollision() is being called in cases where srcObj (the sender) is the monster, and dstObj (the receiver) is the player. Based on the above, this should not be the case.
I can send a more complete example if it's really necessary. I just want to confirm that my asertion about collision masks is correct, and that in my case the player should not receive collisions from the monster.
Thanks in advance, I'm having a blast in T2D!
-Jacob
Now I have 3 object types that will be colliding: the player, a monster, and the collidable tiles.
I want all of my collisions to be handled by the player. So the playerSprite should "send" all of the collisions, and everyone else should "receive" them. However, monsters and other sprites still need to collide with walls, so they will have to send as well as receive.
According to the reference, the setCollisionMasks() sets the collision mask for "sent" collisions.
So by this, I would take it to understand that onCollision would only be called if a sprite sends a collision to a sprite or tile layer in the group(s) that are in the *sending* sprite's collision mask.
So I did, basically (just pseudo-code):
$player.setCollisionActive(true, false); // send only $player.setCollisionMasks($tilelayergroup|$monstergroup, $tilelayerlayer,$monsterlayer); // send collisions to tiles and monster $monster.setCollisionActive(true, true); // send and receieve $monster.setCollisionMasks($tilelayergroup, $tilelayerlayer); // only send to tile layer $tilelayer.setCollisionActive(false, true); // receive only $tilelayer.setCollisionMasks(0, 0); // collision masks mean nothing if the layer isn't sending collisions
The collisions work between player and tiles, and monster and tiles. But for monster and player, things act up. It's apparent that onCollision() is being called in cases where srcObj (the sender) is the monster, and dstObj (the receiver) is the player. Based on the above, this should not be the case.
I can send a more complete example if it's really necessary. I just want to confirm that my asertion about collision masks is correct, and that in my case the player should not receive collisions from the monster.
Thanks in advance, I'm having a blast in T2D!
-Jacob
#2
But that's basically what I do have. I have my groups and layers set up properly, just didn't show that part of the code. My bad. :)
Either way, I guess my real question is does setCollisionMask() work on both sent and received collisions, or only sent collisions (as the guide implies)? If it's expected that the monster will process both collisions sent by the player and received by the tiles, then I'll just have to put more checks in my collision functions...
Could be that I'm just not understanding what Sent collisions are versus Receieved collisions.
EDIT:
Okay, I was able to circumvent this problem for now by putting a "if ((%dstObj.getGroup) != $playerGroup)" around the monster's collision function (I'm still not understanding why I would have to, assuming I have everything correct), but I ran into another interesting problem.
It seems that whenever the monster collides with the player while the player is standing on the ground (basically "floating" just above the ground, not colliding with it), it re-casts the collision with the ground tiles, even though the player is not colliding with them in any other frame. This causes the player to get stuck and not be able to jump or walk while the player and monster are colliding. Not the intended effect.
I read this thread which seems to point to a case where a collision can be called multiple times in a frame. However, their suggestions are to temporarily disable collisions for one colliding party until the next frame. My question is, what if the player is falling and collides with a monster the same frame that he collides with the ground? If the monster collision is handled first and I turn off collisions for the player, then the player won't collide with the ground tiles and will essentially fall through them?
... back to the drawing board I go. I'm just not understanding how collisions work completely...
-Jacob
09/23/2005 (9:14 pm)
Thanks Danny -But that's basically what I do have. I have my groups and layers set up properly, just didn't show that part of the code. My bad. :)
Either way, I guess my real question is does setCollisionMask() work on both sent and received collisions, or only sent collisions (as the guide implies)? If it's expected that the monster will process both collisions sent by the player and received by the tiles, then I'll just have to put more checks in my collision functions...
Could be that I'm just not understanding what Sent collisions are versus Receieved collisions.
EDIT:
Okay, I was able to circumvent this problem for now by putting a "if ((%dstObj.getGroup) != $playerGroup)" around the monster's collision function (I'm still not understanding why I would have to, assuming I have everything correct), but I ran into another interesting problem.
It seems that whenever the monster collides with the player while the player is standing on the ground (basically "floating" just above the ground, not colliding with it), it re-casts the collision with the ground tiles, even though the player is not colliding with them in any other frame. This causes the player to get stuck and not be able to jump or walk while the player and monster are colliding. Not the intended effect.
I read this thread which seems to point to a case where a collision can be called multiple times in a frame. However, their suggestions are to temporarily disable collisions for one colliding party until the next frame. My question is, what if the player is falling and collides with a monster the same frame that he collides with the ground? If the monster collision is handled first and I turn off collisions for the player, then the player won't collide with the ground tiles and will essentially fall through them?
... back to the drawing board I go. I'm just not understanding how collisions work completely...
-Jacob
#3
I finally narrowed my problem down to a function I have called isOnGround(), that I use a lot. This function does a CastCollision() to determine if a player is directly above the ground. When I added the monster, the CastCollision was getting false-positives from collisions with the monster.
A quick workaround I did (which caused my above problem) was to just say if (%collisionobj.getGroup() != $collidetilegroup) return false;
This made it so that while the player was colliding with the monster, he was never on the ground.
What I did to fix it was to save the old collision mask, set a new collision mask that only included the tile layer, then do CastCollision and finally set the collision mask back to the original.
It works!
... are there plans for "beefing up" CastCollision to return multiple objects, etc? It could be VERY useful later on.
Thanks again everyone!
10/01/2005 (7:06 pm)
Of course this was a problem with my code. Sorta.I finally narrowed my problem down to a function I have called isOnGround(), that I use a lot. This function does a CastCollision() to determine if a player is directly above the ground. When I added the monster, the CastCollision was getting false-positives from collisions with the monster.
A quick workaround I did (which caused my above problem) was to just say if (%collisionobj.getGroup() != $collidetilegroup) return false;
This made it so that while the player was colliding with the monster, he was never on the ground.
What I did to fix it was to save the old collision mask, set a new collision mask that only included the tile layer, then do CastCollision and finally set the collision mask back to the original.
It works!
... are there plans for "beefing up" CastCollision to return multiple objects, etc? It could be VERY useful later on.
Thanks again everyone!
Torque Owner Danny Koo