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>
This commit is contained in:
parent
4b9c6e6934
commit
d2e9a5113e
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue