diff options
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BulletSimData.cs')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BulletSimData.cs | 282 |
1 files changed, 282 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BulletSimData.cs b/OpenSim/Region/Physics/BulletSPlugin/BulletSimData.cs new file mode 100755 index 0000000..e5c4777 --- /dev/null +++ b/OpenSim/Region/Physics/BulletSPlugin/BulletSimData.cs | |||
@@ -0,0 +1,282 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyrightD | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | using System; | ||
28 | using System.Collections.Generic; | ||
29 | using System.Text; | ||
30 | using OMV = OpenMetaverse; | ||
31 | |||
32 | namespace OpenSim.Region.Physics.BulletSPlugin | ||
33 | { | ||
34 | // Classes to allow some type checking for the API | ||
35 | // These hold pointers to allocated objects in the unmanaged space. | ||
36 | |||
37 | // The physics engine controller class created at initialization | ||
38 | public struct BulletSim | ||
39 | { | ||
40 | public BulletSim(uint worldId, BSScene bss, IntPtr xx) | ||
41 | { | ||
42 | ptr = xx; | ||
43 | worldID = worldId; | ||
44 | physicsScene = bss; | ||
45 | } | ||
46 | public IntPtr ptr; | ||
47 | public uint worldID; | ||
48 | // The scene is only in here so very low level routines have a handle to print debug/error messages | ||
49 | public BSScene physicsScene; | ||
50 | } | ||
51 | |||
52 | // An allocated Bullet btRigidBody | ||
53 | public struct BulletBody | ||
54 | { | ||
55 | public BulletBody(uint id) : this(id, IntPtr.Zero) | ||
56 | { | ||
57 | } | ||
58 | public BulletBody(uint id, IntPtr xx) | ||
59 | { | ||
60 | ID = id; | ||
61 | ptr = xx; | ||
62 | collisionType = CollisionType.Static; | ||
63 | } | ||
64 | public IntPtr ptr; | ||
65 | public uint ID; | ||
66 | public CollisionType collisionType; | ||
67 | |||
68 | public void Clear() | ||
69 | { | ||
70 | ptr = IntPtr.Zero; | ||
71 | } | ||
72 | public bool HasPhysicalBody { get { return ptr != IntPtr.Zero; } } | ||
73 | |||
74 | // Apply the specificed collision mask into the physical world | ||
75 | public void ApplyCollisionMask() | ||
76 | { | ||
77 | // Should assert the body has been added to the physical world. | ||
78 | // (The collision masks are stored in the collision proxy cache which only exists for | ||
79 | // a collision body that is in the world.) | ||
80 | BulletSimAPI.SetCollisionGroupMask2(ptr, | ||
81 | BulletSimData.CollisionTypeMasks[collisionType].group, | ||
82 | BulletSimData.CollisionTypeMasks[collisionType].mask); | ||
83 | } | ||
84 | |||
85 | public override string ToString() | ||
86 | { | ||
87 | StringBuilder buff = new StringBuilder(); | ||
88 | buff.Append("<id="); | ||
89 | buff.Append(ID.ToString()); | ||
90 | buff.Append(",p="); | ||
91 | buff.Append(ptr.ToString("X")); | ||
92 | buff.Append(",c="); | ||
93 | buff.Append(collisionType); | ||
94 | buff.Append(">"); | ||
95 | return buff.ToString(); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | public struct BulletShape | ||
100 | { | ||
101 | public BulletShape(IntPtr xx) | ||
102 | { | ||
103 | ptr = xx; | ||
104 | type=BSPhysicsShapeType.SHAPE_UNKNOWN; | ||
105 | shapeKey = (System.UInt64)FixedShapeKey.KEY_NONE; | ||
106 | isNativeShape = false; | ||
107 | } | ||
108 | public BulletShape(IntPtr xx, BSPhysicsShapeType typ) | ||
109 | { | ||
110 | ptr = xx; | ||
111 | type = typ; | ||
112 | shapeKey = 0; | ||
113 | isNativeShape = false; | ||
114 | } | ||
115 | public IntPtr ptr; | ||
116 | public BSPhysicsShapeType type; | ||
117 | public System.UInt64 shapeKey; | ||
118 | public bool isNativeShape; | ||
119 | |||
120 | public void Clear() | ||
121 | { | ||
122 | ptr = IntPtr.Zero; | ||
123 | } | ||
124 | public bool HasPhysicalShape { get { return ptr != IntPtr.Zero; } } | ||
125 | |||
126 | public override string ToString() | ||
127 | { | ||
128 | StringBuilder buff = new StringBuilder(); | ||
129 | buff.Append("<p="); | ||
130 | buff.Append(ptr.ToString("X")); | ||
131 | buff.Append(",s="); | ||
132 | buff.Append(type.ToString()); | ||
133 | buff.Append(",k="); | ||
134 | buff.Append(shapeKey.ToString("X")); | ||
135 | buff.Append(",n="); | ||
136 | buff.Append(isNativeShape.ToString()); | ||
137 | buff.Append(">"); | ||
138 | return buff.ToString(); | ||
139 | } | ||
140 | } | ||
141 | |||
142 | // An allocated Bullet btConstraint | ||
143 | public struct BulletConstraint | ||
144 | { | ||
145 | public BulletConstraint(IntPtr xx) | ||
146 | { | ||
147 | ptr = xx; | ||
148 | } | ||
149 | public IntPtr ptr; | ||
150 | |||
151 | public void Clear() | ||
152 | { | ||
153 | ptr = IntPtr.Zero; | ||
154 | } | ||
155 | public bool HasPhysicalConstraint { get { return ptr != IntPtr.Zero; } } | ||
156 | } | ||
157 | |||
158 | // An allocated HeightMapThing which holds various heightmap info. | ||
159 | // Made a class rather than a struct so there would be only one | ||
160 | // instance of this and C# will pass around pointers rather | ||
161 | // than making copies. | ||
162 | public class BulletHeightMapInfo | ||
163 | { | ||
164 | public BulletHeightMapInfo(uint id, float[] hm, IntPtr xx) { | ||
165 | ID = id; | ||
166 | Ptr = xx; | ||
167 | heightMap = hm; | ||
168 | terrainRegionBase = OMV.Vector3.Zero; | ||
169 | minCoords = new OMV.Vector3(100f, 100f, 25f); | ||
170 | maxCoords = new OMV.Vector3(101f, 101f, 26f); | ||
171 | minZ = maxZ = 0f; | ||
172 | sizeX = sizeY = 256f; | ||
173 | } | ||
174 | public uint ID; | ||
175 | public IntPtr Ptr; | ||
176 | public float[] heightMap; | ||
177 | public OMV.Vector3 terrainRegionBase; | ||
178 | public OMV.Vector3 minCoords; | ||
179 | public OMV.Vector3 maxCoords; | ||
180 | public float sizeX, sizeY; | ||
181 | public float minZ, maxZ; | ||
182 | public BulletShape terrainShape; | ||
183 | public BulletBody terrainBody; | ||
184 | } | ||
185 | |||
186 | // The general class of collsion object. | ||
187 | public enum CollisionType | ||
188 | { | ||
189 | Avatar, | ||
190 | Groundplane, | ||
191 | Terrain, | ||
192 | Static, | ||
193 | Dynamic, | ||
194 | VolumeDetect, | ||
195 | // Linkset, // A linkset proper should be either Static or Dynamic | ||
196 | LinksetChild, | ||
197 | Unknown | ||
198 | }; | ||
199 | |||
200 | // Hold specification of group and mask collision flags for a CollisionType | ||
201 | public struct CollisionTypeFilterGroup | ||
202 | { | ||
203 | public CollisionTypeFilterGroup(CollisionType t, uint g, uint m) | ||
204 | { | ||
205 | type = t; | ||
206 | group = g; | ||
207 | mask = m; | ||
208 | } | ||
209 | public CollisionType type; | ||
210 | public uint group; | ||
211 | public uint mask; | ||
212 | }; | ||
213 | |||
214 | /* | ||
215 | // The collsion filters and masked are defined in one place -- don't want them scattered | ||
216 | AvatarGroup = BCharacterGroup, | ||
217 | AvatarMask = BAllGroup, | ||
218 | ObjectGroup = BSolidGroup, | ||
219 | ObjectMask = BAllGroup, | ||
220 | StaticObjectGroup = BStaticGroup, | ||
221 | StaticObjectMask = AvatarGroup | ObjectGroup, // static things don't interact with much | ||
222 | LinksetGroup = BLinksetGroup, | ||
223 | LinksetMask = BAllGroup, | ||
224 | LinksetChildGroup = BLinksetChildGroup, | ||
225 | LinksetChildMask = BNoneGroup, // Linkset children disappear from the world | ||
226 | VolumeDetectGroup = BSensorTrigger, | ||
227 | VolumeDetectMask = ~BSensorTrigger, | ||
228 | TerrainGroup = BTerrainGroup, | ||
229 | TerrainMask = BAllGroup & ~BStaticGroup, // static objects on the ground don't collide | ||
230 | GroundPlaneGroup = BGroundPlaneGroup, | ||
231 | GroundPlaneMask = BAllGroup | ||
232 | */ | ||
233 | |||
234 | public static class BulletSimData | ||
235 | { | ||
236 | |||
237 | // Map of collisionTypes to flags for collision groups and masks. | ||
238 | // As mentioned above, don't use the CollisionFilterGroups definitions directly in the code | ||
239 | // but, instead, user references to this dictionary. This makes finding and debugging | ||
240 | // collision flag usage easier. | ||
241 | public static Dictionary<CollisionType, CollisionTypeFilterGroup> CollisionTypeMasks | ||
242 | = new Dictionary<CollisionType, CollisionTypeFilterGroup>() | ||
243 | { | ||
244 | { CollisionType.Avatar, | ||
245 | new CollisionTypeFilterGroup(CollisionType.Avatar, | ||
246 | (uint)CollisionFilterGroups.BCharacterGroup, | ||
247 | (uint)CollisionFilterGroups.BAllGroup) | ||
248 | }, | ||
249 | { CollisionType.Groundplane, | ||
250 | new CollisionTypeFilterGroup(CollisionType.Groundplane, | ||
251 | (uint)CollisionFilterGroups.BGroundPlaneGroup, | ||
252 | (uint)CollisionFilterGroups.BAllGroup) | ||
253 | }, | ||
254 | { CollisionType.Terrain, | ||
255 | new CollisionTypeFilterGroup(CollisionType.Terrain, | ||
256 | (uint)CollisionFilterGroups.BTerrainGroup, | ||
257 | (uint)(CollisionFilterGroups.BAllGroup & ~CollisionFilterGroups.BStaticGroup)) | ||
258 | }, | ||
259 | { CollisionType.Static, | ||
260 | new CollisionTypeFilterGroup(CollisionType.Static, | ||
261 | (uint)CollisionFilterGroups.BStaticGroup, | ||
262 | (uint)(CollisionFilterGroups.BCharacterGroup | CollisionFilterGroups.BSolidGroup)) | ||
263 | }, | ||
264 | { CollisionType.Dynamic, | ||
265 | new CollisionTypeFilterGroup(CollisionType.Dynamic, | ||
266 | (uint)CollisionFilterGroups.BSolidGroup, | ||
267 | (uint)(CollisionFilterGroups.BAllGroup)) | ||
268 | }, | ||
269 | { CollisionType.VolumeDetect, | ||
270 | new CollisionTypeFilterGroup(CollisionType.VolumeDetect, | ||
271 | (uint)CollisionFilterGroups.BSensorTrigger, | ||
272 | (uint)(~CollisionFilterGroups.BSensorTrigger)) | ||
273 | }, | ||
274 | { CollisionType.LinksetChild, | ||
275 | new CollisionTypeFilterGroup(CollisionType.LinksetChild, | ||
276 | (uint)CollisionFilterGroups.BTerrainGroup, | ||
277 | (uint)(CollisionFilterGroups.BNoneGroup)) | ||
278 | }, | ||
279 | }; | ||
280 | |||
281 | } | ||
282 | } | ||