From e3c3430355e53dff419f78168cb99f98915d7f3d Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 14 Dec 2015 14:54:59 +0100 Subject: Call isHit in main --- src/mmscene.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mmscene.cpp b/src/mmscene.cpp index ccae5a5..1554e83 100644 --- a/src/mmscene.cpp +++ b/src/mmscene.cpp @@ -76,7 +76,9 @@ void MMScene::setupGeometry() void MMScene::mousePressed(int x, int y) { renderer()->calculateMousePoints(&m_MouseNear, &m_mouseFar, QPoint(x, y)); - renderer()->mouseIntersection(&m_lastIntersection, v_Y, 0.0, QPoint(x, y)); //get starting point + renderer()->mouseIntersection(&m_lastIntersection, v_Z, 0.0, QPoint(x, y)); //get starting point + if (m_disc->isHit(m_MouseNear, m_mouseFar)) + qDebug() << "Treffer"; } void MMScene::drawTriangles() -- cgit v1.2.3-70-g09d2 From 88f73581a5467cd00aaf7699cd43cfb6216bd1fc Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 14 Dec 2015 16:50:02 +0100 Subject: Add bottom hit detection --- src/gldisc.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/gldisc.cpp b/src/gldisc.cpp index a2c22b5..03a0c6b 100644 --- a/src/gldisc.cpp +++ b/src/gldisc.cpp @@ -82,12 +82,21 @@ void GLDisc::makeSurface(QVector *pointContainer, bool GLDisc::isHit(QVector3D p1, QVector3D p2) { - if(!GLBody::isHit(p1, p2)) + if (!GLBody::isHit(p1, p2)) { return false; } + QVector3D lineVector = p2 - p1; + QVector3D vecBottom = p1 + lineVector * (-p1.z() / lineVector.z()); + + if (vecBottom.length() < m_circleRadius) + { + return true; + } + + return false; } QVector2D GLDisc::calculatePoint(double sideAngle) -- cgit v1.2.3-70-g09d2 From be07837122b161fe7951064da06e6468b35b29f6 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 14 Dec 2015 16:50:29 +0100 Subject: Format gldisc.cpp --- src/gldisc.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gldisc.cpp b/src/gldisc.cpp index 03a0c6b..bd4cb6c 100644 --- a/src/gldisc.cpp +++ b/src/gldisc.cpp @@ -29,10 +29,10 @@ void GLDisc::makeSurface(QVector *pointContainer, m_points->append(GLPoint(m_circleRadius * tmp, -v_Z, tmpTextCoo, m_color)); m_points->append(GLPoint(m_circleRadius * tmp, tmp, tmpTextCoo, m_color)); - m_points->append(GLPoint(QVector3D(m_circleRadius * tmp, m_height), tmp, tmpTextCoo, - m_color)); - m_points->append(GLPoint(QVector3D(m_circleRadius * tmp, m_height), v_Z, tmpTextCoo, - m_color)); + m_points->append(GLPoint(QVector3D(m_circleRadius * tmp, m_height), tmp, + tmpTextCoo, m_color)); + m_points->append(GLPoint(QVector3D(m_circleRadius * tmp, m_height), v_Z, + tmpTextCoo, m_color)); } m_points->append(GLPoint(m_height * v_Z, v_Z, QVector3D(0.5, 0.5, 0.0), -- cgit v1.2.3-70-g09d2 From ca49968016ef47289c4350ce676e444ac33b8180 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 14 Dec 2015 17:05:25 +0100 Subject: Add top hit detection --- src/gldisc.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/gldisc.cpp b/src/gldisc.cpp index bd4cb6c..5afa4d4 100644 --- a/src/gldisc.cpp +++ b/src/gldisc.cpp @@ -96,6 +96,13 @@ bool GLDisc::isHit(QVector3D p1, QVector3D p2) return true; } + QVector3D vecTop = p1 + lineVector * ((m_height - p1.z()) / lineVector.z()); + + if (vecTop.length() < m_circleRadius) + { + return true; + } + return false; } -- cgit v1.2.3-70-g09d2 From 5b10b57badf1b28f3cebed1f045764ba212466e7 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 14 Dec 2015 17:39:25 +0100 Subject: Fix top hit detection --- src/gldisc.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/gldisc.cpp b/src/gldisc.cpp index 5afa4d4..ee0a06b 100644 --- a/src/gldisc.cpp +++ b/src/gldisc.cpp @@ -89,6 +89,7 @@ bool GLDisc::isHit(QVector3D p1, QVector3D p2) QVector3D lineVector = p2 - p1; + // Calculate intersection point with the xy plane QVector3D vecBottom = p1 + lineVector * (-p1.z() / lineVector.z()); if (vecBottom.length() < m_circleRadius) @@ -96,7 +97,10 @@ bool GLDisc::isHit(QVector3D p1, QVector3D p2) return true; } - QVector3D vecTop = p1 + lineVector * ((m_height - p1.z()) / lineVector.z()); + // Calculate intersection point with the xy + m_heigth plane + // Then drop z so that we get a projection onto the xy plane + QVector3D vecTop = (p1 + lineVector * ((m_height - p1.z()) / + lineVector.z())).toVector2D(); if (vecTop.length() < m_circleRadius) { -- cgit v1.2.3-70-g09d2 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