diff --git a/planeRayCastSample.cpp b/planeRayCastSample.cpp index fa9f6e5..00f7804 100644 --- a/planeRayCastSample.cpp +++ b/planeRayCastSample.cpp @@ -1,3 +1,5 @@ +#include + #include "root.h" #include "assetManager.h" #include "quad.h" @@ -39,8 +41,8 @@ int main(){ mat->addBoolUniform("texturingEnabled", false); mat->addVec4Uniform("diffuseColor", Vector4(0, 0, 1, 1)); - Vector3 size = Vector3(100, 100, 0); - int numVertDivs = 20, numHorDivs = 20; + Vector3 size = Vector3(1000, 1000, 0); + int numVertDivs = 50, numHorDivs = 50; Quad *quad = new Quad(size, true, numVertDivs, numHorDivs); quad->setMaterial(mat); @@ -57,14 +59,18 @@ int main(){ for(int i = 0; i < numVertDivs; i++){ for(int j = 0; j < numHorDivs; j++){ Vector3 rayPos = startPos + Vector3(stepX * j, 0, stepZ * i); - vector results = RayCaster::cast(rayPos + Vector3(0, 100, 0), -Vector3::VEC_J, plane); + vector results = RayCaster::cast(startPos + Vector3(0, 100, 0), -Vector3::VEC_J, plane, 0, 30); + bool hit = !results.empty(); + + if(!hit) + cout << "x: " << rayPos.x << ", z: " << rayPos.z << endl; Material *mat = new Material(PATH + "texture"); mat->addBoolUniform("lightingEnabled", false); mat->addBoolUniform("texturingEnabled", false); - mat->addVec4Uniform("diffuseColor", (results.empty() ? Vector4(1, 0, 0, 1) : Vector4(0, 1, 0, 1))); + mat->addVec4Uniform("diffuseColor", (hit ? Vector4(0, 1, 0, 1) : Vector4(1, 0, 0, 1))); - Box *box = new Box(Vector3::VEC_IJK); + Box *box = new Box(Vector3::VEC_IJK * .7); box->setMaterial(mat); Node *node = new Node(rayPos); diff --git a/rayCaster.cpp b/rayCaster.cpp index dc6f6d2..cc56c43 100644 --- a/rayCaster.cpp +++ b/rayCaster.cpp @@ -42,7 +42,7 @@ namespace vb01{ bool skip = false; for(int j = 0; j < 3; j++){ - Vector3 rayPosToVert = (*(vertices[i * 3 + j].pos) - rayPos); + Vector3 rayPosToVert = (*(vertices[indices[i * 3 + j]].pos) - rayPos); float angle = rayDir.getAngleBetween(rayPosToVert.norm()); if(angle > PI / 2) angle = PI - angle; @@ -58,7 +58,9 @@ namespace vb01{ if(skip) continue; } - Vector3 pointA = pos + rot * *(vertices[indices[i * 3]].pos), pointB = pos + rot * *(vertices[indices[i * 3 + 1]].pos), pointC = pos + rot * *(vertices[indices[i * 3 + 2]].pos); + Vector3 pointA = pos + rot * *(vertices[indices[i * 3]].pos); + Vector3 pointB = pos + rot * *(vertices[indices[i * 3 + 1]].pos); + Vector3 pointC = pos + rot * *(vertices[indices[i * 3 + 2]].pos); Vector3 hypVec = pointA - rayPos; Vector3 perpVec = (pointB - pointA).cross(pointC - pointA).norm(); float a1 = hypVec.norm().getAngleBetween(perpVec); @@ -87,7 +89,7 @@ namespace vb01{ bool withinBisecB = ((contactPoint - pointB).norm().getAngleBetween(bisecBVec.norm()) - angleB / 2) <= eps; bool withinBisecC = ((contactPoint - pointC).norm().getAngleBetween(bisecCVec.norm()) - angleC / 2) <= eps; - if(withinBisecA && withinBisecB && withinBisecC){ + if((withinBisecA && withinBisecB) || (withinBisecB && withinBisecC) || (withinBisecA && withinBisecC)){ CollisionResult result; result.pos = contactPoint; result.norm = -perpVec;