2010-09-04 18:18:14 +08:00
|
|
|
/* Copyright (c) 2007 Scott Lembcke
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-05-21 14:58:22 +08:00
|
|
|
#include "chipmunk_private.h"
|
2010-09-04 18:18:14 +08:00
|
|
|
#include "chipmunk_unsafe.h"
|
|
|
|
|
|
|
|
cpPolyShape *
|
|
|
|
cpPolyShapeAlloc(void)
|
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
return (cpPolyShape *)cpcalloc(1, sizeof(cpPolyShape));
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
2012-03-21 14:53:57 +08:00
|
|
|
static cpBB
|
2010-09-04 18:18:14 +08:00
|
|
|
cpPolyShapeTransformVerts(cpPolyShape *poly, cpVect p, cpVect rot)
|
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
cpVect *src = poly->verts;
|
|
|
|
cpVect *dst = poly->tVerts;
|
|
|
|
|
|
|
|
cpFloat l = (cpFloat)INFINITY, r = -(cpFloat)INFINITY;
|
|
|
|
cpFloat b = (cpFloat)INFINITY, t = -(cpFloat)INFINITY;
|
|
|
|
|
|
|
|
for(int i=0; i<poly->numVerts; i++){
|
|
|
|
cpVect v = cpvadd(p, cpvrotate(src[i], rot));
|
|
|
|
|
|
|
|
dst[i] = v;
|
|
|
|
l = cpfmin(l, v.x);
|
|
|
|
r = cpfmax(r, v.x);
|
|
|
|
b = cpfmin(b, v.y);
|
|
|
|
t = cpfmax(t, v.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cpBBNew(l, b, r, t);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cpPolyShapeTransformAxes(cpPolyShape *poly, cpVect p, cpVect rot)
|
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
cpSplittingPlane *src = poly->planes;
|
|
|
|
cpSplittingPlane *dst = poly->tPlanes;
|
|
|
|
|
|
|
|
for(int i=0; i<poly->numVerts; i++){
|
|
|
|
cpVect n = cpvrotate(src[i].n, rot);
|
|
|
|
dst[i].n = n;
|
|
|
|
dst[i].d = cpvdot(p, n) + src[i].d;
|
|
|
|
}
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static cpBB
|
2012-03-21 14:53:57 +08:00
|
|
|
cpPolyShapeCacheData(cpPolyShape *poly, cpVect p, cpVect rot)
|
2010-09-04 18:18:14 +08:00
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
cpPolyShapeTransformAxes(poly, p, rot);
|
|
|
|
cpBB bb = poly->shape.bb = cpPolyShapeTransformVerts(poly, p, rot);
|
|
|
|
|
|
|
|
return bb;
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-21 14:53:57 +08:00
|
|
|
cpPolyShapeDestroy(cpPolyShape *poly)
|
2010-09-04 18:18:14 +08:00
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
cpfree(poly->verts);
|
|
|
|
cpfree(poly->planes);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
2012-06-12 09:38:38 +08:00
|
|
|
static void
|
|
|
|
cpPolyShapeNearestPointQuery(cpPolyShape *poly, cpVect p, cpNearestPointQueryInfo *info){
|
|
|
|
int count = poly->numVerts;
|
|
|
|
cpSplittingPlane *planes = poly->tPlanes;
|
|
|
|
cpVect *verts = poly->tVerts;
|
|
|
|
|
|
|
|
cpVect v0 = verts[count - 1];
|
|
|
|
cpFloat minDist = INFINITY;
|
|
|
|
cpVect closestPoint = cpvzero;
|
|
|
|
cpBool outside = cpFalse;
|
|
|
|
|
|
|
|
for(int i=0; i<count; i++){
|
|
|
|
if(cpSplittingPlaneCompare(planes[i], p) > 0.0f) outside = cpTrue;
|
|
|
|
|
|
|
|
cpVect v1 = verts[i];
|
|
|
|
cpVect closest = cpClosetPointOnSegment(p, v0, v1);
|
|
|
|
|
|
|
|
cpFloat dist = cpvdist(p, closest);
|
|
|
|
if(dist < minDist){
|
|
|
|
minDist = dist;
|
|
|
|
closestPoint = closest;
|
|
|
|
}
|
|
|
|
|
|
|
|
v0 = v1;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->shape = (cpShape *)poly;
|
|
|
|
info->p = closestPoint; // TODO div/0
|
|
|
|
info->d = (outside ? minDist : -minDist);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-03-21 14:53:57 +08:00
|
|
|
cpPolyShapeSegmentQuery(cpPolyShape *poly, cpVect a, cpVect b, cpSegmentQueryInfo *info)
|
2010-09-04 18:18:14 +08:00
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
cpSplittingPlane *axes = poly->tPlanes;
|
|
|
|
cpVect *verts = poly->tVerts;
|
|
|
|
int numVerts = poly->numVerts;
|
|
|
|
|
|
|
|
for(int i=0; i<numVerts; i++){
|
|
|
|
cpVect n = axes[i].n;
|
|
|
|
cpFloat an = cpvdot(a, n);
|
|
|
|
if(axes[i].d > an) continue;
|
|
|
|
|
|
|
|
cpFloat bn = cpvdot(b, n);
|
|
|
|
cpFloat t = (axes[i].d - an)/(bn - an);
|
|
|
|
if(t < 0.0f || 1.0f < t) continue;
|
|
|
|
|
|
|
|
cpVect point = cpvlerp(a, b, t);
|
|
|
|
cpFloat dt = -cpvcross(n, point);
|
|
|
|
cpFloat dtMin = -cpvcross(n, verts[i]);
|
|
|
|
cpFloat dtMax = -cpvcross(n, verts[(i+1)%numVerts]);
|
|
|
|
|
|
|
|
if(dtMin <= dt && dt <= dtMax){
|
|
|
|
info->shape = (cpShape *)poly;
|
|
|
|
info->t = t;
|
|
|
|
info->n = n;
|
|
|
|
}
|
|
|
|
}
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const cpShapeClass polyClass = {
|
2012-06-12 09:38:38 +08:00
|
|
|
CP_POLY_SHAPE,
|
|
|
|
(cpShapeCacheDataImpl)cpPolyShapeCacheData,
|
|
|
|
(cpShapeDestroyImpl)cpPolyShapeDestroy,
|
|
|
|
(cpShapeNearestPointQueryImpl)cpPolyShapeNearestPointQuery,
|
|
|
|
(cpShapeSegmentQueryImpl)cpPolyShapeSegmentQuery,
|
2010-09-04 18:18:14 +08:00
|
|
|
};
|
|
|
|
|
2011-05-21 14:58:22 +08:00
|
|
|
cpBool
|
|
|
|
cpPolyValidate(const cpVect *verts, const int numVerts)
|
2010-09-04 18:18:14 +08:00
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
for(int i=0; i<numVerts; i++){
|
|
|
|
cpVect a = verts[i];
|
|
|
|
cpVect b = verts[(i+1)%numVerts];
|
|
|
|
cpVect c = verts[(i+2)%numVerts];
|
|
|
|
|
|
|
|
if(cpvcross(cpvsub(b, a), cpvsub(c, a)) > 0.0f){
|
|
|
|
return cpFalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cpTrue;
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cpPolyShapeGetNumVerts(cpShape *shape)
|
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
cpAssertHard(shape->klass == &polyClass, "Shape is not a poly shape.");
|
|
|
|
return ((cpPolyShape *)shape)->numVerts;
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cpVect
|
|
|
|
cpPolyShapeGetVert(cpShape *shape, int idx)
|
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
cpAssertHard(shape->klass == &polyClass, "Shape is not a poly shape.");
|
|
|
|
cpAssertHard(0 <= idx && idx < cpPolyShapeGetNumVerts(shape), "Index out of range.");
|
|
|
|
|
|
|
|
return ((cpPolyShape *)shape)->verts[idx];
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2012-11-20 16:15:21 +08:00
|
|
|
setUpVerts(cpPolyShape *poly, int numVerts, const cpVect *verts, cpVect offset)
|
2010-09-04 18:18:14 +08:00
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
// Fail if the user attempts to pass a concave poly, or a bad winding.
|
|
|
|
cpAssertHard(cpPolyValidate(verts, numVerts), "Polygon is concave or has a reversed winding. Consider using cpConvexHull() or CP_CONVEX_HULL().");
|
|
|
|
|
|
|
|
poly->numVerts = numVerts;
|
|
|
|
poly->verts = (cpVect *)cpcalloc(2*numVerts, sizeof(cpVect));
|
|
|
|
poly->planes = (cpSplittingPlane *)cpcalloc(2*numVerts, sizeof(cpSplittingPlane));
|
|
|
|
poly->tVerts = poly->verts + numVerts;
|
|
|
|
poly->tPlanes = poly->planes + numVerts;
|
|
|
|
|
|
|
|
for(int i=0; i<numVerts; i++){
|
|
|
|
cpVect a = cpvadd(offset, verts[i]);
|
|
|
|
cpVect b = cpvadd(offset, verts[(i+1)%numVerts]);
|
|
|
|
cpVect n = cpvnormalize(cpvperp(cpvsub(b, a)));
|
2010-09-04 18:18:14 +08:00
|
|
|
|
2012-06-12 09:38:38 +08:00
|
|
|
poly->verts[i] = a;
|
|
|
|
poly->planes[i].n = n;
|
|
|
|
poly->planes[i].d = cpvdot(n, a);
|
|
|
|
}
|
|
|
|
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cpPolyShape *
|
2012-11-20 16:15:21 +08:00
|
|
|
cpPolyShapeInit(cpPolyShape *poly, cpBody *body, int numVerts, const cpVect *verts, cpVect offset)
|
2010-09-04 18:18:14 +08:00
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
setUpVerts(poly, numVerts, verts, offset);
|
|
|
|
cpShapeInit((cpShape *)poly, &polyClass, body);
|
2010-09-04 18:18:14 +08:00
|
|
|
|
2012-06-12 09:38:38 +08:00
|
|
|
return poly;
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cpShape *
|
|
|
|
cpPolyShapeNew(cpBody *body, int numVerts, cpVect *verts, cpVect offset)
|
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
return (cpShape *)cpPolyShapeInit(cpPolyShapeAlloc(), body, numVerts, verts, offset);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cpPolyShape *
|
|
|
|
cpBoxShapeInit(cpPolyShape *poly, cpBody *body, cpFloat width, cpFloat height)
|
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
cpFloat hw = width/2.0f;
|
|
|
|
cpFloat hh = height/2.0f;
|
|
|
|
|
|
|
|
return cpBoxShapeInit2(poly, body, cpBBNew(-hw, -hh, hw, hh));
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cpPolyShape *
|
|
|
|
cpBoxShapeInit2(cpPolyShape *poly, cpBody *body, cpBB box)
|
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
cpVect verts[] = {
|
|
|
|
cpv(box.l, box.b),
|
|
|
|
cpv(box.l, box.t),
|
|
|
|
cpv(box.r, box.t),
|
|
|
|
cpv(box.r, box.b),
|
|
|
|
};
|
|
|
|
|
|
|
|
return cpPolyShapeInit(poly, body, 4, verts, cpvzero);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cpShape *
|
|
|
|
cpBoxShapeNew(cpBody *body, cpFloat width, cpFloat height)
|
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
return (cpShape *)cpBoxShapeInit(cpPolyShapeAlloc(), body, width, height);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|
|
|
|
|
2012-03-21 14:53:57 +08:00
|
|
|
cpShape *
|
|
|
|
cpBoxShapeNew2(cpBody *body, cpBB box)
|
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
return (cpShape *)cpBoxShapeInit2(cpPolyShapeAlloc(), body, box);
|
2012-03-21 14:53:57 +08:00
|
|
|
}
|
|
|
|
|
2010-09-04 18:18:14 +08:00
|
|
|
// Unsafe API (chipmunk_unsafe.h)
|
|
|
|
|
|
|
|
void
|
|
|
|
cpPolyShapeSetVerts(cpShape *shape, int numVerts, cpVect *verts, cpVect offset)
|
|
|
|
{
|
2012-06-12 09:38:38 +08:00
|
|
|
cpAssertHard(shape->klass == &polyClass, "Shape is not a poly shape.");
|
|
|
|
cpPolyShapeDestroy((cpPolyShape *)shape);
|
|
|
|
setUpVerts((cpPolyShape *)shape, numVerts, verts, offset);
|
2010-09-04 18:18:14 +08:00
|
|
|
}
|