Commit 74e2a6c5 authored by Leon's avatar Leon

Some code optimization

parent ccd3e5b4
......@@ -15,7 +15,7 @@ namespace duodim
static bool twoPolygons(Collision* c, Body* a, Body* b);
private:
static float findAxisLeastPenetration(unsigned int* faceIdx, ConvexPolygon* cpa, ConvexPolygon* cpb);
static int clip(Vector2D n, float c, Vector2D* face);
static unsigned int clip(Vector2D n, float c, Vector2D* face);
static void findIncidentFace(Vector2D* v, ConvexPolygon* refShape, ConvexPolygon* incShape, unsigned int refIdx);
};
}
......
......@@ -9,6 +9,7 @@ namespace duodim
public:
static Transform* atPosition(int x, int y);
static Vector2D toLocalPosition(Matrix2 rotationMatrix, Vector2D worldPosition, Vector2D actualPosition);
static Vector2D getFrameOfReference(Matrix2 rotationMatrix, Vector2D referenceNormal);
Vector2D position;
Matrix2 rotMatrix;
......
......@@ -12,8 +12,8 @@ bool NarrowPhase::twoCircles(Collision* c, Body* a, Body* b)
{
bool result = false;
Circle* sa = static_cast<Circle*>(a->shape);
Circle* sb = static_cast<Circle*>(b->shape);
Circle* sa = dynamic_cast<Circle*>(a->shape);
Circle* sb = dynamic_cast<Circle*>(b->shape);
Vector2D betweenCenters = b->transform->position - a->transform->position;
float sqBetweenCentersDistance = betweenCenters.squaredMagnitude();
......@@ -40,8 +40,8 @@ bool NarrowPhase::twoCircles(Collision* c, Body* a, Body* b)
bool NarrowPhase::circlePolygon(Collision* c, Body* a, Body* b)
{
Circle* sa = static_cast<Circle*>(a->shape);
ConvexPolygon* sb = static_cast<ConvexPolygon*>(b->shape);
Circle* sa = dynamic_cast<Circle*>(a->shape);
ConvexPolygon* sb = dynamic_cast<ConvexPolygon*>(b->shape);
Transform* at = a->transform;
Transform* bt = b->transform;
......@@ -191,13 +191,12 @@ void NarrowPhase::findIncidentFace(Vector2D* v, ConvexPolygon* refShape, ConvexP
Transform* incT = incShape->body->transform;
// Calculate normal in incident's frame of reference -> to World space -> to incident's model space
Vector2D refNormal = incT->getLocalPosition(refT->getWorldPosition(refShape->normals[refIdx]));
Vector2D refNormal = Transform::getFrameOfReference(incT->rotMatrix.transpose(), Transform::getFrameOfReference(refT->rotMatrix, refShape->normals[refIdx]));
// Find most anti-normal face on incident polygon
int incidentFace = 0;
float minDot = FLT_MAX;
vector<Vector2D> incVertices = incShape->vertices;
for(unsigned int i = 0; i < incVertices.size(); ++i)
unsigned int incidentFace = 0;
for(unsigned int i = 0; i < incShape->vertices.size(); ++i)
{
float dot = refNormal * incShape->normals[i];
if(dot < minDot)
......@@ -208,15 +207,15 @@ void NarrowPhase::findIncidentFace(Vector2D* v, ConvexPolygon* refShape, ConvexP
}
// Assign face vertices for incidentFace
v[0] = incT->getWorldPosition(incVertices[incidentFace]);
incidentFace = incidentFace + 1 >= (int) incVertices.size() ? 0 : incidentFace + 1;
v[1] = incT->getWorldPosition(incVertices[incidentFace]);
v[0] = incT->getWorldPosition(incShape->vertices[incidentFace]);
incidentFace = (__int64) incidentFace + 1 >= incShape->vertices.size() ? 0 : incidentFace + 1;
v[1] = incT->getWorldPosition(incShape->vertices[incidentFace]);
refT = NULL;
incT = NULL;
}
int NarrowPhase::clip(Vector2D n, float c, Vector2D* face)
unsigned int NarrowPhase::clip(Vector2D n, float c, Vector2D* face)
{
unsigned int sp = 0;
......@@ -241,9 +240,8 @@ int NarrowPhase::clip(Vector2D n, float c, Vector2D* face)
if ((d1 * d2) < 0.0f) // less than to ignore -0.0f
{
// Push interesection point
float alpha = d1 / (d1 - d2);
Vector2D newFace1 = ((face[1] - face[0]) * alpha) + face[0];
out[sp++] = newFace1;
out[sp] = ((face[1] - face[0]) * (d1 / (d1 - d2))) + face[0];
++sp;
}
// Assign our new converted values
......@@ -256,8 +254,8 @@ int NarrowPhase::clip(Vector2D n, float c, Vector2D* face)
bool NarrowPhase::twoPolygons(Collision* c, Body* a, Body* b)
{
ConvexPolygon* sa = static_cast<ConvexPolygon*>(a->shape);
ConvexPolygon* sb = static_cast<ConvexPolygon*>(b->shape);
ConvexPolygon* sa = dynamic_cast<ConvexPolygon*>(a->shape);
ConvexPolygon* sb = dynamic_cast<ConvexPolygon*>(b->shape);
c->contactCount = 0u;
// Check for a separating axis with A's face planes
......
......@@ -13,6 +13,11 @@ Vector2D Transform::toLocalPosition(Matrix2 rotationMatrix, Vector2D worldPositi
return rotationMatrix * (worldPosition - actualPosition);
}
Vector2D Transform::getFrameOfReference(Matrix2 rotationMatrix, Vector2D referenceNormal)
{
return rotationMatrix * referenceNormal;
}
Transform::Transform()
: position(Vector2D()), rotation(0.f), rotMatrix(Matrix2::fromRadians(0.f)) {}
Transform::Transform(Vector2D position)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment