BSCharacter.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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 OMV.Vector3 _position;
  45. private float _mass;
  46. private float _avatarVolume;
  47. private float _collisionScore;
  48. private OMV.Vector3 _acceleration;
  49. private OMV.Quaternion _orientation;
  50. private int _physicsActorType;
  51. private bool _isPhysical;
  52. private bool _flying;
  53. private bool _setAlwaysRun;
  54. private bool _throttleUpdates;
  55. private bool _floatOnWater;
  56. private OMV.Vector3 _rotationalVelocity;
  57. private bool _kinematic;
  58. private float _buoyancy;
  59. private BSActorAvatarMove m_moveActor;
  60. private const string AvatarMoveActorName = "BSCharacter.AvatarMove";
  61. private OMV.Vector3 _PIDTarget;
  62. private bool _usePID;
  63. private float _PIDTau;
  64. public BSCharacter(uint localID, String avName, BSScene parent_scene, OMV.Vector3 pos, OMV.Vector3 size, bool isFlying)
  65. : base(parent_scene, localID, avName, "BSCharacter")
  66. {
  67. _physicsActorType = (int)ActorTypes.Agent;
  68. _position = pos;
  69. _flying = isFlying;
  70. _orientation = OMV.Quaternion.Identity;
  71. RawVelocity = OMV.Vector3.Zero;
  72. _buoyancy = ComputeBuoyancyFromFlying(isFlying);
  73. Friction = BSParam.AvatarStandingFriction;
  74. Density = BSParam.AvatarDensity / BSParam.DensityScaleFactor;
  75. // Old versions of ScenePresence passed only the height. If width and/or depth are zero,
  76. // replace with the default values.
  77. _size = size;
  78. if (_size.X == 0f) _size.X = BSParam.AvatarCapsuleDepth;
  79. if (_size.Y == 0f) _size.Y = BSParam.AvatarCapsuleWidth;
  80. // The dimensions of the physical capsule are kept in the scale.
  81. // Physics creates a unit capsule which is scaled by the physics engine.
  82. Scale = ComputeAvatarScale(_size);
  83. // set _avatarVolume and _mass based on capsule size, _density and Scale
  84. ComputeAvatarVolumeAndMass();
  85. // The avatar's movement is controlled by this motor that speeds up and slows down
  86. // the avatar seeking to reach the motor's target speed.
  87. // This motor runs as a prestep action for the avatar so it will keep the avatar
  88. // standing as well as moving. Destruction of the avatar will destroy the pre-step action.
  89. m_moveActor = new BSActorAvatarMove(PhysScene, this, AvatarMoveActorName);
  90. PhysicalActors.Add(AvatarMoveActorName, m_moveActor);
  91. DetailLog("{0},BSCharacter.create,call,size={1},scale={2},density={3},volume={4},mass={5}",
  92. LocalID, _size, Scale, Density, _avatarVolume, RawMass);
  93. // do actual creation in taint time
  94. PhysScene.TaintedObject("BSCharacter.create", delegate()
  95. {
  96. DetailLog("{0},BSCharacter.create,taint", LocalID);
  97. // New body and shape into PhysBody and PhysShape
  98. PhysScene.Shapes.GetBodyAndShape(true, PhysScene.World, this);
  99. SetPhysicalProperties();
  100. });
  101. return;
  102. }
  103. // called when this character is being destroyed and the resources should be released
  104. public override void Destroy()
  105. {
  106. base.Destroy();
  107. DetailLog("{0},BSCharacter.Destroy", LocalID);
  108. PhysScene.TaintedObject("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 = _position;
  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. _size = value;
  165. // Old versions of ScenePresence passed only the height. If width and/or depth are zero,
  166. // replace with the default values.
  167. if (_size.X == 0f) _size.X = BSParam.AvatarCapsuleDepth;
  168. if (_size.Y == 0f) _size.Y = BSParam.AvatarCapsuleWidth;
  169. Scale = ComputeAvatarScale(_size);
  170. ComputeAvatarVolumeAndMass();
  171. DetailLog("{0},BSCharacter.setSize,call,size={1},scale={2},density={3},volume={4},mass={5}",
  172. LocalID, _size, Scale, Density, _avatarVolume, RawMass);
  173. PhysScene.TaintedObject("BSCharacter.setSize", delegate()
  174. {
  175. if (PhysBody.HasPhysicalBody && PhysShape.physShapeInfo.HasPhysicalShape)
  176. {
  177. PhysScene.PE.SetLocalScaling(PhysShape.physShapeInfo, Scale);
  178. UpdatePhysicalMassProperties(RawMass, true);
  179. // Make sure this change appears as a property update event
  180. PhysScene.PE.PushUpdate(PhysBody);
  181. }
  182. });
  183. }
  184. }
  185. public override PrimitiveBaseShape Shape
  186. {
  187. set { BaseShape = value; }
  188. }
  189. public override bool Grabbed {
  190. set { _grabbed = value; }
  191. }
  192. public override bool Selected {
  193. set { _selected = value; }
  194. }
  195. public override bool IsSelected
  196. {
  197. get { return _selected; }
  198. }
  199. public override void CrossingFailure() { return; }
  200. public override void link(PhysicsActor obj) { return; }
  201. public override void delink() { return; }
  202. // Set motion values to zero.
  203. // Do it to the properties so the values get set in the physics engine.
  204. // Push the setting of the values to the viewer.
  205. // Called at taint time!
  206. public override void ZeroMotion(bool inTaintTime)
  207. {
  208. RawVelocity = OMV.Vector3.Zero;
  209. _acceleration = OMV.Vector3.Zero;
  210. _rotationalVelocity = OMV.Vector3.Zero;
  211. // Zero some other properties directly into the physics engine
  212. PhysScene.TaintedObject(inTaintTime, "BSCharacter.ZeroMotion", delegate()
  213. {
  214. if (PhysBody.HasPhysicalBody)
  215. PhysScene.PE.ClearAllForces(PhysBody);
  216. });
  217. }
  218. public override void ZeroAngularMotion(bool inTaintTime)
  219. {
  220. _rotationalVelocity = OMV.Vector3.Zero;
  221. PhysScene.TaintedObject(inTaintTime, "BSCharacter.ZeroMotion", delegate()
  222. {
  223. if (PhysBody.HasPhysicalBody)
  224. {
  225. PhysScene.PE.SetInterpolationAngularVelocity(PhysBody, OMV.Vector3.Zero);
  226. PhysScene.PE.SetAngularVelocity(PhysBody, OMV.Vector3.Zero);
  227. // The next also get rid of applied linear force but the linear velocity is untouched.
  228. PhysScene.PE.ClearForces(PhysBody);
  229. }
  230. });
  231. }
  232. public override void LockAngularMotion(OMV.Vector3 axis) { return; }
  233. public override OMV.Vector3 RawPosition
  234. {
  235. get { return _position; }
  236. set { _position = value; }
  237. }
  238. public override OMV.Vector3 Position {
  239. get {
  240. // Don't refetch the position because this function is called a zillion times
  241. // _position = PhysicsScene.PE.GetObjectPosition(Scene.World, LocalID);
  242. return _position;
  243. }
  244. set {
  245. _position = value;
  246. PhysScene.TaintedObject("BSCharacter.setPosition", delegate()
  247. {
  248. DetailLog("{0},BSCharacter.SetPosition,taint,pos={1},orient={2}", LocalID, _position, _orientation);
  249. PositionSanityCheck();
  250. ForcePosition = _position;
  251. });
  252. }
  253. }
  254. public override OMV.Vector3 ForcePosition {
  255. get {
  256. _position = PhysScene.PE.GetPosition(PhysBody);
  257. return _position;
  258. }
  259. set {
  260. _position = value;
  261. if (PhysBody.HasPhysicalBody)
  262. {
  263. PhysScene.PE.SetTranslation(PhysBody, _position, _orientation);
  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, _position, terrainHeight);
  288. _position.Z = terrainHeight + BSParam.AvatarBelowGroundUpCorrectionMeters;
  289. ret = true;
  290. }
  291. if ((CurrentCollisionFlags & CollisionFlags.BS_FLOATS_ON_WATER) != 0)
  292. {
  293. float waterHeight = PhysScene.TerrainManager.GetWaterLevelAtXYZ(_position);
  294. if (Position.Z < waterHeight)
  295. {
  296. _position.Z = 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, "BSCharacter.PositionSanityCheck", delegate()
  313. {
  314. DetailLog("{0},BSCharacter.PositionSanityCheck,taint,pos={1},orient={2}", LocalID, _position, _orientation);
  315. ForcePosition = _position;
  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("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 OMV.Vector3 GeometricCenter { get { return OMV.Vector3.Zero; } }
  353. public override OMV.Vector3 CenterOfMass { get { return OMV.Vector3.Zero; } }
  354. // Sets the target in the motor. This starts the changing of the avatar's velocity.
  355. public override OMV.Vector3 TargetVelocity
  356. {
  357. get
  358. {
  359. return base.m_targetVelocity;
  360. }
  361. set
  362. {
  363. DetailLog("{0},BSCharacter.setTargetVelocity,call,vel={1}", LocalID, value);
  364. m_targetVelocity = value;
  365. OMV.Vector3 targetVel = value;
  366. if (_setAlwaysRun && !_flying)
  367. targetVel *= new OMV.Vector3(BSParam.AvatarAlwaysRunFactor, BSParam.AvatarAlwaysRunFactor, 0f);
  368. if (m_moveActor != null)
  369. m_moveActor.SetVelocityAndTarget(RawVelocity, targetVel, false /* inTaintTime */);
  370. }
  371. }
  372. // Directly setting velocity means this is what the user really wants now.
  373. public override OMV.Vector3 Velocity {
  374. get { return RawVelocity; }
  375. set {
  376. RawVelocity = value;
  377. // m_log.DebugFormat("{0}: set velocity = {1}", LogHeader, RawVelocity);
  378. PhysScene.TaintedObject("BSCharacter.setVelocity", delegate()
  379. {
  380. if (m_moveActor != null)
  381. m_moveActor.SetVelocityAndTarget(RawVelocity, RawVelocity, true /* inTaintTime */);
  382. DetailLog("{0},BSCharacter.setVelocity,taint,vel={1}", LocalID, RawVelocity);
  383. ForceVelocity = RawVelocity;
  384. });
  385. }
  386. }
  387. public override OMV.Vector3 ForceVelocity {
  388. get { return RawVelocity; }
  389. set {
  390. PhysScene.AssertInTaintTime("BSCharacter.ForceVelocity");
  391. RawVelocity = value;
  392. PhysScene.PE.SetLinearVelocity(PhysBody, RawVelocity);
  393. PhysScene.PE.Activate(PhysBody, true);
  394. }
  395. }
  396. public override OMV.Vector3 Torque {
  397. get { return RawTorque; }
  398. set { RawTorque = value;
  399. }
  400. }
  401. public override float CollisionScore {
  402. get { return _collisionScore; }
  403. set { _collisionScore = value;
  404. }
  405. }
  406. public override OMV.Vector3 Acceleration {
  407. get { return _acceleration; }
  408. set { _acceleration = value; }
  409. }
  410. public override OMV.Quaternion RawOrientation
  411. {
  412. get { return _orientation; }
  413. set { _orientation = value; }
  414. }
  415. public override OMV.Quaternion Orientation {
  416. get { return _orientation; }
  417. set {
  418. // Orientation is set zillions of times when an avatar is walking. It's like
  419. // the viewer doesn't trust us.
  420. if (_orientation != value)
  421. {
  422. _orientation = value;
  423. PhysScene.TaintedObject("BSCharacter.setOrientation", delegate()
  424. {
  425. // Bullet assumes we know what we are doing when forcing orientation
  426. // so it lets us go against all the rules and just compensates for them later.
  427. // This forces rotation to be only around the Z axis and doesn't change any of the other axis.
  428. // This keeps us from flipping the capsule over which the veiwer does not understand.
  429. float oRoll, oPitch, oYaw;
  430. _orientation.GetEulerAngles(out oRoll, out oPitch, out oYaw);
  431. OMV.Quaternion trimmedOrientation = OMV.Quaternion.CreateFromEulers(0f, 0f, oYaw);
  432. // DetailLog("{0},BSCharacter.setOrientation,taint,val={1},valDir={2},conv={3},convDir={4}",
  433. // LocalID, _orientation, OMV.Vector3.UnitX * _orientation,
  434. // trimmedOrientation, OMV.Vector3.UnitX * trimmedOrientation);
  435. ForceOrientation = trimmedOrientation;
  436. });
  437. }
  438. }
  439. }
  440. // Go directly to Bullet to get/set the value.
  441. public override OMV.Quaternion ForceOrientation
  442. {
  443. get
  444. {
  445. _orientation = PhysScene.PE.GetOrientation(PhysBody);
  446. return _orientation;
  447. }
  448. set
  449. {
  450. _orientation = value;
  451. if (PhysBody.HasPhysicalBody)
  452. {
  453. // _position = PhysicsScene.PE.GetPosition(BSBody);
  454. PhysScene.PE.SetTranslation(PhysBody, _position, _orientation);
  455. }
  456. }
  457. }
  458. public override int PhysicsActorType {
  459. get { return _physicsActorType; }
  460. set { _physicsActorType = value;
  461. }
  462. }
  463. public override bool IsPhysical {
  464. get { return _isPhysical; }
  465. set { _isPhysical = value;
  466. }
  467. }
  468. public override bool IsSolid {
  469. get { return true; }
  470. }
  471. public override bool IsStatic {
  472. get { return false; }
  473. }
  474. public override bool IsPhysicallyActive {
  475. get { return true; }
  476. }
  477. public override bool Flying {
  478. get { return _flying; }
  479. set {
  480. _flying = value;
  481. // simulate flying by changing the effect of gravity
  482. Buoyancy = ComputeBuoyancyFromFlying(_flying);
  483. }
  484. }
  485. // Flying is implimented by changing the avatar's buoyancy.
  486. // Would this be done better with a vehicle type?
  487. private float ComputeBuoyancyFromFlying(bool ifFlying) {
  488. return ifFlying ? 1f : 0f;
  489. }
  490. public override bool
  491. SetAlwaysRun {
  492. get { return _setAlwaysRun; }
  493. set { _setAlwaysRun = value; }
  494. }
  495. public override bool ThrottleUpdates {
  496. get { return _throttleUpdates; }
  497. set { _throttleUpdates = value; }
  498. }
  499. public override bool FloatOnWater {
  500. set {
  501. _floatOnWater = value;
  502. PhysScene.TaintedObject("BSCharacter.setFloatOnWater", delegate()
  503. {
  504. if (PhysBody.HasPhysicalBody)
  505. {
  506. if (_floatOnWater)
  507. CurrentCollisionFlags = PhysScene.PE.AddToCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
  508. else
  509. CurrentCollisionFlags = PhysScene.PE.RemoveFromCollisionFlags(PhysBody, CollisionFlags.BS_FLOATS_ON_WATER);
  510. }
  511. });
  512. }
  513. }
  514. public override OMV.Vector3 RotationalVelocity {
  515. get { return _rotationalVelocity; }
  516. set { _rotationalVelocity = value; }
  517. }
  518. public override OMV.Vector3 ForceRotationalVelocity {
  519. get { return _rotationalVelocity; }
  520. set { _rotationalVelocity = value; }
  521. }
  522. public override bool Kinematic {
  523. get { return _kinematic; }
  524. set { _kinematic = value; }
  525. }
  526. // neg=fall quickly, 0=1g, 1=0g, pos=float up
  527. public override float Buoyancy {
  528. get { return _buoyancy; }
  529. set { _buoyancy = value;
  530. PhysScene.TaintedObject("BSCharacter.setBuoyancy", delegate()
  531. {
  532. DetailLog("{0},BSCharacter.setBuoyancy,taint,buoy={1}", LocalID, _buoyancy);
  533. ForceBuoyancy = _buoyancy;
  534. });
  535. }
  536. }
  537. public override float ForceBuoyancy {
  538. get { return _buoyancy; }
  539. set {
  540. PhysScene.AssertInTaintTime("BSCharacter.ForceBuoyancy");
  541. _buoyancy = value;
  542. DetailLog("{0},BSCharacter.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy);
  543. // Buoyancy is faked by changing the gravity applied to the object
  544. float grav = BSParam.Gravity * (1f - _buoyancy);
  545. Gravity = new OMV.Vector3(0f, 0f, grav);
  546. if (PhysBody.HasPhysicalBody)
  547. PhysScene.PE.SetGravity(PhysBody, Gravity);
  548. }
  549. }
  550. // Used for MoveTo
  551. public override OMV.Vector3 PIDTarget {
  552. set { _PIDTarget = value; }
  553. }
  554. public override bool PIDActive {
  555. set { _usePID = value; }
  556. }
  557. public override float PIDTau {
  558. set { _PIDTau = value; }
  559. }
  560. public override void AddForce(OMV.Vector3 force, bool pushforce)
  561. {
  562. // Since this force is being applied in only one step, make this a force per second.
  563. OMV.Vector3 addForce = force / PhysScene.LastTimeStep;
  564. AddForce(addForce, pushforce, false);
  565. }
  566. private void AddForce(OMV.Vector3 force, bool pushforce, bool inTaintTime) {
  567. if (force.IsFinite())
  568. {
  569. OMV.Vector3 addForce = Util.ClampV(force, BSParam.MaxAddForceMagnitude);
  570. // DetailLog("{0},BSCharacter.addForce,call,force={1}", LocalID, addForce);
  571. PhysScene.TaintedObject(inTaintTime, "BSCharacter.AddForce", delegate()
  572. {
  573. // Bullet adds this central force to the total force for this tick
  574. // DetailLog("{0},BSCharacter.addForce,taint,force={1}", LocalID, addForce);
  575. if (PhysBody.HasPhysicalBody)
  576. {
  577. PhysScene.PE.ApplyCentralForce(PhysBody, addForce);
  578. }
  579. });
  580. }
  581. else
  582. {
  583. m_log.WarnFormat("{0}: Got a NaN force applied to a character. LocalID={1}", LogHeader, LocalID);
  584. return;
  585. }
  586. }
  587. public override void AddAngularForce(OMV.Vector3 force, bool pushforce, bool inTaintTime) {
  588. }
  589. public override void SetMomentum(OMV.Vector3 momentum) {
  590. }
  591. private OMV.Vector3 ComputeAvatarScale(OMV.Vector3 size)
  592. {
  593. OMV.Vector3 newScale;
  594. // Bullet's capsule total height is the "passed height + radius * 2";
  595. // The base capsule is 1 unit in diameter and 2 units in height (passed radius=0.5, passed height = 1)
  596. // The number we pass in for 'scaling' is the multiplier to get that base
  597. // shape to be the size desired.
  598. // So, when creating the scale for the avatar height, we take the passed height
  599. // (size.Z) and remove the caps.
  600. // An oddity of the Bullet capsule implementation is that it presumes the Y
  601. // dimension is the radius of the capsule. Even though some of the code allows
  602. // for a asymmetrical capsule, other parts of the code presume it is cylindrical.
  603. // Scale is multiplier of radius with one of "0.5"
  604. newScale.X = size.X / 2f;
  605. newScale.Y = size.Y / 2f;
  606. float heightAdjust = BSParam.AvatarHeightMidFudge;
  607. if (BSParam.AvatarHeightLowFudge != 0f || BSParam.AvatarHeightHighFudge != 0f)
  608. {
  609. // An avatar is between 1.61 and 2.12 meters. Midpoint is 1.87m.
  610. // The "times 4" relies on the fact that the difference from the midpoint to the extremes is exactly 0.25
  611. float midHeightOffset = size.Z - 1.87f;
  612. if (midHeightOffset < 0f)
  613. {
  614. // Small avatar. Add the adjustment based on the distance from midheight
  615. heightAdjust += -1f * midHeightOffset * 4f * BSParam.AvatarHeightLowFudge;
  616. }
  617. else
  618. {
  619. // Large avatar. Add the adjustment based on the distance from midheight
  620. heightAdjust += midHeightOffset * 4f * BSParam.AvatarHeightHighFudge;
  621. }
  622. }
  623. // The total scale height is the central cylindar plus the caps on the two ends.
  624. newScale.Z = (size.Z + (Math.Min(size.X, size.Y) * 2) + heightAdjust) / 2f;
  625. // m_log.DebugFormat("{0} ComputeAvatarScale: size={1},adj={2},scale={3}", LogHeader, size, heightAdjust, newScale);
  626. // If smaller than the endcaps, just fake like we're almost that small
  627. if (newScale.Z < 0)
  628. newScale.Z = 0.1f;
  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. // Don't change position if standing on a stationary object.
  652. if (!IsStationary)
  653. _position = entprop.Position;
  654. _orientation = 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, _position);
  667. entprop.Position = _position;
  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. // base.RequestPhysicsterseUpdate();
  676. DetailLog("{0},BSCharacter.UpdateProperties,call,pos={1},orient={2},vel={3},accel={4},rotVel={5}",
  677. LocalID, _position, _orientation, RawVelocity, _acceleration, _rotationalVelocity);
  678. }
  679. }
  680. }