diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2016-01-05 12:37:28 +0100 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2016-01-05 12:37:28 +0100 |
| commit | 9e83d173a5995587c208051fdd62420f51ff5378 (patch) | |
| tree | fcd8e2c78e9f19ab441eb5621c8017080184b972 /src | |
| parent | 5b10b57badf1b28f3cebed1f045764ba212466e7 (diff) | |
| download | Multimedia-9e83d173a5995587c208051fdd62420f51ff5378.tar.gz Multimedia-9e83d173a5995587c208051fdd62420f51ff5378.zip | |
Add last hit detectionhitDetection
Diffstat (limited to 'src')
| -rw-r--r-- | src/gldisc.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/gldisc.cpp b/src/gldisc.cpp index ee0a06b..85dd6d4 100644 --- a/src/gldisc.cpp +++ b/src/gldisc.cpp @@ -107,6 +107,41 @@ bool GLDisc::isHit(QVector3D p1, QVector3D p2) return true; } + // Check if the mouse line is hitting the outer disc side + QVector2D vecXYProjection = lineVector.toVector2D(); + QVector2D vecXYStart = p1.toVector2D(); + + // Solved via the "Mitternachtsformel" + float a = QVector2D::dotProduct(vecXYProjection, vecXYProjection); + float b = 2 * QVector2D::dotProduct(vecXYStart, vecXYProjection); + float c = QVector2D::dotProduct(vecXYStart, + vecXYStart) - m_circleRadius * m_circleRadius; + + float discriminant = b * b - 4 * a * c; + + if (discriminant >= 0) + { + discriminant = sqrt(discriminant); + + float lambda1 = (-b - discriminant) / (2*a); + + float z1 = (p1 + lambda1 * lineVector).z(); + + if(z1 >= 0 && z1 <= m_height) + { + return true; + } + + float lambda2 = (-b + discriminant) / (2*a); + + float z2 = (p1 + lambda2 * lineVector).z(); + + if(z2 >= 0 && z2 <= m_height) + { + return true; + } + } + return false; } |
