physicsEvents.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/physicsEvents.h"
  24. #include "math/mathIO.h"
  25. #include "core/stream/bitStream.h"
  26. #include "T3D/physics/physicsPlugin.h"
  27. #include "T3D/physics/physicsWorld.h"
  28. #include "scene/sceneObject.h"
  29. #include "T3D/gameBase/gameConnection.h"
  30. #include "console/engineAPI.h"
  31. RadialImpulseEvent::RadialImpulseEvent()
  32. : mPosition( 0, 0, 0 ),
  33. mRadius( 0 ),
  34. mMagnitude( 0 )
  35. {
  36. }
  37. RadialImpulseEvent::RadialImpulseEvent( const Point3F &pos, F32 radius, F32 magnitude )
  38. : mPosition( pos ),
  39. mRadius( radius ),
  40. mMagnitude( magnitude )
  41. {
  42. }
  43. RadialImpulseEvent::~RadialImpulseEvent()
  44. {
  45. }
  46. void RadialImpulseEvent::pack( NetConnection* /*ps*/, BitStream *bstream )
  47. {
  48. mathWrite( *bstream, mPosition );
  49. bstream->write( mRadius );
  50. bstream->write( mMagnitude );
  51. }
  52. void RadialImpulseEvent::write( NetConnection*, BitStream *bstream )
  53. {
  54. mathWrite( *bstream, mPosition );
  55. bstream->write( mRadius );
  56. bstream->write( mMagnitude );
  57. }
  58. void RadialImpulseEvent::unpack( NetConnection *ps, BitStream *bstream )
  59. {
  60. mathRead( *bstream, &mPosition );
  61. bstream->read( &mRadius );
  62. bstream->read( &mMagnitude );
  63. }
  64. void RadialImpulseEvent::process(NetConnection *con)
  65. {
  66. impulse( &gClientContainer, mPosition, mRadius, mMagnitude );
  67. }
  68. void RadialImpulseEvent::_impulseCallback( SceneObject *obj, void *key )
  69. {
  70. ImpulseInfo *info = (ImpulseInfo*)key;
  71. obj->applyRadialImpulse( info->pos, info->radius, info->magnitude );
  72. }
  73. void RadialImpulseEvent::impulse( SceneContainer *con, const Point3F &position, F32 radius, F32 magnitude )
  74. {
  75. Point3F offset( radius, radius, radius );
  76. Box3F bounds( position - offset, position + offset );
  77. ImpulseInfo info;
  78. info.pos = position;
  79. info.radius = radius;
  80. info.magnitude = magnitude;
  81. con->findObjects( bounds, -1, _impulseCallback, &info );
  82. }
  83. IMPLEMENT_CO_NETEVENT_V1( RadialImpulseEvent );
  84. ConsoleDocClass( RadialImpulseEvent,
  85. "@brief Creates a physics-based impulse effect from a defined central point and magnitude.\n\n"
  86. "@see RadialImpulseEvent::send\n"
  87. "@ingroup Physics\n"
  88. );
  89. DefineEngineStaticMethod(RadialImpulseEvent, send, void, (const char* inPosition, F32 radius, F32 magnitude), ("1.0 1.0 1.0", 10.0f, 20.0f),
  90. "@brief Applies a radial impulse to any SceneObjects within the area of effect.\n\n"
  91. "This event is performed both server and client-side.\n\n"
  92. "@param position Center point for this radial impulse.\n"
  93. "@param radius Distance from the position for this radial impulse to affect.\n"
  94. "@param magnitude The force applied to objects within the radius from the position of this radial impulse effect.\n\n"
  95. "@tsexample\n"
  96. "// Define the Position\n"
  97. "%position = \"10.0 15.0 10.0\";\n\n"
  98. "// Define the Radius\n"
  99. "%radius = \"25.0\";\n\n"
  100. "// Define the Magnitude\n"
  101. "%magnitude = \"30.0\"\n\n"
  102. "// Create a globalRadialImpulse physics effect.\n"
  103. "RadialImpulseEvent::send(%position,%radius,%magnitude);\n"
  104. "@endtsexample\n\n")
  105. {
  106. // Scan out arguments...
  107. Point3F position;
  108. dSscanf( inPosition, "%f %f %f", &position.x, &position.y, &position.z );
  109. // Apply server-side.
  110. RadialImpulseEvent::impulse( &gServerContainer, position, radius, magnitude );
  111. // Transmit event to each client to perform client-side...
  112. SimGroup *pClientGroup = Sim::getClientGroup();
  113. if ( !pClientGroup )
  114. {
  115. Con::errorf( "globalRadialImpulse() - Client group not found!" );
  116. return;
  117. }
  118. SimGroup::iterator itr = pClientGroup->begin();
  119. for ( ; itr != pClientGroup->end(); itr++ )
  120. {
  121. GameConnection* gc = static_cast<GameConnection*>(*itr);
  122. if ( gc )
  123. gc->postNetEvent( new RadialImpulseEvent( position, radius, magnitude ) );
  124. }
  125. }