containerQuery.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/containerQuery.h"
  24. #include "scene/sceneObject.h"
  25. #include "environment/waterObject.h"
  26. #include "T3D/physicalZone.h"
  27. void findRouter( SceneObject *obj, void *key )
  28. {
  29. if (obj->getTypeMask() & WaterObjectType)
  30. waterFind(obj, key);
  31. else if (obj->getTypeMask() & PhysicalZoneObjectType)
  32. physicalZoneFind(obj, key);
  33. else {
  34. AssertFatal(false, "Error, must be either water or physical zone here!");
  35. }
  36. }
  37. void waterFind( SceneObject *obj, void *key )
  38. {
  39. PROFILE_SCOPE( waterFind );
  40. // This is called for each WaterObject the ShapeBase object is overlapping.
  41. ContainerQueryInfo *info = static_cast<ContainerQueryInfo*>(key);
  42. WaterObject *water = dynamic_cast<WaterObject*>(obj);
  43. AssertFatal( water != NULL, "containerQuery - waterFind(), passed object was not of class WaterObject!");
  44. // Get point at the bottom/center of the box.
  45. Point3F testPnt = info->box.getCenter();
  46. testPnt.z = info->box.minExtents.z;
  47. F32 coverage = water->getWaterCoverage(info->box);
  48. // Since a WaterObject can have global bounds we may get this call
  49. // even though we have zero coverage. If so we want to early out and
  50. // not save the water properties.
  51. if ( coverage == 0.0f )
  52. return;
  53. // Add in flow force. Would be appropriate to try scaling it by coverage
  54. // thought. Or perhaps have getFlow do that internally and take
  55. // the box parameter.
  56. info->appliedForce += water->getFlow( testPnt );
  57. // Only save the following properties for the WaterObject with the
  58. // greatest water coverage for this ShapeBase object.
  59. if ( coverage < info->waterCoverage )
  60. return;
  61. info->waterCoverage = coverage;
  62. info->liquidType = water->getLiquidType();
  63. info->waterViscosity = water->getViscosity();
  64. info->waterDensity = water->getDensity();
  65. info->waterHeight = water->getSurfaceHeight( Point2F(testPnt.x,testPnt.y) );
  66. info->waterObject = water;
  67. }
  68. void physicalZoneFind(SceneObject* obj, void *key)
  69. {
  70. PROFILE_SCOPE( physicalZoneFind );
  71. ContainerQueryInfo *info = static_cast<ContainerQueryInfo*>(key);
  72. PhysicalZone* pz = dynamic_cast<PhysicalZone*>(obj);
  73. AssertFatal(pz != NULL, "Error, not a physical zone!");
  74. if (pz == NULL || pz->testBox(info->box) == false)
  75. return;
  76. if (pz->isActive()) {
  77. info->gravityScale *= pz->getGravityMod();
  78. info->appliedForce += pz->getForce();
  79. }
  80. }