From 9e83d173a5995587c208051fdd62420f51ff5378 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Tue, 5 Jan 2016 12:37:28 +0100 Subject: Add last hit detection --- src/gldisc.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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; } -- cgit v1.2.3-70-g09d2