1
0

BSCharacter.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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.Reflection;
  30. using log4net;
  31. using OMV = OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Physics.Manager;
  34. namespace OpenSim.Region.Physics.BulletSPlugin
  35. {
  36. public sealed class BSCharacter : BSPhysObject
  37. {
  38. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. private static readonly string LogHeader = "[BULLETS CHAR]";
  40. // private bool _stopped;
  41. private OMV.Vector3 _size;
  42. private bool _grabbed;
  43. private bool _selected;
  44. private float _mass;
  45. private float _avatarVolume;
  46. private float _collisionScore;
  47. private OMV.Vector3 _acceleration;
  48. private int _physicsActorType;
  49. private bool _isPhysical;
  50. private bool _flying;
  51. private bool _setAlwaysRun;
  52. private bool _throttleUpdates;
  53. private bool _floatOnWater;
  54. private OMV.Vector3 _rotationalVelocity;
  55. private bool _kinematic;
  56. private float _buoyancy;
  57. private BSActorAvatarMove m_moveActor;
  58. private const string AvatarMoveActorName = "BSCharacter.AvatarMove";
  59. private OMV.Vector3 _PIDTarget;
  60. private bool _usePID;
  61. private float _PIDTau;
  62. public BSCharacter(uint localID, String avName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size, bool isFlying)
  63. : base(parent_scene, localID, avName, "BSCharacter")
  64. {
  65. _physicsActorType = (int)ActorTypes.Agent;
  66. RawPosition = pos;
  67. _flying = isFlying;
  68. RawOrientation = OMV.Quaternion.Identity;
  69. RawVelocity = OMV.Vector3.Zero;
  70. _buoyancy = ComputeBuoyancyFromFlying(isFlying);
  71. Friction = BSParam.AvatarStandingFriction;
  72. Density = BSParam.AvatarDensity;
  73. // Old versions of ScenePresence passed only the height. If width and/or depth are zero,
  74. // replace with the default values.
  75. _size = size;
  76. if (_size.X == 0f) _size.X = BSParam.AvatarCapsuleDepth;
  77. if (_size.Y == 0f) _size.Y = BSParam.AvatarCapsuleWidth;
  78. // The dimensions of the physical capsule are kept in the scale.
  79. // Physics creates a unit capsule which is scaled by the physics engine.
  80. Scale = ComputeAvatarScale(_size);
  81. // set _avatarVolume and _mass based on capsule size, _density and Scale
  82. ComputeAvatarVolumeAndMass();
  83. DetailLog("{0},BSCharacter.create,call,size={1},scale={2},density={3},volume={4},mass={5},pos={6}",
  84. LocalID, _size, Scale, Density, _avatarVolume, RawMass, pos);
  85. // do actual creation in taint time
  86. PhysScene.TaintedObject(LocalID, "BSCharacter.create", delegate()
  87. {
  88. DetailLog("{0},BSCharacter.create,taint", LocalID);
  89. // New body and shape into PhysBody and PhysShape
  90. PhysScene.Shapes.GetBodyAndShape(true, PhysScene.World, this);
  91. // The avatar's movement is controlled by this motor that speeds up and slows down
  92. // the avatar seeking to reach the motor's target speed.
  93. // This motor runs as a prestep action for the avatar so it will keep the avatar
  94. // standing as well as moving. Destruction of the avatar will destroy the pre-step action.
  95. m_moveActor = new BSActorAvatarMove(PhysScene, this, AvatarMoveActorName);
  96. PhysicalActors.Add(AvatarMoveActorName, m_moveActor);
  97. SetPhysicalProperties();
  98. IsInitialized = true;
  99. });
  100. return;
  101. }
  102. // called when this character is being destroyed and the resources should be released
  103. public override void Destroy()
  104. {
  105. IsInitialized = false;
  106. base.Destroy();
  107. DetailLog("{0},BSCharacter.Destroy", LocalID);
  108. PhysScene.TaintedObject(LocalID, "BSCharacter.destroy", delegate()
  109. {
  110. PhysScene.Shapes.DereferenceBody(PhysBody, null /* bodyCallback */);
  111. PhysBody.Clear();
  112. PhysShape.Dereference(PhysScene);
  113. PhysShape = new BSShapeNull();
  114. });
  115. }
  116. private void SetPhysicalProperties()
  117. {
  118. PhysScene.PE.RemoveObjectFromWorld(PhysScene.World, PhysBody);
  119. ZeroMotion(true);
  120. ForcePosition = RawPosition;
  121. // Set the velocity
  122. if (m_moveActor != null)
  123. m_moveActor.SetVelocityAndTarget(RawVelocity, RawVelocity, false);
  124. ForceVelocity = RawVelocity;
  125. // This will enable or disable the flying buoyancy of the avatar.
  126. // Needs to be reset especially when an avatar is recreated after crossing a region boundry.
  127. Flying = _flying;
  128. PhysScene.PE.SetRestitution(PhysBody, BSParam.AvatarRestitution);
  129. PhysScene.PE.SetMargin(PhysShape.physShapeInfo, PhysScene.Params.collisionMargin);
  130. PhysScene.PE.SetLocalScaling(PhysShape.physShapeInfo, Scale);
  131. PhysScene.PE.SetContactProcessingThreshold(PhysBody, BSParam.ContactProcessingThreshold);
  132. if (BSParam.CcdMotionThreshold > 0f)
  133. {
  134. PhysScene.PE.SetCcdMotionThreshold(PhysBody, BSParam.CcdMotionThreshold);
  135. PhysScene.PE.SetCcdSweptSphereRadius(PhysBody, BSParam.CcdSweptSphereRadius);
  136. }
  137. UpdatePhysicalMassProperties(RawMass, false);
  138. // Make so capsule does not fall over
  139. PhysScene.PE.SetAngularFactorV(PhysBody, OMV.Vector3.Zero);
  140. // The avatar mover sets some parameters.
  141. PhysicalActors.Refresh();
  142. PhysScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.CF_CHARACTER_OBJECT);
  143. PhysScene.PE.AddObjectToWorld(PhysScene.World, PhysBody);
  144. // PhysicsScene.PE.ForceActivationState(PhysBody, ActivationState.ACTIVE_TAG);
  145. PhysScene.PE.ForceActivationState(PhysBody, ActivationState.DISABLE_DEACTIVATION);
  146. PhysScene.PE.UpdateSingleAabb(PhysScene.World, PhysBody);
  147. // Do this after the object has been added to the world
  148. PhysBody.collisionType = CollisionType.Avatar;
  149. PhysBody.ApplyCollisionMask(PhysScene);
  150. }
  151. public override void RequestPhysicsterseUpdate()
  152. {
  153. base.RequestPhysicsterseUpdate();
  154. }
  155. // No one calls this method so I don't know what it could possibly mean
  156. public override bool Stopped { get { return false; } }
  157. public override OMV.Vector3 Size {
  158. get
  159. {
  160. // Avatar capsule size is kept in the scale parameter.
  161. return _size;
  162. }
  163. set {
  164. // This is how much the avatar size is changing. Positive means getting bigger.
  165. // The avatar altitude must be adjusted for this change.
  166. float heightChange = value.Z - _size.Z;
  167. _size = value;
  168. // Old versions of ScenePresence passed only the height. If width and/or depth are zero,
  169. // replace with the default values.
  170. if (_size.X == 0f) _size.X = BSParam.AvatarCapsuleDepth;
  171. if (_size.Y == 0f) _size.Y = BSParam.AvatarCapsuleWidth;
  172. Scale = ComputeAvatarScale(_size);
  173. ComputeAvatarVolumeAndMass();
  174. DetailLog("{0},BSCharacter.setSize,call,size={1},scale={2},density={3},volume={4},mass={5}",
  175. LocalID, _size, Scale, Density, _avatarVolume, RawMass);
  176. PhysScene.TaintedObject(LocalID, "BSCharacter.setSize", delegate()
  177. {
  178. if (PhysBody.HasPhysicalBody && PhysShape.physShapeInfo.HasPhysicalShape)
  179. {
  180. PhysScene.PE.SetLocalScaling(PhysShape.physShapeInfo, Scale);
  181. UpdatePhysicalMassProperties(RawMass, true);
  182. // Adjust the avatar's position to account for the increase/decrease in size
  183. ForcePosition = new OMV.Vector3(RawPosition.X, RawPosition.Y, RawPosition.Z + heightChange / 2f);
  184. // Make sure this change appears as a property update event
  185. PhysScene.PE.PushUpdate(PhysBody);
  186. }
  187. });
  188. }
  189. }
  190. public override PrimitiveBaseShape Shape
  191. {
  192. set { BaseShape = value; }
  193. }
  194. public override bool Grabbed {
  195. set { _grabbed = value; }
  196. }
  197. public override bool Selected {
  198. set { _selected = value; }
  199. }
  200. public override bool IsSelected
  201. {
  202. get { return _selected; }
  203. }
  204. public override void CrossingFailure() { return; }
  205. public override void link(PhysicsActor obj) { return; }
  206. public override void delink() { return; }
  207. // Set motion values to zero.
  208. // Do it to the properties so the values get set in the physics engine.
  209. // Push the setting of the values to the viewer.
  210. // Called at taint time!
  211. public override void ZeroMotion(bool inTaintTime)
  212. {
  213. RawVelocity = OMV.Vector3.Zero;
  214. _acceleration = OMV.Vector3.Zero;
  215. _rotationalVelocity = OMV.Vector3.Zero;
  216. // Zero some other properties directly into the physics engine
  217. PhysScene.TaintedObject(inTaintTime, LocalID, "BSCharacter.ZeroMotion", delegate()
  218. {
  219. if (PhysBody.HasPhysicalBody)
  220. PhysScene.PE.ClearAllForces(PhysBody);
  221. });
  222. }
  223. public override void ZeroAngularMotion(bool inTaintTime)
  224. {
  225. _rotationalVelocity = OMV.Vector3.Zero;
  226. PhysScene.TaintedObject(inTaintTime, LocalID, "BSCharacter.ZeroMotion", delegate()
  227. {
  228. if (PhysBody.HasPhysicalBody)
  229. {
  230. PhysScene.PE.SetInterpolationAngularVelocity(PhysBody, OMV.Vector3.Zero);
  231. PhysScene.PE.SetAngularVelocity(PhysBody, OMV.Vector3.Zero);
  232. // The next also get rid of applied linear force but the linear velocity is untouched.
  233. PhysScene.PE.ClearForces(PhysBody);
  234. }
  235. });
  236. }
  237. public override void LockAngularMotion(OMV.Vector3 axis) { return; }
  238. public override OMV.Vector3 Position {
  239. get {
  240. // Don't refetch the position because this function is called a zillion times
  241. // RawPosition = PhysicsScene.PE.GetObjectPosition(Scene.World, LocalID);
  242. return RawPosition;
  243. }
  244. set {
  245. RawPosition = value;
  246. PhysScene.TaintedObject(LocalID, "BSCharacter.setPosition", delegate()
  247. {
  248. DetailLog("{0},BSCharacter.SetPosition,taint,pos={1},orient={2}", LocalID, RawPosition, RawOrientation);
  249. PositionSanityCheck();
  250. ForcePosition = RawPosition;
  251. });
  252. }
  253. }
  254. public override OMV.Vector3 ForcePosition {
  255. get {
  256. RawPosition = PhysScene.PE.GetPosition(PhysBody);
  257. return RawPosition;
  258. }
  259. set {
  260. RawPosition = value;
  261. if (PhysBody.HasPhysicalBody)
  262. {
  263. PhysScene.PE.SetTranslation(PhysBody, RawPosition, RawOrientation);
  264. }
  265. }
  266. }
  267. // Check that the current position is sane and, if not, modify the position to make it so.
  268. // Check for being below terrain or on water.
  269. // Returns 'true' of the position was made sane by some action.
  270. private bool PositionSanityCheck()
  271. {
  272. bool ret = false;
  273. // TODO: check for out of bounds
  274. if (!PhysScene.TerrainManager.IsWithinKnownTerrain(RawPosition))
  275. {
  276. // The character is out of the known/simulated area.
  277. // Force the avatar position to be within known. ScenePresence will use the position
  278. // plus the velocity to decide if the avatar is moving out of the region.
  279. RawPosition = PhysScene.TerrainManager.ClampPositionIntoKnownTerrain(RawPosition);
  280. DetailLog("{0},BSCharacter.PositionSanityCheck,notWithinKnownTerrain,clampedPos={1}", LocalID, RawPosition);
  281. return true;
  282. }
  283. // If below the ground, move the avatar up
  284. float terrainHeight = PhysScene.TerrainManager.GetTerrainHeightAtXYZ(RawPosition);
  285. if (Position.Z < terrainHeight)
  286. {
  287. DetailLog("{0},BSCharacter.PositionSanityCheck,adjustForUnderGround,pos={1},terrain={2}", LocalID, RawPosition, terrainHeight);
  288. RawPosition = new OMV.Vector3(RawPosition.X, RawPosition.Y, terrainHeight + BSParam.AvatarBelowGroundUpCorrectionMeters);
  289. ret = true;
  290. }
  291. if ((CurrentCollisionFlags & CollisionFlags.BS_FLOATS_ON_WATER) != 0)
  292. {
  293. float waterHeight = PhysScene.TerrainManager.GetWaterLevelAtXYZ(RawPosition);
  294. if (Position.Z < waterHeight)
  295. {
  296. RawPosition = new OMV.Vector3(RawPosition.X, RawPosition.Y, waterHeight);
  297. ret = true;
  298. }
  299. }
  300. return ret;
  301. }
  302. // A version of the sanity check that also makes sure a new position value is
  303. // pushed back to the physics engine. This routine would be used by anyone
  304. // who is not already pushing the value.
  305. private bool PositionSanityCheck(bool inTaintTime)
  306. {
  307. bool ret = false;
  308. if (PositionSanityCheck())
  309. {
  310. // The new position value must be pushed into the physics engine but we can't
  311. // just assign to "Position" because of potential call loops.
  312. PhysScene.TaintedObject(inTaintTime, LocalID, "BSCharacter.PositionSanityCheck", delegate()
  313. {
  314. DetailLog("{0},BSCharacter.PositionSanityCheck,taint,pos={1},orient={2}", LocalID, RawPosition, RawOrientation);
  315. ForcePosition = RawPosition;
  316. });
  317. ret = true;
  318. }
  319. return ret;
  320. }
  321. public override float Mass { get { return _mass; } }
  322. // used when we only want this prim's mass and not the linkset thing
  323. public override float RawMass {
  324. get {return _mass; }
  325. }
  326. public override void UpdatePhysicalMassProperties(float physMass, bool inWorld)
  327. {
  328. OMV.Vector3 localInertia = PhysScene.PE.CalculateLocalInertia(PhysShape.physShapeInfo, physMass);
  329. PhysScene.PE.SetMassProps(PhysBody, physMass, localInertia);
  330. }
  331. public override OMV.Vector3 Force {
  332. get { return RawForce; }
  333. set {
  334. RawForce = value;
  335. // m_log.DebugFormat("{0}: Force = {1}", LogHeader, _force);
  336. PhysScene.TaintedObject(LocalID, "BSCharacter.SetForce", delegate()
  337. {
  338. DetailLog("{0},BSCharacter.setForce,taint,force={1}", LocalID, RawForce);
  339. if (PhysBody.HasPhysicalBody)
  340. PhysScene.PE.SetObjectForce(PhysBody, RawForce);
  341. });
  342. }
  343. }
  344. // Avatars don't do vehicles
  345. public override int VehicleType { get { return (int)Vehicle.TYPE_NONE; } set { return; } }
  346. public override void VehicleFloatParam(int param, float value) { }
  347. public override void VehicleVectorParam(int param, OMV.Vector3 value) {}
  348. public override void VehicleRotationParam(int param, OMV.Quaternion rotation) { }
  349. public override void VehicleFlags(int param, bool remove) { }
  350. // Allows the detection of collisions with inherently non-physical prims. see llVolumeDetect for more
  351. public override void SetVolumeDetect(int param) { return; }
  352. public override bool IsVolumeDetect { get { return false; } }
  353. public override OMV.Vector3 GeometricCenter { get { return OMV.Vector3.Zero; } }
  354. public override OMV.Vector3 CenterOfMass { get { return OMV.Vector3.Zero; } }
  355. // Sets the target in the motor. This starts the changing of the avatar's velocity.
  356. public override OMV.Vector3 TargetVelocity
  357. {
  358. get
  359. {
  360. return base.m_targetVelocity;
  361. }
  362. set
  363. {
  364. DetailLog("{0},BSCharacter.setTargetVelocity,call,vel={1}", LocalID, value);
  365. m_targetVelocity = value;
  366. OMV.Vector3 targetVel = value;
  367. if (_setAlwaysRun && !_flying)
  368. targetVel *= new OMV.Vector3(BSParam.AvatarAlwaysRunFactor, BSParam.AvatarAlwaysRunFactor, 0f);
  369. if (m_moveActor != null)
  370. m_moveActor.SetVelocityAndTarget(RawVelocity, targetVel, false /* inTaintTime */);
  371. }
  372. }
  373. // Directly setting velocity means this is what the user really wants now.
  374. public override OMV.Vector3 Velocity {
  375. get { return RawVelocity; }
  376. set {
  377. RawVelocity = value;
  378. // m_log.DebugFormat("{0}: set velocity = {1}", LogHeader, RawVelocity);
  379. PhysScene.TaintedObject(LocalID, "BSCharacter.setVelocity", delegate()
  380. {
  381. if (m_moveActor != null)
  382. m_moveActor.SetVelocityAndTarget(RawVelocity, RawVelocity, true /* inTaintTime */);
  383. DetailLog("{0},BSCharacter.setVelocity,taint,vel={1}", LocalID, RawVelocity);
  384. ForceVelocity = RawVelocity;
  385. });
  386. }
  387. }
  388. public override OMV.Vector3 ForceVelocity {
  389. get { return RawVelocity; }
  390. set {
  391. PhysScene.AssertInTaintTime("BSCharacter.ForceVelocity");
  392. RawVelocity = value;
  393. PhysScene.PE.SetLinearVelocity(PhysBody, RawVelocity);
  394. PhysScene.PE.Activate(PhysBody, true);
  395. }
  396. }
  397. public override OMV.Vector3 Torque {
  398. get { return RawTorque; }
  399. set { RawTorque = value;
  400. }
  401. }
  402. public override float CollisionScore {
  403. get { return _collisionScore; }
  404. set { _collisionScore = value;
  405. }
  406. }
  407. public override OMV.Vector3 Acceleration {
  408. get { return _acceleration; }
  409. set { _acceleration = value; }
  410. }
  411. public override OMV.Quaternion Orientation {
  412. get { return RawOrientation; }
  413. set {
  414. // Orientation is set zillions of times when an avatar is walking. It's like
  415. // the viewer doesn't trust us.
  416. if (RawOrientation != value)
  417. {
  418. RawOrientation = value;
  419. PhysScene.TaintedObject(LocalID, "BSCharacter.setOrientation", delegate()
  420. {
  421. // Bullet assumes we know what we are doing when forcing orientation
  422. // so it lets us go against all the rules and just compensates for them later.
  423. // This forces rotation to be only around the Z axis and doesn't change any of the other axis.
  424. // This keeps us from flipping the capsule over which the veiwer does not understand.
  425. float oRoll, oPitch, oYaw;
  426. RawOrientation.GetEulerAngles(out oRoll, out oPitch, out oYaw);
  427. OMV.Quaternion trimmedOrientation = OMV.Quaternion.CreateFromEulers(0f, 0f, oYaw);
  428. // DetailLog("{0},BSCharacter.setOrientation,taint,val={1},valDir={2},conv={3},convDir={4}",
  429. // LocalID, RawOrientation, OMV.Vector3.UnitX * RawOrientation,
  430. // trimmedOrientation, OMV.Vector3.UnitX * trimmedOrientation);
  431. ForceOrientation = trimmedOrientation;
  432. });
  433. }
  434. }
  435. }
  436. // Go directly to Bullet to get/set the value.
  437. public override OMV.Quaternion ForceOrientation
  438. {
  439. get
  440. {
  441. RawOrientation = PhysScene.PE.GetOrientation(PhysBody);
  442. return RawOrientation;
  443. }
  444. set
  445. {
  446. RawOrientation = value;
  447. if (PhysBody.HasPhysicalBody)
  448. {
  449. // RawPosition = PhysicsScene.PE.GetPosition(BSBody);
  450. PhysScene.PE.SetTranslation(PhysBody, RawPosition, RawOrientation);
  451. }
  452. }
  453. }
  454. public override int PhysicsActorType {
  455. get { return _physicsActorType; }
  456. set { _physicsActorType = value;
  457. }
  458. }
  459. public override bool IsPhysical {
  460. get { return _isPhysical; }
  461. set { _isPhysical = value;
  462. }
  463. }
  464. public override bool IsSolid {
  465. get { return true; }
  466. }
  467. public override bool IsStatic {
  468. get { return false; }
  469. }
  470. public override bool IsPhysicallyActive {
  471. get { return true; }
  472. }
  473. public override bool Flying {
  474. get { return _flying; }
  475. set {
  476. _flying = value;
  477. // simulate flying by changing the effect of gravity
  478. Buoyancy = ComputeBuoyancyFromFlying(_flying);
  479. }
  480. }
  481. // Flying is implimented by changing the avatar's buoyancy.
  482. // Would this be done better with a vehicle type?
  483. private float ComputeBuoyancyFromFlying(bool ifFlying) {
  484. return ifFlying ? 1f : 0f;
  485. }
  486. public override bool
  487. SetAlwaysRun {
  488. get { return _setAlwaysRun; }
  489. set { _setAlwaysRun = value; }
  490. }
  491. public override bool ThrottleUpdates {
  492. get { return _throttleUpdates; }
  493. set { _throttleUpdates = value; }
  494. }
  495. public override bool FloatOnWater {
  496. set {
  497. _floatOnWater = value;
  498. PhysScene.TaintedObject(LocalID, "BSCharacter.setFloatOnWater", delegate()
  499. {
  500. if (PhysBody.HasPhysicalBody)
  501. {
  502. if (_floatOnWater)
  503. CurrentCollisionFlags = PhysScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
  504. else
  505. CurrentCollisionFlags = PhysScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
  506. }
  507. });
  508. }
  509. }
  510. public override OMV.Vector3 RotationalVelocity {
  511. get { return _rotationalVelocity; }
  512. set { _rotationalVelocity = value; }
  513. }
  514. public override OMV.Vector3 ForceRotationalVelocity {
  515. get { return _rotationalVelocity; }
  516. set { _rotationalVelocity = value; }
  517. }
  518. public override bool Kinematic {
  519. get { return _kinematic; }
  520. set { _kinematic = value; }
  521. }
  522. // neg=fall quickly, 0=1g, 1=0g, pos=float up
  523. public override float Buoyancy {
  524. get { return _buoyancy; }
  525. set { _buoyancy = value;
  526. PhysScene.TaintedObject(LocalID, "BSCharacter.setBuoyancy", delegate()
  527. {
  528. DetailLog("{0},BSCharacter.setBuoyancy,taint,buoy={1}", LocalID, _buoyancy);
  529. ForceBuoyancy = _buoyancy;
  530. });
  531. }
  532. }
  533. public override float ForceBuoyancy {
  534. get { return _buoyancy; }
  535. set {
  536. PhysScene.AssertInTaintTime("BSCharacter.ForceBuoyancy");
  537. _buoyancy = value;
  538. DetailLog("{0},BSCharacter.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy);
  539. // Buoyancy is faked by changing the gravity applied to the object
  540. float grav = BSParam.Gravity * (1f - _buoyancy);
  541. Gravity = new OMV.Vector3(0f, 0f, grav);
  542. if (PhysBody.HasPhysicalBody)
  543. PhysScene.PE.SetGravity(PhysBody, Gravity);
  544. }
  545. }
  546. // Used for MoveTo
  547. public override OMV.Vector3 PIDTarget {
  548. set { _PIDTarget = value; }
  549. }
  550. public override bool PIDActive {
  551. set { _usePID = value; }
  552. }
  553. public override float PIDTau {
  554. set { _PIDTau = value; }
  555. }
  556. public override void AddForce(OMV.Vector3 force, bool pushforce)
  557. {
  558. // Since this force is being applied in only one step, make this a force per second.
  559. OMV.Vector3 addForce = force / PhysScene.LastTimeStep;
  560. AddForce(addForce, pushforce, false);
  561. }
  562. public override void AddForce(OMV.Vector3 force, bool pushforce, bool inTaintTime) {
  563. if (force.IsFinite())
  564. {
  565. OMV.Vector3 addForce = Util.ClampV(force, BSParam.MaxAddForceMagnitude);
  566. // DetailLog("{0},BSCharacter.addForce,call,force={1}", LocalID, addForce);
  567. PhysScene.TaintedObject(inTaintTime, LocalID, "BSCharacter.AddForce", delegate()
  568. {
  569. // Bullet adds this central force to the total force for this tick
  570. // DetailLog("{0},BSCharacter.addForce,taint,force={1}", LocalID, addForce);
  571. if (PhysBody.HasPhysicalBody)
  572. {
  573. PhysScene.PE.ApplyCentralForce(PhysBody, addForce);
  574. }
  575. });
  576. }
  577. else
  578. {
  579. m_log.WarnFormat("{0}: Got a NaN force applied to a character. LocalID={1}", LogHeader, LocalID);
  580. return;
  581. }
  582. }
  583. public override void AddAngularForce(OMV.Vector3 force, bool pushforce, bool inTaintTime) {
  584. }
  585. public override void SetMomentum(OMV.Vector3 momentum) {
  586. }
  587. private OMV.Vector3 ComputeAvatarScale(OMV.Vector3 size)
  588. {
  589. OMV.Vector3 newScale;
  590. // Bullet's capsule total height is the "passed height + radius * 2";
  591. // The base capsule is 1 unit in diameter and 2 units in height (passed radius=0.5, passed height = 1)
  592. // The number we pass in for 'scaling' is the multiplier to get that base
  593. // shape to be the size desired.
  594. // So, when creating the scale for the avatar height, we take the passed height
  595. // (size.Z) and remove the caps.
  596. // An oddity of the Bullet capsule implementation is that it presumes the Y
  597. // dimension is the radius of the capsule. Even though some of the code allows
  598. // for a asymmetrical capsule, other parts of the code presume it is cylindrical.
  599. // Scale is multiplier of radius with one of "0.5"
  600. newScale.X = size.X / 2f;
  601. newScale.Y = size.Y / 2f;
  602. float heightAdjust = BSParam.AvatarHeightMidFudge;
  603. if (BSParam.AvatarHeightLowFudge != 0f || BSParam.AvatarHeightHighFudge != 0f)
  604. {
  605. const float AVATAR_LOW = 1.1f;
  606. const float AVATAR_MID = 1.775f; // 1.87f
  607. const float AVATAR_HI = 2.45f;
  608. // An avatar is between 1.1 and 2.45 meters. Midpoint is 1.775m.
  609. float midHeightOffset = size.Z - AVATAR_MID;
  610. if (midHeightOffset < 0f)
  611. {
  612. // Small avatar. Add the adjustment based on the distance from midheight
  613. heightAdjust += ((-1f * midHeightOffset) / (AVATAR_MID - AVATAR_LOW)) * BSParam.AvatarHeightLowFudge;
  614. }
  615. else
  616. {
  617. // Large avatar. Add the adjustment based on the distance from midheight
  618. heightAdjust += ((midHeightOffset) / (AVATAR_HI - AVATAR_MID)) * BSParam.AvatarHeightHighFudge;
  619. }
  620. }
  621. // The total scale height is the central cylindar plus the caps on the two ends.
  622. newScale.Z = (size.Z + (Math.Min(size.X, size.Y) * 2) + heightAdjust) / 2f;
  623. // m_log.DebugFormat("{0} ComputeAvatarScale: size={1},adj={2},scale={3}", LogHeader, size, heightAdjust, newScale);
  624. // If smaller than the endcaps, just fake like we're almost that small
  625. if (newScale.Z < 0)
  626. newScale.Z = 0.1f;
  627. DetailLog("{0},BSCharacter.ComputerAvatarScale,size={1},lowF={2},midF={3},hiF={4},adj={5},newScale={6}",
  628. LocalID, size, BSParam.AvatarHeightLowFudge, BSParam.AvatarHeightMidFudge, BSParam.AvatarHeightHighFudge, heightAdjust, newScale);
  629. return newScale;
  630. }
  631. // set _avatarVolume and _mass based on capsule size, _density and Scale
  632. private void ComputeAvatarVolumeAndMass()
  633. {
  634. _avatarVolume = (float)(
  635. Math.PI
  636. * Size.X / 2f
  637. * Size.Y / 2f // the area of capsule cylinder
  638. * Size.Z // times height of capsule cylinder
  639. + 1.33333333f
  640. * Math.PI
  641. * Size.X / 2f
  642. * Math.Min(Size.X, Size.Y) / 2
  643. * Size.Y / 2f // plus the volume of the capsule end caps
  644. );
  645. _mass = Density * BSParam.DensityScaleFactor * _avatarVolume;
  646. }
  647. // The physics engine says that properties have updated. Update same and inform
  648. // the world that things have changed.
  649. public override void UpdateProperties(EntityProperties entprop)
  650. {
  651. // Let anyone (like the actors) modify the updated properties before they are pushed into the object and the simulator.
  652. TriggerPreUpdatePropertyAction(ref entprop);
  653. RawPosition = entprop.Position;
  654. RawOrientation = entprop.Rotation;
  655. // Smooth velocity. OpenSimulator is VERY sensitive to changes in velocity of the avatar
  656. // and will send agent updates to the clients if velocity changes by more than
  657. // 0.001m/s. Bullet introduces a lot of jitter in the velocity which causes many
  658. // extra updates.
  659. if (!entprop.Velocity.ApproxEquals(RawVelocity, 0.1f))
  660. RawVelocity = entprop.Velocity;
  661. _acceleration = entprop.Acceleration;
  662. _rotationalVelocity = entprop.RotationalVelocity;
  663. // Do some sanity checking for the avatar. Make sure it's above ground and inbounds.
  664. if (PositionSanityCheck(true))
  665. {
  666. DetailLog("{0},BSCharacter.UpdateProperties,updatePosForSanity,pos={1}", LocalID, RawPosition);
  667. entprop.Position = RawPosition;
  668. }
  669. // remember the current and last set values
  670. LastEntityProperties = CurrentEntityProperties;
  671. CurrentEntityProperties = entprop;
  672. // Tell the linkset about value changes
  673. // Linkset.UpdateProperties(UpdatedProperties.EntPropUpdates, this);
  674. // Avatars don't report their changes the usual way. Changes are checked for in the heartbeat loop.
  675. // PhysScene.PostUpdate(this);
  676. DetailLog("{0},BSCharacter.UpdateProperties,call,pos={1},orient={2},vel={3},accel={4},rotVel={5}",
  677. LocalID, RawPosition, RawOrientation, RawVelocity, _acceleration, _rotationalVelocity);
  678. }
  679. }
  680. }