BasicVehicles.cs 6.8 KB

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