Problems with Collisions

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Stevemata
Posts: 33
Joined: Sat Mar 17, 2012 11:11 pm

Problems with Collisions

Post by Stevemata »

I'm using two CollisionGroup objects, one for level geometry and one for event triggers, I put both groups into a CollisionGroupPair object and set CollisionRules.CollisionGroupRules.Add(groupPair, CollisionRule.NoBroadPhase), and it still triggers a collision event. Am mistaken in assuming they won't trigger an event?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Problems with Collisions

Post by Norbo »

If the final rule between the two objects is truly NoBroadPhase, no pair will be created and no event can occur. Are any other rules taking precedence over the group rules (specific and personal rules both come first)? Is it possibly another compound body related difficulty?
Stevemata
Posts: 33
Joined: Sat Mar 17, 2012 11:11 pm

Re: Problems with Collisions

Post by Stevemata »

There is clearly something going on, because the event is triggered instantly when my game starts and then not again until the player actually triggers the event by encountering the trigger. When I reset the level, after loan time, the glitch doesn't happen. Is there a specific order to the procedure?

Here is my current procedure:
1: Create level geometry group
2: Create trigger group
3: Create collision group pair
4: Set Collision Rules with group pair and NoBroadPhase
5: Create the staticmesh object with the level geometry model
6: Set the collision group to levelgeometry
7: Add the staticmesh to space
9: Create the trigger geometry as a Box object
9: Set the trigger's personal collision rule to NoSolver
10: Add the Trigger to the trigger group
11: Create the event for the actual trigger
12: Add the trigger to space
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Problems with Collisions

Post by Norbo »

The sequence of operations during initialization is unimportant. The trigger's personal rule is being set to NoSolver. Using the default prioritization, that will override the groupwise NoBroadPhase rule, allowing a pair to be created. So, either change up the rules to fit the priority scheme, or change the priority scheme.

Here's a mockup of the outlined initialization, with a modification in #9 to swap the priorities:

Code: Select all

            //1: Create level geometry group
            var levelGroup = new CollisionGroup();

            //2: Create trigger group
            var triggerGroup = new CollisionGroup();

            //3: Create collision group pair
            var pair = new CollisionGroupPair(levelGroup, triggerGroup);

            //4: Set Collision Rules with group pair and NoBroadPhase
            CollisionGroup.DefineCollisionRule(levelGroup, triggerGroup, CollisionRule.NoBroadPhase);

            //5: Create the staticmesh object with the level geometry model
            Vector3[] staticTriangleVertices;
            int[] staticTriangleIndices;
            var playgroundModel = game.Content.Load<Model>("playground");
            TriangleMesh.GetVerticesAndIndicesFromModel(playgroundModel, out staticTriangleVertices, out staticTriangleIndices);
            var staticMesh = new StaticMesh(staticTriangleVertices, staticTriangleIndices, new AffineTransform(Matrix3X3.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi), new Vector3(0, -10, 0)));
            staticMesh.Sidedness = TriangleSidedness.Counterclockwise;

            //6: Set the collision group to levelgeometry
            staticMesh.CollisionRules.Group = levelGroup;

            //7: Add the staticmesh to space
            Space.Add(staticMesh);
            game.ModelDrawer.Add(staticMesh);

            //8: Create the trigger geometry as a Box object
            Box trigger = new Box(new Vector3(0, 10, 0), 10, 20, 10);

            //9: Set the trigger's personal collision rule to NoSolver
            //NOTE: This would override the group rule specified earlier!
            trigger.CollisionInformation.CollisionRules.Personal = CollisionRule.NoSolver;

            //To avoid overriding the group rule, swap the priorities.
            //(Note: This was created in the v1.2 development version where ICollisionRulesOwners are passed into the calculator as opposed to CollisionRules directly.
            //This provides a bit more freedom in filtering since you can analyze the owner, but does require a slight change.)
            CollisionRules.CollisionRuleCalculator = (aOwner, bOwner) =>
                {
                    var a = aOwner.CollisionRules;
                    var b = bOwner.CollisionRules;
                    CollisionRule pairRule = CollisionRules.GetSpecificCollisionRuleDefault(a, b);
                    if (pairRule == CollisionRule.Defer)
                    {
                        //The usual order here is to check the personal rule before the group rule.  Swap it!
                        pairRule = CollisionRules.GetGroupCollisionRuleDefault(a, b);
                        if (pairRule == CollisionRule.Defer)
                            pairRule = CollisionRules.GetPersonalCollisionRuleDefault(a, b);
                    }

                    if (pairRule == CollisionRule.Defer)
                        pairRule = CollisionRules.DefaultCollisionRule;
                    return pairRule;
                };

            //10: Add the Trigger to the trigger group
            trigger.CollisionInformation.CollisionRules.Group = triggerGroup;

            //11: Create the event for the actual trigger
            trigger.CollisionInformation.Events.InitialCollisionDetected += (sender, other, pairHandler) =>
            {
                Console.WriteLine("Meow meow meow meow meow meow meow meow meow meow meow meow meow MEOW MEOW MEOW MEOW MEOW");
            };
            //12: Add the trigger to space
            Space.Add(trigger);
Post Reply