pxMaterial.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/physics/physX/px.h"
  24. #include "T3D/physics/physX/pxMaterial.h"
  25. #include "T3D/physics/physX/pxWorld.h"
  26. #include "T3D/physics/physicsPlugin.h"
  27. #include "console/consoleTypes.h"
  28. #include "core/stream/bitStream.h"
  29. IMPLEMENT_CO_DATABLOCK_V1( PxMaterial );
  30. ConsoleDocClass( PxMaterial,
  31. "@brief Defines a PhysX material assignable to a PxMaterial.\n\n"
  32. "When two actors collide, the collision behavior that results depends on the material properties "
  33. "of the actors' surfaces. For example, the surface properties determine if the actors will or will "
  34. "not bounce, or if they will slide or stick. Currently, the only special feature supported by materials "
  35. "is anisotropic friction, but according to Nvidia, other effects such as moving surfaces and more types "
  36. "of friction are slotted for future release.\n\n"
  37. "For more information, refer to Nvidia's PhysX docs.\n\n"
  38. "@ingroup Physics"
  39. );
  40. PxMaterial::PxMaterial()
  41. : mNxMat( NULL ),
  42. mNxMatId( -1 ),
  43. restitution( 0.0f ),
  44. staticFriction( 0.1f ),
  45. dynamicFriction( 0.95f ),
  46. mServer( false )
  47. {
  48. }
  49. PxMaterial::~PxMaterial()
  50. {
  51. }
  52. void PxMaterial::consoleInit()
  53. {
  54. Parent::consoleInit();
  55. }
  56. void PxMaterial::initPersistFields()
  57. {
  58. Parent::initPersistFields();
  59. addGroup("PxMaterial");
  60. addField( "restitution", TypeF32, Offset( restitution, PxMaterial ),
  61. "@brief Coeffecient of a bounce applied to the shape in response to a collision.\n\n"
  62. "A value of 0 makes the object bounce as little as possible, while higher values up to 1.0 result in more bounce.\n\n"
  63. "@note Values close to or above 1.0 may cause stability problems and/or increasing energy.");
  64. addField( "staticFriction", TypeF32, Offset( staticFriction, PxMaterial ),
  65. "@brief Coefficient of static %friction to be applied.\n\n"
  66. "Static %friction determines the force needed to start moving an at-rest object in contact with a surface. "
  67. "If the force applied onto shape cannot overcome the force of static %friction, the shape will remain at rest. "
  68. "A higher coefficient will require a larger force to start motion. "
  69. "@note This value should be larger than 0.\n\n");
  70. addField( "dynamicFriction", TypeF32, Offset( dynamicFriction, PxMaterial ),
  71. "@brief Coefficient of dynamic %friction to be applied.\n\n"
  72. "Dynamic %friction reduces the velocity of a moving object while it is in contact with a surface. "
  73. "A higher coefficient will result in a larger reduction in velocity. "
  74. "A shape's dynamicFriction should be equal to or larger than 0.\n\n");
  75. endGroup("PxMaterial");
  76. }
  77. void PxMaterial::onStaticModified( const char *slotName, const char *newValue )
  78. {
  79. if ( isProperlyAdded() && mNxMat != NULL )
  80. {
  81. mNxMat->setRestitution( restitution );
  82. mNxMat->setStaticFriction( staticFriction );
  83. mNxMat->setDynamicFriction( dynamicFriction );
  84. }
  85. }
  86. bool PxMaterial::preload( bool server, String &errorBuffer )
  87. {
  88. mServer = server;
  89. PxWorld *world = dynamic_cast<PxWorld*>( PHYSICSMGR->getWorld( server ? "server" : "client" ) );
  90. if ( !world )
  91. {
  92. // TODO: Error... in error buffer?
  93. return false;
  94. }
  95. NxMaterialDesc material;
  96. material.restitution = restitution;
  97. material.staticFriction = staticFriction;
  98. material.dynamicFriction = dynamicFriction;
  99. mNxMat = world->createMaterial( material );
  100. mNxMatId = mNxMat->getMaterialIndex();
  101. if ( mNxMatId == -1 )
  102. {
  103. errorBuffer = "PxMaterial::preload() - unable to create material!";
  104. return false;
  105. }
  106. return Parent::preload( server, errorBuffer );
  107. }
  108. void PxMaterial::packData( BitStream* stream )
  109. {
  110. Parent::packData( stream );
  111. stream->write( restitution );
  112. stream->write( staticFriction );
  113. stream->write( dynamicFriction );
  114. }
  115. void PxMaterial::unpackData( BitStream* stream )
  116. {
  117. Parent::unpackData( stream );
  118. stream->read( &restitution );
  119. stream->read( &staticFriction );
  120. stream->read( &dynamicFriction );
  121. }