diff options
author | Andreas Buhr <andreas@andreasbuhr.de> | 2021-03-02 15:28:59 +0100 |
---|---|---|
committer | Andreas Buhr <andreas@andreasbuhr.de> | 2021-03-05 15:21:25 +0100 |
commit | d2e9a5113eab4a1eda45cb3d945faca8e01cbb7f (patch) | |
tree | a8550f37a2fef2578e7ff74983dd846fae8060d2 /examples/bluetooth/pingpong | |
parent | 4b9c6e69345169de23210852d0b1a3997668e942 (diff) |
Pingpong example: Add convex paddles and speedup
Game logic of the pingpong example was a bit boring, because the
angle of the ball never changed. This patch adds convex paddles.
The surface of the paddle is simulated to be a quarter of a circle.
Also, in each reflection, the speed of the ball increases.
Change-Id: I208e7322f2b4b849118d98959a6076458d20f409
Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'examples/bluetooth/pingpong')
-rw-r--r-- | examples/bluetooth/pingpong/pingpong.cpp | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/examples/bluetooth/pingpong/pingpong.cpp b/examples/bluetooth/pingpong/pingpong.cpp index 2d7a05ee..671f673d 100644 --- a/examples/bluetooth/pingpong/pingpong.cpp +++ b/examples/bluetooth/pingpong/pingpong.cpp @@ -136,11 +136,59 @@ void PingPong::checkBoundaries() if (((m_ballX + ballWidth) > (1. - blockSize)) && (ballCenterY < (m_rightBlockY + blockHeight)) && (ballCenterY > m_rightBlockY)) { // right paddle collision - m_speedx = -std::abs(m_speedx); + // simulating paddle surface to be a quarter of a circle + float paddlecenter = m_rightBlockY + blockHeight / 2.; + float relpos = (ballCenterY - paddlecenter) / (blockHeight / 2.); // [-1 : 1] + float surfaceangle = M_PI_4 * relpos; + + // calculation of surface normal + float normalx = -cos(surfaceangle); + float normaly = sin(surfaceangle); + + // calculation of surface tangent + float tangentx = sin(surfaceangle); + float tangenty = cos(surfaceangle); + + // calculation of tangentialspeed + float tangentialspeed = tangentx * m_speedx + tangenty * m_speedy; + // calculation of normal speed + float normalspeed = normalx * m_speedx + normaly * m_speedy; + + // speed increase of 10% + normalspeed *= 1.1f; + + if (normalspeed < 0) { // if we are coming from the left. To avoid double reflections + m_speedx = tangentialspeed * tangentx - normalspeed * normalx; + m_speedy = tangentialspeed * tangenty - normalspeed * normaly; + } } else if ((m_ballX < blockSize) && (ballCenterY < (m_leftBlockY + blockHeight)) && (ballCenterY > m_leftBlockY)) { // left paddle collision - m_speedx = std::abs(m_speedx); + // simulating paddle surface to be a quarter of a circle + float paddlecenter = m_leftBlockY + blockHeight / 2.; + float relpos = (ballCenterY - paddlecenter) / (blockHeight / 2.); // [-1 : 1] + float surfaceangle = M_PI_4 * relpos; + + // calculation of surface normal + float normalx = cos(surfaceangle); + float normaly = sin(surfaceangle); + + // calculation of surface tangent + float tangentx = -sin(surfaceangle); + float tangenty = cos(surfaceangle); + + // calculation of tangentialspeed + float tangentialspeed = tangentx * m_speedx + tangenty * m_speedy; + // calculation of normal speed + float normalspeed = normalx * m_speedx + normaly * m_speedy; + + // speed increase of 10% + normalspeed *= 1.1f; + + if (normalspeed < 0) { // if we are coming from the left. To avoid double reflections + m_speedx = tangentialspeed * tangentx - normalspeed * normalx; + m_speedy = tangentialspeed * tangenty - normalspeed * normaly; + } } else if (m_ballY < 0) { m_speedy = std::abs(m_speedy); } else if (m_ballY + ballWidth > 1.f) { |