danfis / libccd

Library for collision detection between two convex shapes
Other
496 stars 110 forks source link

Any example for Convex polytope? #12

Closed tl3shi closed 9 years ago

tl3shi commented 9 years ago

Is there any example for collision detection between two convex polytopes?

danfis commented 9 years ago

Box is a convex polytope and there are examples in src/testsuites for that. If you mean a general polytope then there is no example for that -- it would highly depend on the definition of the polytope anyway. If you need to work with polytopes just think about a proper support function (or search literature).

tl3shi commented 9 years ago

Thank you for you reply. If the convex polyhedron is defined as following:

Polyhedron:
    vector<Polygon3D>;
Polygon3D:
    uvec3 normal;
    vector<vec3> points;

In the support function ccdSupport(const void * convex, const ccd_vec3_t *_dir, ccd_vec3_t *v), I need fill the variable v, which should be an intersection point between one Polygon3D and the ray with normal _dir , and the intersection point should be the one that has maximum dot value with the normal.
Am i right?

tl3shi commented 9 years ago

Or just select the known points of the Polygon3D, like the following code segment, I copyed from here

I am doubting that the following way is OK.

static void ccdSupportConvex(const void *obj, const ccd_vec3_t *_dir, ccd_vec3_t *v)
{
    const ccd_convex_t *c = (const ccd_convex_t *)obj;
    ccd_vec3_t dir, p;
    ccd_real_t maxdot, dot;
    size_t i;
    dReal *curp;

    ccdVec3Copy(&dir, _dir);
    ccdQuatRotVec(&dir, &c->o.rot_inv);

    maxdot = -CCD_REAL_MAX;
    curp = c->convex->points;
    for (i = 0; i < c->convex->pointcount; i++, curp += 3){
        ccdVec3Set(&p, curp[0], curp[1], curp[2]);
        dot = ccdVec3Dot(&dir, &p);
        if (dot > maxdot){
            ccdVec3Copy(v, &p);
            maxdot = dot;
        }
    }
    // transform support vertex
    ccdQuatRotVec(v, &c->o.rot);
    ccdVec3Add(v, &c->o.pos);
}
danfis commented 9 years ago

The ccdSupportConvex() function seems ok -- the support point is the vertex from the polyhedron that is furthest along the direction vector (dot product says you the length of projection on the direction vector)... But if you are unsure, try some literature.

danfis commented 9 years ago

Now when I think about it, I think the code you posted was originaly written by me (as part of ODE physic SDK). So I would say it is correct. :)

tl3shi commented 9 years ago

Thanks. It is correct. Some papers said that the procedure can be optimazed to constant time when in moving environment.