Example #1
0
// Create the left walls, start must point to a segment with a wall on the left and a leading
// non wall segment.
// FIXME: Does not work for a closed ring "by design".
void buildWalls(tTrackSeg *start, int side) {

	if (start == NULL) {
		return;
	}

	tTrackSeg *current = start;
	bool close = false;

	// Going down the wall.
	do {
		tTrackSeg* s = current->side[side];
		tTrackSeg* p = current->prev->side[side];
		tTrackSeg* n = current->next->side[side];

		// Current segment is a wall?
		if (s != NULL && s->style == TR_WALL && s->side[side] != NULL) {

			float h = s->height;
			t3Dd svl = s->vertex[TR_SL];
			t3Dd svr = s->vertex[TR_SR];
			t3Dd evl = s->vertex[TR_EL];
			t3Dd evr = s->vertex[TR_ER];
			static float weps = 0.01f;

			// Close the start with a ploygon?
			if (p == NULL || p->style != TR_WALL ||
				(fabs(p->vertex[TR_EL].x - svl.x) > weps) ||
				(fabs(p->vertex[TR_ER].x - svr.x) > weps) ||
				(fabs(h - p->height) > weps) ||
				fixedid == 0)
			{
				// Enough space in array?
				if (fixedid >= sizeof(fixedobjects)/sizeof(fixedobjects[0])) {
					GfError("fixedobjects full in %s, line %d\n", __FILE__, __LINE__);
					return;
				}

				if (close == true) {
					dtEndComplexShape();
					GfError("Shape not closed %s, line %d\n", __FILE__, __LINE__);
				}

				// Start new shape.
				fixedobjects[fixedid] = dtNewComplexShape();
				fixedid++;
				close = true;

				dtBegin(DT_POLYGON);
					dtVertex(svl.x, svl.y, svl.z);
					dtVertex(svr.x, svr.y, svr.z);
					dtVertex(svr.x, svr.y, svr.z + h);
					dtVertex(svl.x, svl.y, svl.z + h);
				dtEnd();
			}

			// Build sides, left, top, right. Depending on how we want to use it we
			// can remove top perhaps.
			// TODO: do we want the top poly?
			if (close == true) {
				// Left.
				dtBegin(DT_POLYGON);
					dtVertex(svl.x, svl.y, svl.z);
					dtVertex(svl.x, svl.y, svl.z + h);
					dtVertex(evl.x, evl.y, evl.z + h);
					dtVertex(evl.x, evl.y, evl.z);
				dtEnd();
				/*
				// Top.
				dtBegin(DT_POLYGON);
					dtVertex(svl.x, svl.y, svl.z + h);
					dtVertex(svr.x, svr.y, svr.z + h);
					dtVertex(evr.x, evr.y, evr.z + h);
					dtVertex(evl.x, evl.y, evl.z + h);
				dtEnd();*/
				// Right.
				dtBegin(DT_POLYGON);
					dtVertex(svr.x, svr.y, svr.z + h);
					dtVertex(svr.x, svr.y, svr.z);
					dtVertex(evr.x, evr.y, evr.z);
					dtVertex(evr.x, evr.y, evr.z + h);
				dtEnd();
			} else {
				GfError("Shape not open %s, line %d\n", __FILE__, __LINE__);
			}

			// Close the end with a ploygon?
			if (n == NULL || n->style != TR_WALL ||
				(fabs(n->vertex[TR_SL].x - evl.x) > weps) ||
				(fabs(n->vertex[TR_SR].x - evr.x) > weps) ||
				(fabs(h - n->height) > weps))
			{
				if (close == true) {
					dtBegin(DT_POLYGON);
						dtVertex(svl.x, svl.y, svl.z);
						dtVertex(svr.x, svr.y, svr.z);
						dtVertex(svr.x, svr.y, svr.z + h);
						dtVertex(svl.x, svl.y, svl.z + h);
					dtEnd();

					dtEndComplexShape();
					close = false;
				} else {
					GfError("Shape not open %s, line %d\n", __FILE__, __LINE__);
				}
			}

		}

		current = current->next;

	} while (current != start);

}
Example #2
0
int main(int argc, char** argv)
{

    object1.id = 1;
    object2.id = 2;

    /*vec3 boxMax1 = vec3(2, 2, 2);
    vec3 boxMax2 = vec3(1, 1, 1);
    
    boxobj1 = BoundingBox(-boxMax1, boxMax1);
    boxobj2 = BoundingBox(-boxMax2, boxMax2);
    */
    boxobj1 = BoundingBox(vec3(-0.9438, -0.4669, -0.61679), vec3(0.607788, 1.06996, 0.587146));
    boxobj2 = BoundingBox(vec3(1.056195, -0.46690, -0.6167920), vec3(2.6077880000000002, 1.0699599999999998, 0.58714600000000006));

    /*
    DtShapeRef shape1 = dtBox(boxMax1.x * 2, boxMax1.y * 2, boxMax1.z * 2);
    DtShapeRef shape2 = dtBox(boxMax2.x * 2, boxMax2.y * 2, boxMax2.z * 2);
    */

    vector<Point>  solid_points0;   
    vector<CP_Vector3D> points0 = boxobj1.GetAABBVertices();
    for(int i = 0; i < points0.size(); i++)
        solid_points0.push_back(Point(points0[i].x, points0[i].y, points0[i].z));

    vector<DtIndex> solid_indices0;
    auto indices = boxobj1.GetAABBIndices(); 
    for(auto it = indices.begin(); it != indices.end(); it++)
        solid_indices0.push_back(*it);
    DtShapeRef shape1 = dtNewComplexShape();
    dtVertexBase(&solid_points0[0]);
    for(int i = 0; i < solid_indices0.size()/3; i++)
    {
        dtBegin(DT_SIMPLEX);
        dtVertexIndex(solid_indices0[i*3+0]);
        dtVertexIndex(solid_indices0[i*3+1]);
        dtVertexIndex(solid_indices0[i*3+2]);
        dtEnd();
    }
    //dtVertexIndices(DT_POLYHEDRON, solid_indices0.size(), &solid_indices0[0]);
    dtEndComplexShape();

    vector<Point> solid_points1;
    vector<CP_Vector3D> points1 = boxobj2.GetAABBVertices();
    for(int i = 0; i < points1.size(); i++)
        solid_points1.push_back(Point(points1[i].x, points1[i].y, points1[i].z));
    
    auto indices1 = boxobj2.GetAABBIndices();
    vector<DtIndex> solid_indices1(indices1.begin(), indices1.end());
 
    DtShapeRef shape2 = dtNewComplexShape();
    dtVertexBase(&solid_points1[0]);
    for(int i = 0; i < solid_indices1.size()/3; i++)
    {
        dtBegin(DT_SIMPLEX);
        dtVertexIndex(solid_indices1[i*3+0]);
        dtVertexIndex(solid_indices1[i*3+1]);
        dtVertexIndex(solid_indices1[i*3+2]);
        dtEnd();
    } 
    //dtVertexIndices(DT_POLYHEDRON, solid_indices1.size(), &solid_indices1[0]);
    dtEndComplexShape();

    dtCreateObject(&object1, shape1); 
    dtCreateObject(&object2, shape2); 

    dtDisableCaching();

    dtSetDefaultResponse(collide1, DT_SIMPLE_RESPONSE, stdout);


    if(NO_DISPLAY) return 0;

    draw(argc, argv);

    return 0;
}