From 6242ac493620ad6bc873502acda977b727e30fa6 Mon Sep 17 00:00:00 2001 From: ketmar Date: Sun, 19 Jan 2014 17:00:51 +0200 Subject: [PATCH] cosmetix --- src/jelly/Body.cpp | 87 +++++++++++++++++++++++++++-------------------------- src/jelly/Body.h | 10 +++--- src/jelly/World.cpp | 6 ++-- 3 files changed, 52 insertions(+), 51 deletions(-) diff --git a/src/jelly/Body.cpp b/src/jelly/Body.cpp index 695764a..03c8397 100644 --- a/src/jelly/Body.cpp +++ b/src/jelly/Body.cpp @@ -144,7 +144,7 @@ void Body::setMassIndividual (int index, float mass) { } -void Body::setMassFromList (std::vector masses) { +void Body::setMassFromList (const std::vector &masses) { if (masses.size() == (unsigned int)mPointCount) { for (int i = 0; i < mPointCount; ++i) mPointMasses[i].Mass = masses[i]; } @@ -306,118 +306,119 @@ bool Body::contains (const Vector2 &pt) const { } -float Body::getClosestPoint (const Vector2 &pt, Vector2 &hitPt, Vector2 &norm, int &pointA, int &pointB, float &edgeD) const { - hitPt = Vector2::Zero; - pointA = -1; - pointB = -1; - edgeD = 0.0f; - norm = Vector2::Zero; +float Body::getClosestPoint (const Vector2 &pt, Vector2 *hitPt, Vector2 *norm, int *pointA, int *pointB, float *edgeD) const { + if (hitPt) *hitPt = Vector2::Zero; + if (pointA) *pointA = -1; + if (pointB) *pointB = -1; + if (edgeD) *edgeD = 0.0f; + if (norm) *norm = Vector2::Zero; float closestD = 1000.0f; int c = mPointCount; //k8: trick again int j = c-1; for (int i = 0; i < c; j = i++) { - Vector2 tempHit; - Vector2 tempNorm; + Vector2 tempHit, tempNorm; float tempEdgeD; - float dist = getClosestPointOnEdge(pt, j, tempHit, tempNorm, tempEdgeD); + float dist = getClosestPointOnEdge(pt, j, &tempHit, &tempNorm, &tempEdgeD); if (dist < closestD) { closestD = dist; - pointA = j; - pointB = i; - edgeD = tempEdgeD; - norm = tempNorm; - hitPt = tempHit; + if (pointA) *pointA = j; + if (pointB) *pointB = i; + if (edgeD) *edgeD = tempEdgeD; + if (norm) *norm = tempNorm; + if (hitPt) *hitPt = tempHit; } } return closestD; } -float Body::getClosestPointOnEdge (const Vector2 &pt, int edgeNum, Vector2 &hitPt, Vector2 &norm, float &edgeD) const { +float Body::getClosestPointOnEdge (const Vector2 &pt, int edgeNum, Vector2 *hitPt, Vector2 *norm, float *edgeD) const { float dist = 0.0f; Vector2 ptA = mPointMasses[edgeNum].Position; Vector2 ptB = mPointMasses[edgeNum < mPointCount-1 ? edgeNum+1 : 0].Position; - hitPt = Vector2::Zero; - norm = Vector2::Zero; - edgeD = 0.0f; Vector2 toP = pt-ptA; Vector2 E = mEdgeInfo[edgeNum].dir; // get the length of the edge, and use that to normalize the vector float edgeLength = mEdgeInfo[edgeNum].length; // normal Vector2 n = E.getPerpendicular(); + // + if (hitPt) *hitPt = Vector2::Zero; + if (norm) *norm = Vector2::Zero; + if (edgeD) *edgeD = 0.0f; // calculate the distance! float x = toP.dotProduct(E); if (x <= 0.0f) { // x is outside the line segment, distance is from pt to ptA dist = (pt-ptA).length(); - hitPt = ptA; - edgeD = 0.0f; - norm = n; + if (hitPt) *hitPt = ptA; + if (edgeD) *edgeD = 0.0f; + if (norm) *norm = n; } else if (x >= edgeLength) { // x is outside of the line segment, distance is from pt to ptB dist = (pt-ptB).length(); - hitPt = ptB; - edgeD = 1.0f; - norm = n; + if (hitPt) *hitPt = ptB; + if (edgeD) *edgeD = 1.0f; + if (norm) *norm = n; } else { // point lies somewhere on the line segment dist = fabsf(toP.crossProduct(E)); - hitPt = ptA+(E*x); - edgeD = x/edgeLength; - norm = n; + if (hitPt) *hitPt = ptA+(E*x); + if (edgeD) *edgeD = x/edgeLength; + if (norm) *norm = n; } return dist; } -float Body::getClosestPointOnEdgeSquared (const Vector2 &pt, int edgeNum, Vector2 &hitPt, Vector2 &norm, float &edgeD) const { +float Body::getClosestPointOnEdgeSquared (const Vector2 &pt, int edgeNum, Vector2 *hitPt, Vector2 *norm, float *edgeD) const { float dist = 0.0f; Vector2 ptA = mPointMasses[edgeNum].Position; Vector2 ptB = mPointMasses[edgeNum < mPointCount-1 ? edgeNum+1 : 0].Position; - hitPt = Vector2::Zero; - norm = Vector2::Zero; - edgeD = 0.0f; Vector2 toP = pt-ptA; Vector2 E = mEdgeInfo[edgeNum].dir; // get the length of the edge, and use that to normalize the vector float edgeLength = mEdgeInfo[edgeNum].length; // normal Vector2 n = E.getPerpendicular(); + // + if (hitPt) *hitPt = Vector2::Zero; + if (norm) *norm = Vector2::Zero; + if (edgeD) *edgeD = 0.0f; // calculate the distance! float x = toP.dotProduct(E); if (x <= 0.0f) { // x is outside the line segment, distance is from pt to ptA dist = (pt-ptA).lengthSquared(); //Vector2.DistanceSquared(ref pt, ref ptA, out dist); - hitPt = ptA; - edgeD = 0.0f; - norm = n; + if (hitPt) *hitPt = ptA; + if (edgeD) *edgeD = 0.0f; + if (norm) *norm = n; //printf("getClosestPointonEdgeSquared - closest is ptA: %f\n", dist); } else if (x >= edgeLength) { // x is outside of the line segment, distance is from pt to ptB dist = (pt-ptB).lengthSquared(); //Vector2.DistanceSquared(ref pt, ref ptB, out dist); - hitPt = ptB; - edgeD = 1.0f; - norm = n; + if (hitPt) *hitPt = ptB; + if (edgeD) *edgeD = 1.0f; + if (norm) *norm = n; //printf("getClosestPointonEdgeSquared - closest is ptB: %f\n", dist); } else { // point lies somewhere on the line segment dist = toP.crossProduct(E); //Vector3.Cross(ref toP3, ref E3, out E3); dist *= dist; - hitPt = ptA+(E*x); - edgeD = x/edgeLength; - norm = n; + if (hitPt) *hitPt = ptA+(E*x); + if (edgeD) *edgeD = x/edgeLength; + if (norm) *norm = n; //printf("getClosestPointonEdgeSquared - closest is at %f: %f\n", edgeD, dist); } return dist; } -int Body::getClosestPointMass (const Vector2 &pos, float &dist) const { +int Body::getClosestPointMass (const Vector2 &pos, float *dist) const { float closestSQD = 100000.0f; int closest = -1, c = mPointCount; for (int i = 0; i < c-1; ++i) { @@ -427,7 +428,7 @@ int Body::getClosestPointMass (const Vector2 &pos, float &dist) const { closest = i; } } - dist = sqrtf(closestSQD); + if (dist) *dist = sqrtf(closestSQD); return closest; } diff --git a/src/jelly/Body.h b/src/jelly/Body.h index 470a520..ade669a 100644 --- a/src/jelly/Body.h +++ b/src/jelly/Body.h @@ -45,7 +45,7 @@ public: void setMassAll (float mass); void setMassIndividual (int index, float mass); - void setMassFromList (std::vector masses); + void setMassFromList (const std::vector &masses); int getMaterial () const { return mMaterial; } void setMaterial (int val) { mMaterial = val; } @@ -80,10 +80,10 @@ public: bool contains (const Vector2 &pt) const; - float getClosestPoint (const Vector2 &pt, Vector2 &hitPt, Vector2 &norm, int &pointA, int &pointB, float &edgeD) const; - float getClosestPointOnEdge (const Vector2 &pt, int edgeNum, Vector2 &hitPt, Vector2 &norm, float &edgeD) const; - float getClosestPointOnEdgeSquared (const Vector2 &pt, int edgeNum, Vector2 &hitPt, Vector2 &norm, float &edgeD) const; - int getClosestPointMass (const Vector2 &pos, float &dist) const; + float getClosestPoint (const Vector2 &pt, Vector2 *hitPt, Vector2 *norm, int *pointA, int *pointB, float *edgeD) const; + float getClosestPointOnEdge (const Vector2 &pt, int edgeNum, Vector2 *hitPt, Vector2 *norm, float *edgeD) const; + float getClosestPointOnEdgeSquared (const Vector2 &pt, int edgeNum, Vector2 *hitPt, Vector2 *norm, float *edgeD) const; + int getClosestPointMass (const Vector2 &pos, float *dist) const; int getPointMassCount () const { return mPointCount; } PointMass *getPointMass (int index) { return &mPointMasses[index]; } diff --git a/src/jelly/World.cpp b/src/jelly/World.cpp index a128512..afb445f 100644 --- a/src/jelly/World.cpp +++ b/src/jelly/World.cpp @@ -166,7 +166,7 @@ void World::getClosestPointMass (const Vector2 &pt, int &bodyID, int &pmID) { float closestD = 1000.0f; for (unsigned int i = 0; i < mBodies.size(); ++i) { float dist = 0.0f; - int pm = mBodies[i]->getClosestPointMass(pt, dist); + int pm = mBodies[i]->getClosestPointMass(pt, &dist); if (dist < closestD) { closestD = dist; bodyID = i; @@ -475,7 +475,7 @@ void World::bodyCollide (Body *bA, Body *bB, std::vector &inf continue; }*/ // test against this edge. - float dist = bB->getClosestPointOnEdgeSquared(pt, j, hitPt, norm, edgeD); + float dist = bB->getClosestPointOnEdgeSquared(pt, j, &hitPt, &norm, &edgeD); //printf("bodyCollide - dist:%f\n", dist); // only perform the check if the normal for this edge is facing AWAY from the point normal. float dot = ptNorm.dotProduct(norm); @@ -614,7 +614,7 @@ void World::_addBoundaryBefore (Body::BodyBoundary *me, Body::BodyBoundary *toBe void World::_logBoundaries () const { - // first, find the "first" boundary in the list... + // first, find the "first" boundary in the list if (mBodies.size() == 0) return; Body::BodyBoundary *bb = &mBodies[0]->mBoundStart; while (bb->prev) bb = bb->prev; -- 2.11.4.GIT