-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathDetectorVolumeDemo.cs
136 lines (119 loc) · 5.38 KB
/
DetectorVolumeDemo.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using BEPUphysics.BroadPhaseEntries;
using BEPUphysics.DataStructures;
using BEPUphysics.Entities;
using BEPUphysics.Entities.Prefabs;
using BEPUphysics.NarrowPhaseSystems.Pairs;
using BEPUphysicsDrawer.Models;
using Microsoft.Xna.Framework.Graphics;
using BEPUutilities;
namespace BEPUphysicsDemos.Demos
{
/// <summary>
/// Box changes colors as it interacts with a detector volume.
/// </summary>
public class DetectorVolumeDemo : StandardDemo
{
//Box to change colors when it hits the detector volume.
private ModelDisplayObject displayBox;
private DetectorVolume detectorVolume;
private Entity testEntity;
/// <summary>
/// Constructs a new demo.
/// </summary>
/// <param name="game">Game owning this demo.</param>
public DetectorVolumeDemo(DemosGame game)
: base(game)
{
var model = game.Content.Load<Model>("tube");
Vector3[] modelVertices;
int[] modelIndices;
ModelDataExtractor.GetVerticesAndIndicesFromModel(model, out modelVertices, out modelIndices);
detectorVolume = new DetectorVolume(new TriangleMesh(new StaticMeshData(modelVertices, modelIndices)));
Space.Add(detectorVolume);
game.ModelDrawer.Add(detectorVolume.TriangleMesh);
//The detector volume works on all of the entity types:
//Convexes!
testEntity = new Box(new Vector3(0, -10, 0), 1, 1, 1);
//Compounds!
//var bodies = new List<CompoundShapeEntry>
//{
// new CompoundShapeEntry(new SphereShape(.5f), new Vector3(0, -8.2f, 0), 1),
// new CompoundShapeEntry(new SphereShape(.5f), new Vector3(0, -9f, 0), 1),
// new CompoundShapeEntry(new SphereShape(.5f), new Vector3(0, -9.8f, 0), 1)
//};
//testEntity = new CompoundBody(bodies);
//Mobile meshes!
//model = game.Content.Load<Model>("tube");
//TriangleMesh.GetVerticesAndIndicesFromModel(model, out modelVertices, out modelIndices);
//testEntity = new MobileMesh(modelVertices, modelIndices, new AffineTransform(new Vector3(.2f, .2f, .2f), Quaternion.Identity, new Vector3(0, -10, 0)), MobileMeshSolidity.Solid);
testEntity.Tag = "noDisplayObject";
displayBox = game.ModelDrawer.Add(testEntity);
SetColor(0);
game.ModelDrawer.IsWireframe = true;
testEntity.LinearVelocity = new Vector3(0, 1, 0);
Space.Add(testEntity);
//The color of the entity will change based upon events.
//The events don't pay attention to the causing events, so you can toss a ball through the volume to recolor the entity.
detectorVolume.EntityBeganTouching += BeganTouching;
detectorVolume.EntityStoppedTouching += StoppedTouching;
detectorVolume.VolumeBeganContainingEntity += BeganContaining;
detectorVolume.VolumeStoppedContainingEntity += StoppedContaining;
game.Camera.Position = new Vector3(0, 0, 22);
Space.ForceUpdater.Gravity = new Vector3();
}
/// <summary>
/// Gets the name of the simulation.
/// </summary>
public override string Name
{
get { return "Detector Volume"; }
}
private void StoppedContaining(DetectorVolume volume, Entity entity)
{
SetColor(3);
}
private void BeganContaining(DetectorVolume volume, Entity entity)
{
SetColor(2);
}
private void StoppedTouching(DetectorVolume volume, Entity toucher)
{
SetColor(0);
}
private void BeganTouching(DetectorVolume volume, Entity toucher)
{
SetColor(1);
}
void SetColor(int index)
{
//The model drawer requires a reset to change the color.
Game.ModelDrawer.Remove(testEntity);
displayBox = Game.ModelDrawer.GetDisplayObject(testEntity);
displayBox.TextureIndex = index;
Game.ModelDrawer.Add(displayBox);
}
public override void DrawUI()
{
//Here's an alternate method of keeping track of changes: check the pairs list!
//This will check specifically for our test entity, ignoring any other flying objects that might have been tossed through.
DetectorVolumePairHandler pair;
if (detectorVolume.Pairs.TryGetValue(testEntity, out pair))
{
if (pair.Containing)
Game.DataTextDrawer.Draw("Contained", new Microsoft.Xna.Framework.Vector2(50, 50));
else if (pair.Touching)
Game.DataTextDrawer.Draw("Touching", new Microsoft.Xna.Framework.Vector2(50, 50));
else
Game.DataTextDrawer.Draw("Separated", new Microsoft.Xna.Framework.Vector2(50, 50));
}
else
Game.DataTextDrawer.Draw("Separated", new Microsoft.Xna.Framework.Vector2(50, 50));
base.DrawUI();
}
public override void CleanUp()
{
Game.ModelDrawer.IsWireframe = false;
base.CleanUp();
}
}
}