Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

l0k0

Member Since 17 Aug 2009
Offline Last Active May 19 2013 03:56 PM
-----

Posts I've Made

In Topic: What's the Deal with them Indices?

14 April 2013 - 07:25 PM

It might be easier to think of it this way:

 

 

Vector2 vertices[] = 

{

       Vector2(-1.0f, -1.0f),  // Index 0

       Vector2(1.0f, -1.0f),   // Index 1

       Vector2(-1.0f,  1.0f),  // Index 2

       Vector2(1.0f,  1.0f),   // Index 3

};

 

 

Triangle indices[] = 

{

     Triangle(0, 3, 1),  // Bottom left to top right to bottom right triangle

     Triangle(0, 2, 3),  // Bottom left to top left to top right triangle

};

 

Each index is referencing a point on the quad.  You are ultimately drawing 2 triangles to achieve this.  For quads, index buffers don't help all that much, but for something like a cube the amount of data sent to the gpu can get a lot smaller (especially when encoding more than just positions in the vertices) when using index buffers of 16 bit offsets instead of 3 whole floats again.

 

Open GL takes things as flat arrays of floats and integral types.  As such, you have to think of the indices and vertices as groups inside these arrays.  Does that make more sense?


In Topic: Matrix LookAt problem

13 April 2013 - 02:13 AM

The view matrix transforms from world to screen space.  It is the inverse of the camera's world matrix.  

 

We know that the inverse of a rotation matrix is its transpose.  Since what you are calculating is the view matrix, you need to transpose the upper 3x3 portion of the matrix to get the desired result.  Notice if you do that with your results, that you get the same thing as the library's matrix (don't just negate that's silly).  You can either transpose the matrix after setting the 3 columns or just set it directly.

 

What you are doing now is right for the translation, but is giving the world space rotation (when really we want its inverse).  Use your left hand given your input, have it point from the "eye" to the "at".  Hopefully it then becomes clear how the properly calculated view matrix is undoing that transformation.


In Topic: Trouble implementing SAT collision

18 February 2013 - 03:08 PM

I would test the intersection code in isolation.  Debug draw the returned normal and depth and if they are intersecting.  If that seems accurate, it is probably an issue with the response code.  Also, post some more of the response code here.  What you have there isn't really telling us much.  Got to see the nitty gritty math to help determine what's wrong.


In Topic: Trouble implementing SAT collision

16 February 2013 - 10:23 PM

I'd do it like this:

 

 

 

void GetProjectionOnAxis(const Vector2 * pVertices, const int N, const Vector2& axisN, float &projMin, float &projMax) {
    projMin = projMax = Dot(pVertices[0], axisN); // axisN assumed to be normalized
    for (int i = 1; i < N; ++i) {
        projMin = Min(projMin, pVertices[i]);
        projMax = Max(projMax, pVertices[i]);
    }
} 

float GetOverlapOnAxis(const float projMinA, const float projMaxA, const float projMinB, const float projMaxB) {
    float minP = Min(projMinA, projMinB);
    float maxP = Max(projMaxA, projMaxB);
    float overlap = maxP - minP;    // negative if no intersection
    return overlap;
}

struct SATResult {
    Vector2 m_normal;
    float m_depth;
    bool m_intersect;
};

bool TestPolygonPolygonSAT(const Vector2 * pVerts0, const int n0, const Vector2 * pVerts1, const int n1, SATResult& satOut) {
    jlVector2 edge, edgePerp, edgePerpN;    
    float min0, max0, min1, max1;
    float overlap, minOverlap = -F32_MAX;
    jlVector4 minNormal;
    satOut.m_intersect = false; 
    int j, i;
    for (j = n0 - 1, i = 0; i < n0; j = i, ++i) {
        // compute our orthogonal axis for each edge
        edge = pVerts0[i] - pVerts0[j];
        edgePerp = RightPerp(edge); // y, -x
        edgePerpN = Normalize(edgePerp);
        // get min/max projection on each axis
        GetProjectionOnAxis(verts0, n0, edgePerpN, min0, max0);
        GetProjectionOnAxis(verts1, n1, edgePerpN, min1, max1);
        // if no overlap, no intersection
        overlap = jlOverlapOnAxis(min0, max0, min1, max1);
        // otherwise store minimum overlap value and the normal
        if (overlap < 0.0f) {
            return false; // have to intersect on every tested axis for SAT to return true
        } else if (overlap < minOverlap) {
            minOverlap = overlap;
            minNormal = edgePerpN;
        }
    }
    // now do the same thing, on pVerts1 axis
    for (j = n0 - 1, i = 0; i < n0; j = i, ++i) {
        // compute our orthogonal axis for each edge
        edge = pVerts1[i] - pVerts1[j];
        edgePerp = RightPerp(edge); // y, -x
        edgePerpN = Normalize(edgePerp);
        // get min/max projection on each axis
        GetProjectionOnAxis(verts0, n0, edgePerpN, min0, max0);
        GetProjectionOnAxis(verts1, n1, edgePerpN, min1, max1);
        // if no overlap, no intersection
        overlap = jlOverlapOnAxis(min0, max0, min1, max1);
        // otherwise store minimum overlap value and the normal
        if (overlap < 0.0f) {
            return false; // have to intersect on every tested axis for SAT to return true
        } else if (overlap < minOverlap) {
            minOverlap = overlap;
            minNormal = edgePerpN;
        }
    }
    // if we get here they are intersecting
    satOut.m_normal = minNormal;

    satOut.m_depth = minOverlap;
    satOut.m_intersect = true;
    return true;
}

 

 

You would probably want to break the TestPolygonPolygonSAT into a helper function you call twice.  Also, I was lazy and just did this on the spot.  Pretty sure it is right though smile.png.


In Topic: Find point on line closest to another point

12 February 2013 - 02:28 AM

This can be done much simpler.  We know that we can project a vector a onto b with (dot(a, b) / dot(b, b)) * b.  We can apply this by subtracting the point by one of the points on the line, projecting onto the line vector using the above, clamping our t value if needed, and adding to that point on the line again.  Some code for you:

 

 

// p0 and p1 define the line segment, pt is an arbitrary point in space
const Vector3 ClosestPointOnSegment(const Vector3& p0, const Vector3& p1, const Vector3& pt) {
    const Vector3 lineSeg = p1 - p0; // seg vec
    // saturate clamps from 0 to 1, which will get us a point between p0 and p1
    // if we want the closest point on the infinite line defined by both points we would 
    // omit the saturate
    const float t = Saturate(Dot((pt - p0), lineSeg) / Dot(lineSeg, lineSeg));  
    return p0 + lineSeg * t;
}
 

PARTNERS