Checking for collision between two groups
Checking for collision between two groups
Hey Norbo,
I have a simple problem. How do I check if a bounding box of an object from Collisiongroup A is intersecting a bounding box of an object of Collisiongroup B? More specifically, I want to check if the player(which is a capsule) is intersecting a bounding box of an object of a specific collisiongroup or not.
Appreciate your time.
Thanks.
I have a simple problem. How do I check if a bounding box of an object from Collisiongroup A is intersecting a bounding box of an object of Collisiongroup B? More specifically, I want to check if the player(which is a capsule) is intersecting a bounding box of an object of a specific collisiongroup or not.
Appreciate your time.
Thanks.
Re: Checking for collision between two groups
If the collision groups' relationship is such that the objects can create a collision pair, then there are two fairly simple ways:
1) Each frame, loop through the characterBody.CollisionInformation.OverlappingCollidables list. Check the otherCollidable.CollisionRules.Group field to see if it belongs to the desired group.
2) Attach an event handler to the characterBody.CollisionInformation.Events.PairUpdated method. Check if the other object's CollisionRules.Group.
The first option does the logic whereever you want outside of the Space.Update. The second option uses deferred events, like the PairUpdated method, that will execute near the end of the timestep. Depending on what you're using the information for, one approach may be easier than the other.
If the collision groups' relationship is such that there are no pairs, there's a slightly slower option. You can use the Space.BroadPhase.QueryAccelerator.GetEntries(BoundingBox) method to query the character's bounding box. Check all the objects found by the query for their CollisionRules.Group.
1) Each frame, loop through the characterBody.CollisionInformation.OverlappingCollidables list. Check the otherCollidable.CollisionRules.Group field to see if it belongs to the desired group.
2) Attach an event handler to the characterBody.CollisionInformation.Events.PairUpdated method. Check if the other object's CollisionRules.Group.
The first option does the logic whereever you want outside of the Space.Update. The second option uses deferred events, like the PairUpdated method, that will execute near the end of the timestep. Depending on what you're using the information for, one approach may be easier than the other.
If the collision groups' relationship is such that there are no pairs, there's a slightly slower option. You can use the Space.BroadPhase.QueryAccelerator.GetEntries(BoundingBox) method to query the character's bounding box. Check all the objects found by the query for their CollisionRules.Group.
Re: Checking for collision between two groups
Thanks For the reply Norbo.
WhT Im trying to do is this- when the character is crouhing under some obstacle, I want to prevent him from standing up if he's touching it. So Im planning to add all the crouchable stuff to a group and checking for collisision before standing up. Dont know if this is the best way to do it though. Anyway this will also be used to climb ladders and other interactions.
Which option you suggested will be the best for this?
Thanks.
WhT Im trying to do is this- when the character is crouhing under some obstacle, I want to prevent him from standing up if he's touching it. So Im planning to add all the crouchable stuff to a group and checking for collisision before standing up. Dont know if this is the best way to do it though. Anyway this will also be used to climb ladders and other interactions.
Which option you suggested will be the best for this?
Thanks.
Re: Checking for collision between two groups
I would not recommend using CollisionGroups for this purpose. A query that validates the area above the character as open would be sufficient.
The CharacterController does exactly this. It uses a 'query shape' which it tests against objects in an expanded bounding box of the character. If the query results in contacts which meet some conditions, the character must stay crouched because he is blocked.
A simpler, though less robust, approach is to just do a single ray cast upward. If it hits anything that isn't the character, it can't stand up. If it doesn't hit anything, then there's a good chance it's clear- though not always.
If you can, using the CharacterController directly might save you a lot of time.
Adding other behaviors, like ladder climbing, is possible too, though it would require an understanding of the CharacterController's modules and their interaction. For example, if the character's body (or a simplified bounding box surrounding the character) is touching a 'ladder' object, then enter into a 'ladder' control mode.
The CharacterController does exactly this. It uses a 'query shape' which it tests against objects in an expanded bounding box of the character. If the query results in contacts which meet some conditions, the character must stay crouched because he is blocked.
A simpler, though less robust, approach is to just do a single ray cast upward. If it hits anything that isn't the character, it can't stand up. If it doesn't hit anything, then there's a good chance it's clear- though not always.
If you can, using the CharacterController directly might save you a lot of time.
Adding other behaviors, like ladder climbing, is possible too, though it would require an understanding of the CharacterController's modules and their interaction. For example, if the character's body (or a simplified bounding box surrounding the character) is touching a 'ladder' object, then enter into a 'ladder' control mode.
Re: Checking for collision between two groups
I should elaborate a bit more to keep you from unnecessarily going down a path that you might not want.
If you don't need the precision of the character, and could do with bounding boxes alone, then using a simplified approach like #1 in my first post would be fine. Except, instead of testing for collision groups, you perform some simple collision testing between the character's expanded bounding box and the possible blocking bounding boxes.
If you don't need the precision of the character, and could do with bounding boxes alone, then using a simplified approach like #1 in my first post would be fine. Except, instead of testing for collision groups, you perform some simple collision testing between the character's expanded bounding box and the possible blocking bounding boxes.
Re: Checking for collision between two groups
Ah I see. Thanks for the suggestions.
I'll report back what happens.
Thanks.
I'll report back what happens.
Thanks.
Re: Checking for collision between two groups
I'll definitely try your suggestion. But can you point me how to expand the player's bounding box?
And just checking for collision when standing up could bring unintended behaviour like not standing up when next to a wall because of bounding boxs colliding, dont you think? Isnt a group nessesary?
Thanks.
And just checking for collision when standing up could bring unintended behaviour like not standing up when next to a wall because of bounding boxs colliding, dont you think? Isnt a group nessesary?
Thanks.
Re: Checking for collision between two groups
An example can be found in the CharacterController.cs or SphereCharacterController.cs ExpandBoundingBox methods. Those methods are attached to the Space.BoundingBoxUpdater.Finishing event.But can you point me how to expand the player's bounding box?
A boolean test is insufficient, that is correct. But doing a little primitive collision testing will tell you where the AABB's overlap, how much the AABB's overlap, and along which axis. That is sufficient information to decide whether or not the character can stand. If there exist overlaps with a vertical axis at the top of the character, then you immediately know you cannot stand up. Additionally, if there are horizontal axis overlaps of some depth, with the overlap being above the character's main body, then you know it is blocked by some object coming from the side. Such obstacles are different from regular walls in their depth and location.And just checking for collision when standing up could bring unintended behaviour like not standing up when next to a wall because of bounding boxs colliding, dont you think? Isnt a group nessesary?
Re: Checking for collision between two groups
Didn't know that was possible.doing a little primitive collision testing will tell you where the AABB's overlap, how much the AABB's overlap, and along which axis
Can I find example code for such collision testing also in the CharacterController.cs?
Thanks heaps for your help!
Re: Checking for collision between two groups
Nope; the character uses far more complicated general-purpose collision detection routines built into BEPUphysics. They're abstracted away by the QueryManager's usage of the NarrowPhaseHelper and such.Can I find example code for such collision testing also in the CharacterController.cs?
Performing collision detection between axis-aligned bounding boxes is much simpler than what the QueryManager is calling inside BEPUphysics. I think there are some resources on the XNA website about it. You might try looking into the Ship Game sample and BoxCollider library for examples.
Re: Checking for collision between two groups
Ah thanks for the info.
I'll definitely try it.
Thanks.

I'll definitely try it.
Thanks.