BasicVehicles.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. using System.Collections.Generic;
  28. using NUnit.Framework;
  29. using OpenSim.Framework;
  30. using OpenSim.Region.PhysicsModule.BulletS;
  31. using OpenSim.Region.PhysicsModules.SharedBase;
  32. using OpenSim.Tests.Common;
  33. using OpenMetaverse;
  34. namespace OpenSim.Region.PhysicsModule.BulletS.Tests
  35. {
  36. [TestFixture]
  37. public class BasicVehicles : OpenSimTestCase
  38. {
  39. // Documentation on attributes: http://www.nunit.org/index.php?p=attributes&r=2.6.1
  40. // Documentation on assertions: http://www.nunit.org/index.php?p=assertions&r=2.6.1
  41. BSScene PhysicsScene { get; set; }
  42. BSPrim TestVehicle { get; set; }
  43. Vector3 TestVehicleInitPosition { get; set; }
  44. float simulationTimeStep = 0.089f;
  45. [TestFixtureSetUp]
  46. public void Init()
  47. {
  48. Dictionary<string, string> engineParams = new Dictionary<string, string>();
  49. engineParams.Add("VehicleEnableAngularVerticalAttraction", "true");
  50. engineParams.Add("VehicleAngularVerticalAttractionAlgorithm", "1");
  51. PhysicsScene = BulletSimTestsUtil.CreateBasicPhysicsEngine(engineParams);
  52. PrimitiveBaseShape pbs = PrimitiveBaseShape.CreateSphere();
  53. Vector3 pos = new Vector3(100.0f, 100.0f, 0f);
  54. pos.Z = PhysicsScene.TerrainManager.GetTerrainHeightAtXYZ(pos) + 2f;
  55. TestVehicleInitPosition = pos;
  56. Vector3 size = new Vector3(1f, 1f, 1f);
  57. pbs.Scale = size;
  58. Quaternion rot = Quaternion.Identity;
  59. bool isPhys = false;
  60. uint localID = 123;
  61. PhysicsScene.AddPrimShape("testPrim", pbs, pos, size, rot, isPhys, localID);
  62. TestVehicle = (BSPrim)PhysicsScene.PhysObjects[localID];
  63. // The actual prim shape creation happens at taint time
  64. PhysicsScene.ProcessTaints();
  65. }
  66. [TestFixtureTearDown]
  67. public void TearDown()
  68. {
  69. if (PhysicsScene != null)
  70. {
  71. // The Dispose() will also free any physical objects in the scene
  72. PhysicsScene.Dispose();
  73. PhysicsScene = null;
  74. }
  75. }
  76. [TestCase(2f, 0.2f, 0.25f, 0.25f, 0.25f)]
  77. [TestCase(2f, 0.2f, -0.25f, 0.25f, 0.25f)]
  78. [TestCase(2f, 0.2f, 0.25f, -0.25f, 0.25f)]
  79. [TestCase(2f, 0.2f, -0.25f, -0.25f, 0.25f)]
  80. // [TestCase(2f, 0.2f, 0.785f, 0.0f, 0.25f) /*, "Leaning 45 degrees to the side" */]
  81. // [TestCase(2f, 0.2f, 1.650f, 0.0f, 0.25f) /*, "Leaning more than 90 degrees to the side" */]
  82. // [TestCase(2f, 0.2f, 2.750f, 0.0f, 0.25f) /*, "Almost upside down, tipped right" */]
  83. // [TestCase(2f, 0.2f,-2.750f, 0.0f, 0.25f) /*, "Almost upside down, tipped left" */]
  84. // [TestCase(2f, 0.2f, 0.0f, 0.785f, 0.25f) /*, "Tipped back 45 degrees" */]
  85. // [TestCase(2f, 0.2f, 0.0f, 1.650f, 0.25f) /*, "Tipped back more than 90 degrees" */]
  86. // [TestCase(2f, 0.2f, 0.0f, 2.750f, 0.25f) /*, "Almost upside down, tipped back" */]
  87. // [TestCase(2f, 0.2f, 0.0f,-2.750f, 0.25f) /*, "Almost upside down, tipped forward" */]
  88. public void AngularVerticalAttraction(float timeScale, float efficiency, float initRoll, float initPitch, float initYaw)
  89. {
  90. // Enough simulation steps to cover the timescale the operation should take
  91. int simSteps = (int)(timeScale / simulationTimeStep) + 1;
  92. // Tip the vehicle
  93. Quaternion initOrientation = Quaternion.CreateFromEulers(initRoll, initPitch, initYaw);
  94. TestVehicle.Orientation = initOrientation;
  95. TestVehicle.Position = TestVehicleInitPosition;
  96. // The vehicle controller is not enabled directly (by setting a vehicle type).
  97. // Instead the appropriate values are set and calls are made just the parts of the
  98. // controller we want to exercise. Stepping the physics engine then applies
  99. // the actions of that one feature.
  100. BSDynamics vehicleActor = TestVehicle.GetVehicleActor(true /* createIfNone */);
  101. if (vehicleActor != null)
  102. {
  103. vehicleActor.ProcessFloatVehicleParam(Vehicle.VERTICAL_ATTRACTION_EFFICIENCY, efficiency);
  104. vehicleActor.ProcessFloatVehicleParam(Vehicle.VERTICAL_ATTRACTION_TIMESCALE, timeScale);
  105. // vehicleActor.enableAngularVerticalAttraction = true;
  106. TestVehicle.IsPhysical = true;
  107. PhysicsScene.ProcessTaints();
  108. // Step the simulator a bunch of times and vertical attraction should orient the vehicle up
  109. for (int ii = 0; ii < simSteps; ii++)
  110. {
  111. vehicleActor.ForgetKnownVehicleProperties();
  112. vehicleActor.ComputeAngularVerticalAttraction();
  113. vehicleActor.PushKnownChanged();
  114. PhysicsScene.Simulate(simulationTimeStep);
  115. }
  116. }
  117. TestVehicle.IsPhysical = false;
  118. PhysicsScene.ProcessTaints();
  119. // After these steps, the vehicle should be upright
  120. /*
  121. float finalRoll, finalPitch, finalYaw;
  122. TestVehicle.Orientation.GetEulerAngles(out finalRoll, out finalPitch, out finalYaw);
  123. Assert.That(finalRoll, Is.InRange(-0.01f, 0.01f));
  124. Assert.That(finalPitch, Is.InRange(-0.01f, 0.01f));
  125. Assert.That(finalYaw, Is.InRange(initYaw - 0.1f, initYaw + 0.1f));
  126. */
  127. Vector3 upPointer = Vector3.UnitZ * TestVehicle.Orientation;
  128. Assert.That(upPointer.Z, Is.GreaterThan(0.99f));
  129. }
  130. }
  131. }