aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/PhysicsModules/BasicPhysics/BasicPhysicsScene.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/PhysicsModules/BasicPhysics/BasicPhysicsScene.cs')
-rw-r--r--OpenSim/Region/PhysicsModules/BasicPhysics/BasicPhysicsScene.cs256
1 files changed, 256 insertions, 0 deletions
diff --git a/OpenSim/Region/PhysicsModules/BasicPhysics/BasicPhysicsScene.cs b/OpenSim/Region/PhysicsModules/BasicPhysics/BasicPhysicsScene.cs
new file mode 100644
index 0000000..ac2c1f3
--- /dev/null
+++ b/OpenSim/Region/PhysicsModules/BasicPhysics/BasicPhysicsScene.cs
@@ -0,0 +1,256 @@
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 copyright
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
28using System;
29using System.Collections.Generic;
30using Nini.Config;
31using Mono.Addins;
32using OpenMetaverse;
33using OpenSim.Framework;
34using OpenSim.Region.PhysicsModules.SharedBase;
35using OpenSim.Region.Framework.Scenes;
36using OpenSim.Region.Framework.Interfaces;
37
38namespace OpenSim.Region.PhysicsModule.BasicPhysics
39{
40 /// <summary>
41 /// This is an incomplete extremely basic physics implementation
42 /// </summary>
43 /// <remarks>
44 /// Not useful for anything at the moment apart from some regression testing in other components where some form
45 /// of physics plugin is needed.
46 /// </remarks>
47 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "BasicPhysicsScene")]
48 public class BasicScene : PhysicsScene, INonSharedRegionModule
49 {
50 private List<BasicActor> _actors = new List<BasicActor>();
51 private List<BasicPhysicsPrim> _prims = new List<BasicPhysicsPrim>();
52 private float[] _heightMap;
53 private Vector3 m_regionExtent;
54
55 private bool m_Enabled = false;
56
57 //protected internal string sceneIdentifier;
58 #region INonSharedRegionModule
59 public string Name
60 {
61 get { return "basicphysics"; }
62 }
63
64 public Type ReplaceableInterface
65 {
66 get { return null; }
67 }
68
69 public void Initialise(IConfigSource source)
70 {
71 // TODO: Move this out of Startup
72 IConfig config = source.Configs["Startup"];
73 if (config != null)
74 {
75 string physics = config.GetString("physics", string.Empty);
76 if (physics == Name)
77 m_Enabled = true;
78 }
79
80 }
81
82 public void Close()
83 {
84 }
85
86 public void AddRegion(Scene scene)
87 {
88 if (!m_Enabled)
89 return;
90
91 EngineType = Name;
92 PhysicsSceneName = EngineType + "/" + scene.RegionInfo.RegionName;
93
94 scene.RegisterModuleInterface<PhysicsScene>(this);
95 m_regionExtent = new Vector3(scene.RegionInfo.RegionSizeX, scene.RegionInfo.RegionSizeY, scene.RegionInfo.RegionSizeZ);
96 base.Initialise(scene.PhysicsRequestAsset,
97 (scene.Heightmap != null ? scene.Heightmap.GetFloatsSerialised() : new float[scene.RegionInfo.RegionSizeX * scene.RegionInfo.RegionSizeY]),
98 (float)scene.RegionInfo.RegionSettings.WaterHeight);
99
100 }
101
102 public void RemoveRegion(Scene scene)
103 {
104 if (!m_Enabled)
105 return;
106 }
107
108 public void RegionLoaded(Scene scene)
109 {
110 if (!m_Enabled)
111 return;
112 }
113 #endregion
114
115 public override void Dispose() {}
116
117 public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
118 Vector3 size, Quaternion rotation, bool isPhysical, uint localid)
119 {
120 BasicPhysicsPrim prim = new BasicPhysicsPrim(primName, localid, position, size, rotation, pbs);
121 prim.IsPhysical = isPhysical;
122
123 _prims.Add(prim);
124
125 return prim;
126 }
127
128 public override PhysicsActor AddAvatar(string avName, Vector3 position, Vector3 velocity, Vector3 size, bool isFlying)
129 {
130 BasicActor act = new BasicActor(size);
131 act.Position = position;
132 act.Velocity = velocity;
133 act.Flying = isFlying;
134 _actors.Add(act);
135 return act;
136 }
137
138 public override void RemovePrim(PhysicsActor actor)
139 {
140 BasicPhysicsPrim prim = (BasicPhysicsPrim)actor;
141 if (_prims.Contains(prim))
142 _prims.Remove(prim);
143 }
144
145 public override void RemoveAvatar(PhysicsActor actor)
146 {
147 BasicActor act = (BasicActor)actor;
148 if (_actors.Contains(act))
149 _actors.Remove(act);
150 }
151
152 public override void AddPhysicsActorTaint(PhysicsActor prim)
153 {
154 }
155
156 public override float Simulate(float timeStep)
157 {
158// Console.WriteLine("Simulating");
159
160 float fps = 0;
161 for (int i = 0; i < _actors.Count; ++i)
162 {
163 BasicActor actor = _actors[i];
164 Vector3 actorPosition = actor.Position;
165 Vector3 actorVelocity = actor.Velocity;
166
167 //Console.WriteLine(
168 // "Processing actor {0}, starting pos {1}, starting vel {2}", i, actorPosition, actorVelocity);
169
170 actorPosition.X += actor.Velocity.X * timeStep;
171 actorPosition.Y += actor.Velocity.Y * timeStep;
172
173 if (actor.Position.Y < 0)
174 {
175 actorPosition.Y = 0.1F;
176 }
177 else if (actor.Position.Y >= m_regionExtent.Y)
178 {
179 actorPosition.Y = (m_regionExtent.Y - 0.1f);
180 }
181
182 if (actor.Position.X < 0)
183 {
184 actorPosition.X = 0.1F;
185 }
186 else if (actor.Position.X >= m_regionExtent.X)
187 {
188 actorPosition.X = (m_regionExtent.X - 0.1f);
189 }
190
191 float terrainHeight = 0;
192 if (_heightMap != null)
193 terrainHeight = _heightMap[(int)actor.Position.Y * (int)m_regionExtent.Y + (int)actor.Position.X];
194
195 float height = terrainHeight + actor.Size.Z;
196// Console.WriteLine("height {0}, actorPosition {1}", height, actorPosition);
197
198 if (actor.Flying)
199 {
200 if (actor.Position.Z + (actor.Velocity.Z * timeStep) < terrainHeight + 2)
201 {
202 actorPosition.Z = height;
203 actorVelocity.Z = 0;
204 actor.IsColliding = true;
205 }
206 else
207 {
208 actorPosition.Z += actor.Velocity.Z * timeStep;
209 actor.IsColliding = false;
210 }
211 }
212 else
213 {
214 actorPosition.Z = height;
215 actorVelocity.Z = 0;
216 actor.IsColliding = true;
217 }
218
219 actor.Position = actorPosition;
220 actor.Velocity = actorVelocity;
221 }
222
223 return 1.0f;
224 }
225
226 public override void GetResults()
227 {
228 }
229
230 public override bool IsThreaded
231 {
232 get { return (false); // for now we won't be multithreaded
233 }
234 }
235
236 public override void SetTerrain(float[] heightMap)
237 {
238 _heightMap = heightMap;
239 }
240
241 public override void DeleteTerrain()
242 {
243 }
244
245 public override void SetWaterLevel(float baseheight)
246 {
247 }
248
249 public override Dictionary<uint, float> GetTopColliders()
250 {
251 Dictionary<uint, float> returncolliders = new Dictionary<uint, float>();
252 return returncolliders;
253 }
254
255 }
256}