diff options
Diffstat (limited to 'OpenSim/Region/Physics/UbitOdePlugin/ODESitAvatar.cs')
-rw-r--r-- | OpenSim/Region/Physics/UbitOdePlugin/ODESitAvatar.cs | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/UbitOdePlugin/ODESitAvatar.cs b/OpenSim/Region/Physics/UbitOdePlugin/ODESitAvatar.cs new file mode 100644 index 0000000..225bff8 --- /dev/null +++ b/OpenSim/Region/Physics/UbitOdePlugin/ODESitAvatar.cs | |||
@@ -0,0 +1,183 @@ | |||
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 | // Ubit 2012 | ||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Reflection; | ||
31 | using System.Runtime.InteropServices; | ||
32 | using System.Text; | ||
33 | using OpenSim.Framework; | ||
34 | using OpenSim.Region.Physics.Manager; | ||
35 | using OdeAPI; | ||
36 | using log4net; | ||
37 | using OpenMetaverse; | ||
38 | |||
39 | namespace OpenSim.Region.Physics.OdePlugin | ||
40 | { | ||
41 | /// <summary> | ||
42 | /// </summary> | ||
43 | public class ODESitAvatar | ||
44 | { | ||
45 | private OdeScene m_scene; | ||
46 | private ODERayCastRequestManager m_raymanager; | ||
47 | |||
48 | public ODESitAvatar(OdeScene pScene, ODERayCastRequestManager raymanager) | ||
49 | { | ||
50 | m_scene = pScene; | ||
51 | m_raymanager = raymanager; | ||
52 | } | ||
53 | |||
54 | private static Vector3 SitAjust = new Vector3(0, 0, 0.4f); | ||
55 | |||
56 | public void Sit(PhysicsActor actor, Vector3 avPos, Vector3 avCameraPosition, Vector3 offset, Vector3 avOffset, SitAvatarCallback PhysicsSitResponse) | ||
57 | { | ||
58 | if (!m_scene.haveActor(actor) || !(actor is OdePrim) || ((OdePrim)actor).prim_geom == IntPtr.Zero) | ||
59 | { | ||
60 | PhysicsSitResponse(-1, actor.LocalID, offset, Quaternion.Identity); | ||
61 | return; | ||
62 | } | ||
63 | |||
64 | IntPtr geom = ((OdePrim)actor).prim_geom; | ||
65 | |||
66 | d.Vector3 dtmp = d.GeomGetPosition(geom); | ||
67 | Vector3 geopos; | ||
68 | geopos.X = dtmp.X; | ||
69 | geopos.Y = dtmp.Y; | ||
70 | geopos.Z = dtmp.Z; | ||
71 | |||
72 | |||
73 | d.AABB aabb; | ||
74 | Quaternion ori; | ||
75 | d.Quaternion qtmp; | ||
76 | d.GeomCopyQuaternion(geom, out qtmp); | ||
77 | Quaternion geomOri; | ||
78 | geomOri.X = qtmp.X; | ||
79 | geomOri.Y = qtmp.Y; | ||
80 | geomOri.Z = qtmp.Z; | ||
81 | geomOri.W = qtmp.W; | ||
82 | Quaternion geomInvOri; | ||
83 | geomInvOri.X = -qtmp.X; | ||
84 | geomInvOri.Y = -qtmp.Y; | ||
85 | geomInvOri.Z = -qtmp.Z; | ||
86 | geomInvOri.W = qtmp.W; | ||
87 | |||
88 | Vector3 target = geopos + offset; | ||
89 | Vector3 rayDir = target - avCameraPosition; | ||
90 | float raylen = rayDir.Length(); | ||
91 | float t = 1 / raylen; | ||
92 | rayDir.X *= t; | ||
93 | rayDir.Y *= t; | ||
94 | rayDir.Z *= t; | ||
95 | |||
96 | raylen += 0.5f; | ||
97 | List<ContactResult> rayResults; | ||
98 | |||
99 | rayResults = m_scene.RaycastActor(actor, avCameraPosition, rayDir , raylen, 1); | ||
100 | if (rayResults.Count == 0 || rayResults[0].ConsumerID != actor.LocalID) | ||
101 | { | ||
102 | d.GeomGetAABB(geom,out aabb); | ||
103 | offset = new Vector3(avOffset.X, 0, aabb.MaxZ + avOffset.Z - geopos.Z); | ||
104 | ori = geomInvOri; | ||
105 | offset *= geomInvOri; | ||
106 | |||
107 | PhysicsSitResponse(1, actor.LocalID, offset, ori); | ||
108 | return; | ||
109 | } | ||
110 | |||
111 | offset = rayResults[0].Pos - geopos; | ||
112 | double ang; | ||
113 | float s; | ||
114 | float c; | ||
115 | |||
116 | d.GeomClassID geoclass = d.GeomGetClass(geom); | ||
117 | |||
118 | if (geoclass == d.GeomClassID.SphereClass) | ||
119 | { | ||
120 | float r = d.GeomSphereGetRadius(geom); | ||
121 | |||
122 | offset.Normalize(); | ||
123 | offset *= r; | ||
124 | |||
125 | ang = Math.Atan2(offset.Y, offset.X); | ||
126 | ang *= 0.5d; | ||
127 | s = (float)Math.Sin(ang); | ||
128 | c = (float)Math.Cos(ang); | ||
129 | |||
130 | ori = new Quaternion(0, 0, s, c); | ||
131 | |||
132 | if (r < 0.4f) | ||
133 | { | ||
134 | offset = new Vector3(0, 0, r); | ||
135 | } | ||
136 | else if (offset.Z < 0.4f) | ||
137 | { | ||
138 | t = offset.Z; | ||
139 | float rsq = r * r; | ||
140 | |||
141 | t = 1.0f / (rsq - t * t); | ||
142 | offset.X *= t; | ||
143 | offset.Y *= t; | ||
144 | offset.Z = 0.4f; | ||
145 | t = rsq - 0.16f; | ||
146 | offset.X *= t; | ||
147 | offset.Y *= t; | ||
148 | } | ||
149 | |||
150 | offset += avOffset * ori; | ||
151 | |||
152 | ori = geomInvOri * ori; | ||
153 | offset *= geomInvOri; | ||
154 | |||
155 | PhysicsSitResponse(1, actor.LocalID, offset, ori); | ||
156 | return; | ||
157 | } | ||
158 | |||
159 | Vector3 norm = rayResults[0].Normal; | ||
160 | |||
161 | if (norm.Z < 0) | ||
162 | { | ||
163 | PhysicsSitResponse(0, actor.LocalID, offset, Quaternion.Identity); | ||
164 | return; | ||
165 | } | ||
166 | |||
167 | ang = Math.Atan2(-rayDir.Y, -rayDir.X); | ||
168 | ang *= 0.5d; | ||
169 | s = (float)Math.Sin(ang); | ||
170 | c = (float)Math.Cos(ang); | ||
171 | |||
172 | ori = new Quaternion(0, 0, s, c); | ||
173 | |||
174 | offset += avOffset * ori; | ||
175 | |||
176 | ori = geomInvOri * ori; | ||
177 | offset *= geomInvOri; | ||
178 | |||
179 | PhysicsSitResponse(1, actor.LocalID, offset, ori); | ||
180 | return; | ||
181 | } | ||
182 | } | ||
183 | } \ No newline at end of file | ||