1
0

Raycast.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.PhysicsModule.BulletS;
  35. using OpenSim.Region.PhysicsModules.SharedBase;
  36. using OpenSim.Tests.Common;
  37. using OpenMetaverse;
  38. namespace OpenSim.Region.PhysicsModule.BulletS.Tests
  39. {
  40. [TestFixture]
  41. public class BulletSimRaycast : 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 _targetSphere { get; set; }
  47. Vector3 _targetSpherePosition { get; set; }
  48. // float _simulationTimeStep = 0.089f;
  49. uint _targetLocalID = 123;
  50. [TestFixtureSetUp]
  51. public void Init()
  52. {
  53. Dictionary<string, string> engineParams = new Dictionary<string, string>();
  54. engineParams.Add("UseBulletRaycast", "true");
  55. _physicsScene = BulletSimTestsUtil.CreateBasicPhysicsEngine(engineParams);
  56. PrimitiveBaseShape pbs = PrimitiveBaseShape.CreateSphere();
  57. Vector3 pos = new Vector3(100.0f, 100.0f, 50f);
  58. _targetSpherePosition = pos;
  59. Vector3 size = new Vector3(10f, 10f, 10f);
  60. pbs.Scale = size;
  61. Quaternion rot = Quaternion.Identity;
  62. bool isPhys = false;
  63. _physicsScene.AddPrimShape("TargetSphere", pbs, pos, size, rot, isPhys, _targetLocalID);
  64. _targetSphere = (BSPrim)_physicsScene.PhysObjects[_targetLocalID];
  65. // The actual prim shape creation happens at taint time
  66. _physicsScene.ProcessTaints();
  67. }
  68. [TestFixtureTearDown]
  69. public void TearDown()
  70. {
  71. if (_physicsScene != null)
  72. {
  73. // The Dispose() will also free any physical objects in the scene
  74. _physicsScene.Dispose();
  75. _physicsScene = null;
  76. }
  77. }
  78. // There is a 10x10x10 sphere at <100,100,50>
  79. // Shoot rays around the sphere and verify it hits and doesn't hit
  80. // TestCase parameters are <x,y,z> of start and <x,y,z> of end and expected result
  81. [TestCase(100f, 50f, 50f, 100f, 150f, 50f, true, "Pass through sphere from front")]
  82. [TestCase(50f, 100f, 50f, 150f, 100f, 50f, true, "Pass through sphere from side")]
  83. [TestCase(50f, 50f, 50f, 150f, 150f, 50f, true, "Pass through sphere diaginally")]
  84. [TestCase(100f, 100f, 100f, 100f, 100f, 20f, true, "Pass through sphere from above")]
  85. [TestCase(20f, 20f, 50f, 80f, 80f, 50f, false, "Not reach sphere")]
  86. [TestCase(50f, 50f, 65f, 150f, 150f, 65f, false, "Passed over sphere")]
  87. public void RaycastAroundObject(float fromX, float fromY, float fromZ, float toX, float toY, float toZ, bool expected, string msg)
  88. {
  89. Vector3 fromPos = new Vector3(fromX, fromY, fromZ);
  90. Vector3 toPos = new Vector3(toX, toY, toZ);
  91. Vector3 direction = toPos - fromPos;
  92. float len = Vector3.Distance(fromPos, toPos);
  93. List<ContactResult> results = _physicsScene.RaycastWorld(fromPos, direction, len, 1);
  94. if (expected)
  95. {
  96. // The test coordinates should generate a hit
  97. Assert.True(results.Count != 0, msg + ": Did not return a hit but expected to.");
  98. Assert.True(results.Count == 1, msg + ": Raycast returned not just one hit result.");
  99. Assert.True(results[0].ConsumerID == _targetLocalID, msg + ": Raycast returned a collision object other than the target");
  100. }
  101. else
  102. {
  103. // The test coordinates should not generate a hit
  104. if (results.Count > 0)
  105. {
  106. Assert.False(results.Count > 0, msg + ": Returned a hit at " + results[0].Pos.ToString());
  107. }
  108. }
  109. }
  110. }
  111. }