-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathCollisionFilteringDemo.cs
69 lines (64 loc) · 2.88 KB
/
CollisionFilteringDemo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using BEPUphysics.Entities;
using BEPUphysics.Entities.Prefabs;
using BEPUphysics.CollisionRuleManagement;
using BEPUutilities;
namespace BEPUphysicsDemos.Demos
{
/// <summary>
/// Some objects pass through each other, showing one way in which collision rules function.
/// </summary>
public class CollisionFilteringDemo : StandardDemo
{
/// <summary>
/// Constructs a new demo.
/// </summary>
/// <param name="game">Game owning this demo.</param>
public CollisionFilteringDemo(DemosGame game)
: base(game)
{
Entity toAdd;
toAdd = new Box(new Vector3(0, -.5f, 0), 50, 1, 50);
Space.Add(toAdd);
//Set up two stacks which go through each other
var firstStackGroup = new CollisionGroup();
var secondStackGroup = new CollisionGroup();
//Adding this rule to the space's collision group rules will prevent entities belong to these two groups from generating collision pairs with each other.
groupPair = new CollisionGroupPair(firstStackGroup, secondStackGroup);
CollisionRules.CollisionGroupRules.Add(groupPair, CollisionRule.NoBroadPhase);
for (int k = 0; k < 10; k++)
{
toAdd = new Box(
new Vector3(-4 + .12f * k, .5f + k, 0), 1f, 1f, 1f,
10);
toAdd.CollisionInformation.CollisionRules.Group = firstStackGroup;
Space.Add(toAdd);
toAdd = new Box(new Vector3(4 - .12f * k, .5f + k, 0),
1f, 1f, 1f, 10);
toAdd.CollisionInformation.CollisionRules.Group = secondStackGroup;
Space.Add(toAdd);
}
//Add another two boxes which ignore each other using the specific entities method; they will still collide with the stacks since they will have the default dynamic collision group.
toAdd = new Box(new Vector3(1, 3, 0), 1f, 4f, 2f, 10);
var toAdd2 = new Box(new Vector3(-1, 3, 0), 1f, 4f, 2f, 15);
CollisionRules.AddRule(toAdd, toAdd2, CollisionRule.NoBroadPhase);
Space.Add(toAdd);
Space.Add(toAdd2);
game.Camera.Position = new Vector3(0, 6, 20);
}
/// <summary>
/// Gets the name of the simulation.
/// </summary>
public override string Name
{
get { return "Collision Filtering"; }
}
CollisionGroupPair groupPair;
public override void CleanUp()
{
base.CleanUp();
//The CollisionGroupRules are static, so this dictionary would fill up
//if we kept changing the simulation without cleaning it out.
CollisionRules.CollisionGroupRules.Remove(groupPair);
}
}
}