Guest User

Untitled

a guest
Jul 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include "BallContactListener.h"
  2.  
  3. #include <algorithm>
  4. #include <bitset>
  5. #include <math.h>
  6.  
  7. #include "EnumsConstants.h"
  8. #include "SoundSystem.h"
  9.  
  10. namespace Tuatara
  11. {
  12. BallContactListener::BallContactListener( SoundSystem *sound ) : soundSystem( sound ),
  13. maximum( levelSize - BLOCK_HALF_EXTENT * 3 - BALL_RADIUS ), minimum( BLOCK_HALF_EXTENT + BALL_RADIUS ),
  14. positionBuffer( 0.02f )
  15. {
  16. }
  17.  
  18. bool BallContactListener::IsOnWall( const float& x, const float& y, const float& z, const hkVector4& velocity )
  19. {
  20. auto maximumCheck = [=]( float val ) -> bool
  21. {
  22. if( val >= maximum - positionBuffer )
  23. {
  24. return true;
  25. }
  26. return false;
  27. };
  28.  
  29. auto minimumCheck = [=]( float val ) -> bool
  30. {
  31. if( val <= minimum + positionBuffer )
  32. {
  33. return true;
  34. }
  35. return false;
  36. };
  37.  
  38.  
  39. // left wall
  40. if( minimumCheck( x ) )
  41. {
  42. return true;
  43. }
  44. // right wall
  45. if( maximumCheck( x ) )
  46. {
  47. return true;
  48. }
  49. // top wall
  50. if( maximumCheck( y ) )
  51. {
  52. return true;
  53. }
  54. // bottom wall
  55. if( minimumCheck( y ) && velocity(1) < -0.5f )
  56. {
  57. return true;
  58. }
  59. // front wall
  60. if( minimumCheck( z ) )
  61. {
  62. return true;
  63. }
  64. // back wall
  65. if( maximumCheck( z ) )
  66. {
  67. return true;
  68. }
  69.  
  70. return false;
  71. }
  72.  
  73. void BallContactListener::contactPointCallback( const hkpContactPointEvent& event )
  74. {
  75. using namespace std;
  76.  
  77. auto ball = event.getBody( event.m_source );
  78. hkVector4 position( ball->getPosition() );
  79.  
  80. if( abs( event.getSeparatingVelocity() ) >= 0.75f )
  81. {
  82. if( IsOnWall( position(0), position(1), position(2), ball->getLinearVelocity() ) )
  83. {
  84. soundSystem->PlayCollisionSound();
  85. }
  86. }
  87. }
  88. }
Add Comment
Please, Sign In to add comment