예제 #1
0
bool RigTriangulate(const Rig& rig, std::vector<Rig>& finalTriangles)
{
    Vector2dVector a;
    for(int i = 0; i < rig.size(); i++)
    {
        a.push_back(Vector2d(rig[i].x,rig[i].y));
    }
    Vector2dVector result;
    bool success = Triangulate::Process(a,result);
    
    // print out the results.
    int tcount = result.size()/3;
    for (int i=0; i<tcount; i++)
    {
        const Vector2d &p1 = result[i*3+0];
        const Vector2d &p2 = result[i*3+1];
        const Vector2d &p3 = result[i*3+2];
        
        Rig triangle;
        triangle.push_back(ccp(p1.GetX(),p1.GetY()));
        triangle.push_back(ccp(p2.GetX(),p2.GetY()));
        triangle.push_back(ccp(p3.GetX(),p3.GetY()));
        
        finalTriangles.push_back(triangle);
    }
    
    return success;
}
예제 #2
0
void CPhysics::drawSquare(b2Vec2* points,b2Vec2 center,float angle,int VertexCount, float Camera_Z)
{

	Vector2dVector a;

	for (int i = 0; i < VertexCount; i++)
	{
		a.push_back(Vector2d(points[i].x,points[i].y));
	}
	Vector2dVector result;
	Triangulate::Process(a,result);
	int tcount = result.size()/3;

	glColor3f(0.7,0.3,0.7);
	//glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

	glTranslatef(center.x *M2P,center.y*M2P,Camera_Z);


	glRotatef(angle*180.0/M_PI,0,0,1);
	glBegin(GL_TRIANGLES);

	for(int i=0;i<tcount;i++){
		if (i == 0)
			glColor3f(1,0,1);
		else if (i == 1)
			glColor3f(1,0,0);
		else if (i == 2)
			glColor3f(0,1,0);
		else if (i == 3)
			glColor3f(0,0,1);
		else if (i == 4)
			glColor3f(0,1,1);
		else if (i == 5)
			glColor3f(1,0,1);


		glVertex2f(result[i*3+0].GetX()*M2P,result[i*3+0].GetY()*M2P);
		glVertex2f(result[i*3+1].GetX()*M2P,result[i*3+1].GetY()*M2P);
		glVertex2f(result[i*3+2].GetX()*M2P,result[i*3+2].GetY()*M2P);
	}
	glEnd();
	glPopMatrix();

	glColor3f(1,1,1);
}
예제 #3
0
void ModuleBuildings::addBuildingRoof(Building* building, float height, float elevation){
    //create && fill vector a with polygon corners
    Vector2dVector a;
    bool first = true;
    for(auto corner : building->getCorners()) {
        if (first) { first=false; continue; }
        a.push_back( Vector2d(corner[0], corner[1]));
    }

    Vector2dVector result; // allocate an STL vector to hold the answer.
    Triangulate::Process(a,result); //  Invoke the triangulator to triangulate this polygon.

    //create roof
    height = (float)height * Config::get()->BUILDING_FLOOR_HEIGHT + elevation;
    int tcount = result.size()/3;
    for (int i=0; i<tcount; i++) {
        const Vector2d &p1 = result[i*3+0];
        const Vector2d &p2 = result[i*3+1];
        const Vector2d &p3 = result[i*3+2];

        //create triangle
        for (int j=0; j<3; j++) {
            r_geo_d->inds->addValue(r_geo_d->inds->size());
            r_geo_d->norms->addValue(Vec3f(0, 1, 0));
        }

        r_geo_d->pos->addValue(Vec3f(p1.GetX(), height, p1.GetY()));
        r_geo_d->pos->addValue(Vec3f(p2.GetX(), height, p2.GetY()));
        r_geo_d->pos->addValue(Vec3f(p3.GetX(), height, p3.GetY()));

        //r_geo_d->texs->addValue(Vec2f(p1.GetX()/factor, p1.GetY()/factor)); //use only border color of texture
        //r_geo_d->texs->addValue(Vec2f(p2.GetX()/factor, p2.GetY()/factor)); //use only border color of texture
        //r_geo_d->texs->addValue(Vec2f(p3.GetX()/factor, p3.GetY()/factor)); //use only border color of texture

        float mx = (p1.GetX() + p2.GetX() + p3.GetX() )/3.0;
        float my = (p1.GetY() + p2.GetY() + p3.GetY() )/3.0;

        r_geo_d->texs->addValue(convRTC(p1.GetX(), p1.GetY(), Vec2f(mx,my))); //use only border color of texture
        r_geo_d->texs->addValue(convRTC(p2.GetX(), p2.GetY(), Vec2f(mx,my))); //use only border color of texture
        r_geo_d->texs->addValue(convRTC(p3.GetX(), p3.GetY(), Vec2f(mx,my))); //use only border color of texture

        //r_geo_d->texs->addValue(Vec2f(0.0, 0.9)); //use only border color of texture
        //r_geo_d->texs->addValue(Vec2f(0.1, 0.9)); //use only border color of texture
        //r_geo_d->texs->addValue(Vec2f(0.0, 1.0)); //use only border color of texture
    }
}
예제 #4
0
void PRFilledPolygon::setPoints(Vector2dVector &points) {
    
    CC_SAFE_FREE(areaTrianglePoints);
    CC_SAFE_FREE(textureCoordinates);
    
    Vector2dVector triangulatedPoints = PRRatcliffTriangulator::triangulateVertices(points);
    
    areaTrianglePointCount = triangulatedPoints.size();
    areaTrianglePoints = (CCPoint*) malloc(sizeof(CCPoint) * areaTrianglePointCount);
    textureCoordinates = (CCPoint*) malloc(sizeof(CCPoint) * areaTrianglePointCount);
    
    for (int i = 0; i < areaTrianglePointCount; i++) {
        Vector2d v = (Vector2d)triangulatedPoints.at(i);
        areaTrianglePoints[i] = CCPointMake(v.GetX(), v.GetY());
    }
    
    calculateTextureCoordinates();
}
예제 #5
0
void CCutScene::createBackground()
{
    
    Point points[4] =
    {
        Point(0, 0), Point(0, 2048), Point(2048, 2048), Point(2048, 0)
    };
    Vector2dVector vcPoints;
    vcPoints.clear();
    for (int i = 0; i < 4; i++)
    {
        vcPoints.push_back(Vector2d(points[i].x, points[i].y));
    }
    
    PRFilledPolygon *filledPolygon = PRFilledPolygon::filledPolygonWithPointsAndTexture(vcPoints, "daanban.png");
    //filledPolygon->setPhysicsBody(PhysicsBody::createPolygon(points, 4));
    filledPolygon->setPosition(Vec2::ZERO + Vec2(0, (1536 - 2048) / 2));
    addChild(filledPolygon, 80);
}
예제 #6
0
LDEfloat Triangulate::Area(const Vector2dVector &contour)
{
    int n = contour.size();
    
    LDEfloat A=0.0f;
    
    for(int p=n-1,q=0; q<n; p=q++)
    {
        A+= contour[p].x*contour[q].y - contour[q].x*contour[p].y;
    }
    return A*0.5f;
}
예제 #7
0
bool PRKitDemoScene::init()
{
    if(!Layer::init())
    {
        return false;
    }
    
    auto color_bg = LayerColor::create(Color4B(125, 125, 125, 125));
    addChild(color_bg);
    
    auto demo_info_label = Label::create(demo_info(), "Helvetica", 25, Size(500, 200), TextHAlignment::LEFT);
    this->addChild(demo_info_label);
    demo_info_label->setAnchorPoint(Point(0.0f, 1.0f));
    demo_info_label->setColor(Color3B(255, 255, 255));
    demo_info_label->setPosition(VisibleRect::leftTop() + Point(60, -60));
    
    auto backMenuItem = MenuItemImage::create("backNormal.png", "backSelected.png", CC_CALLBACK_1(PRKitDemoScene::backCallback, this));
    auto restartMenuItem = MenuItemImage::create("refreshNormal.png", "refreshSelected.png", CC_CALLBACK_1(PRKitDemoScene::restartCallback, this));
    
    restartMenuItem->setPosition(VisibleRect::rightTop() + Point(-200, -80));
    backMenuItem->setPosition(restartMenuItem->getPosition() + Point(90, 0));
    auto menu = Menu::create(backMenuItem, restartMenuItem, NULL);
    menu->setPosition(Point::ZERO);
    this->addChild(menu, 300);
    
    //--------------------- START
    Vector2dVector polygonPoints;
    polygonPoints.push_back(Vector2d(100, 100));
    polygonPoints.push_back(Vector2d(200, 100));
    polygonPoints.push_back(Vector2d(300, 200));
    polygonPoints.push_back(Vector2d(400, 300));
    polygonPoints.push_back(Vector2d(500, 500));

    Texture2D *texture = Director::getInstance()->getTextureCache()->addImage("pattern1.png");
    PRFilledPolygon *filledPolygon = PRFilledPolygon::filledPolygonWithPointsAndTexture(polygonPoints, texture);
    addChild(filledPolygon);
    //---------------------- END
    
    return true;
}
예제 #8
0
double Triangulate::Area(const Vector2dVector &contour)
{

  int n = contour.size();

  double A=0.0;

  for(int p=n-1,q=0; q<n; p=q++)
  {
    A+= contour[p].GetX()*contour[q].GetY() - contour[q].GetX()*contour[p].GetY();
  }
return A*0.5;
}
예제 #9
0
PolygonSprite* PolygonSprite::initWithTexture(Texture2D* texture, b2Body* body, bool original)
{
	//gather all the vertices from our Box2D shape
	b2Fixture *originalFixture = body->GetFixtureList();
	b2PolygonShape *shape = (b2PolygonShape*)originalFixture->GetShape();
	int vertexCount = shape->GetVertexCount();

	Vector2dVector points;

	for(int i = 0; i < vertexCount; i++) {
		points.push_back( Vector2d(shape->GetVertex(i).x * PTM_RATIO, shape->GetVertex(i).y * PTM_RATIO) );
	}

	if(initWithPointsAndTexture(points, texture))
	{
		_body = body;
		_body->SetUserData(this);
		_original = original;
		// gets the center of the polygon
		_centroid = this->_body->GetLocalCenter();
		// assign an anchor point based on the center
		_anchorPoint = ccp(_centroid.x * PTM_RATIO / texture->getContentSize().width, 
								 _centroid.y * PTM_RATIO / texture->getContentSize().height);

		// more init stuff here later when you expand PolygonSprite

		// slice
		_sliceExited = false;
		_sliceEntered = false;
		_entryPoint.SetZero();
		_exitPoint.SetZero();
		_sliceEntryTime = 0;

		// fruits
		_state = kStateIdle;
	}

	return this;
}
예제 #10
0
float Triangulate::Area(const Vector2dVector &contour)
{
    
    int n = (int)contour.size();
    
    float A=0.0f;
    
    for(int p=n-1,q=0; q<n; p=q++)
    {
        A+= contour[p].GetX()*contour[q].GetY() - contour[q].GetX()*contour[p].GetY();
    }
    return A*0.5f;
}
NS_CC_BEGIN


Vector2dVector PRRatcliffTriangulator::triangulateVertices ( Vector2dVector aVertices ) 
{
	Vector2dVector*		pInputPointsForTriangulation = new Vector2dVector;

	for ( KDuint uIndex = 0; uIndex < aVertices.size ( ); uIndex++ )
	{
		Vector2d		tValue = (Vector2d) aVertices.at ( uIndex );
		pInputPointsForTriangulation->push_back ( tValue );
	}

	// Triangulate results
	Vector2dVector		aTriangulatedPoints;

	Triangulate::Process ( *pInputPointsForTriangulation, aTriangulatedPoints );

	delete pInputPointsForTriangulation;

	return aTriangulatedPoints;
}
예제 #12
0
void EasyPolygon::setPoints(Vector2dVector &points) {
    
    if (points.size() <= 0) {
        areaTrianglePointCount = 0;
        return;
    }
    
    CC_SAFE_FREE(areaTrianglePoints);
    CC_SAFE_FREE(textureCoordinates);
    
    /*Vector2dVector triangulatedPoints = PRRatcliffTriangulator::triangulateVertices(points);
    
    areaTrianglePointCount = (int)triangulatedPoints.size();
    areaTrianglePoints = (Point*) malloc(sizeof(Point) * areaTrianglePointCount);
    textureCoordinates = (Point*) malloc(sizeof(Point) * areaTrianglePointCount);
    
    for (int i = 0; i < areaTrianglePointCount; i++) {
        Vector2d v = (Vector2d)triangulatedPoints.at(i);
        areaTrianglePoints[i] = Point(v.GetX(), v.GetY());
       // printf("x = %f, y = %f\n", v.GetX, v.GetY);
    }*/
    areaTrianglePointCount = 6;
    areaTrianglePoints = (Point*) malloc(sizeof(Point) * areaTrianglePointCount);
    textureCoordinates = (Point*) malloc(sizeof(Point) * areaTrianglePointCount);
    
    Vector2d v = points.at(0);
    areaTrianglePoints[0] = Point(v.GetX(), v.GetY());
    v = points.at(3);
    areaTrianglePoints[1] = Point(v.GetX(), v.GetY());
    v = points.at(2);
    areaTrianglePoints[2] = Point(v.GetX(), v.GetY());
    v = points.at(2);
    areaTrianglePoints[3] = Point(v.GetX(), v.GetY());
    v = points.at(1);
    areaTrianglePoints[4] = Point(v.GetX(), v.GetY());
    v = points.at(0);
    areaTrianglePoints[5] = Point(v.GetX(), v.GetY());

    
    calculateTextureCoordinates();
}
예제 #13
0
bool FruitCutNinjaScene::init()
{
    if (!Layer::init())
    {
        return false;
    }
    
    _visibleSize = Director::getInstance()->getVisibleSize();
    _deltaRemainder = 0.0f;
    _sliceTag = 1;
    
    //createBackground();
    //加入封闭的盒子,用作墙壁
    auto body = PhysicsBody::createEdgeBox(_visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3);
    auto wallNode = Node::create();
    wallNode->setPosition(_visibleSize.width/2, _visibleSize.height/2);
    wallNode->setPhysicsBody(body);
    this->addChild(wallNode);
    
    auto touchListener = EventListenerTouchAllAtOnce::create();
    touchListener->onTouchesBegan = CC_CALLBACK_2(FruitCutNinjaScene::onTouchesBegan, this);
    touchListener->onTouchesMoved = CC_CALLBACK_2(FruitCutNinjaScene::onTouchesMoved, this);
    touchListener->onTouchesEnded = CC_CALLBACK_2(FruitCutNinjaScene::onTouchesEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
    
    auto texture = Director::getInstance()->getTextureCache()->addImage("streak.png");
    
    for (int i = 0; i < 3; i++)
    {
        Blade* blade = Blade::createWithMaximumPoint(50);
        blade->setAutoDim(false);
        blade->setTexture(texture);
        
        addChild(blade, 100);
        _blades.pushBack(blade);
    }
    
    //PEShapeCache::getInstance()->addBodysWithFile("pineapple.plist");
    //auto body = PEShapeCache::getInstance()->getPhysicsBodyByName("pineapple");
    
    
    //Point points[4] = {Point(-128 + 128, -128 + 128), Point(-128 + 128, 128 + 128), Point(128 + 128, 128 + 128), Point(128 + 128, -128 + 128)};
    /*
    Point points[9] =
    {
        Point(157,50), Point(119, 13), Point(54, 0), Point(7,36), Point(5, 78), Point(23,118), Point(170,252), Point(235, 244), Point(237,158)
    };
    */
    Point points[4] =
    {
        Point(0, 0), Point(0, 512), Point(512, 512), Point(512, 0)
    };
    Vector2dVector vcPoints;
    vcPoints.clear();
    for (int i = 0; i < 4; i++)
    {
        vcPoints.push_back(Vector2d(points[i].x, points[i].y));
    }
    
    //texture = Director::getInstance()->getTextureCache()->addImage("boluo.png");
    PRFilledPolygon *filledPolygon = PRFilledPolygon::filledPolygonWithPointsAndTexture(vcPoints, "boluo.png");
    filledPolygon->setPhysicsBody(PhysicsBody::createPolygon(points, 4));
    filledPolygon->setPosition(Vec2(400, 700));
    filledPolygon->getPhysicsBody()->setTag(_sliceTag);
    filledPolygon->getPhysicsBody()->setGravityEnable(false);
    addChild(filledPolygon, 80);
    
    filledPolygon = PRFilledPolygon::filledPolygonWithPointsAndTexture(vcPoints, "xigua.png");
    filledPolygon->setPhysicsBody(PhysicsBody::createPolygon(points, 4));
    filledPolygon->setPosition(Vec2(1200, 700));
    filledPolygon->getPhysicsBody()->setTag(_sliceTag);
    filledPolygon->getPhysicsBody()->setGravityEnable(false);
    addChild(filledPolygon, 80);
    
    for (int i = 0; i < PARTICLE_COUNT; i++)
    {
        m_pEmitter[i] = ParticleSystemQuad::create("pineapple_splurt.plist");
        this->addChild(m_pEmitter[i], 100);
        m_pEmitter[i]->setAutoRemoveOnFinish(false);
        m_pEmitter[i]->setDuration(0.1f);
        m_pEmitter[i]->setScale(1.5f);
    }
    
    //本DEMO要做成一个切水果原型,尚未完成:)
    scheduleUpdate();
    return true;
}
예제 #14
0
void FruitCutNinjaScene::clipPoly(PhysicsShapePolygon* shape, Point normal, float distance)
{
    PhysicsBody* body = shape->getBody();
    int count = shape->getPointsCount();
    int pointsCount = 0;
    Point* points = new Point[count + 1];
    
    Vector2dVector vcPoints;
    vcPoints.clear();
    Vector2d v2Point(0, 0);
    
    for (int i=0, j=count-1; i<count; j=i, ++i)
    {
        Point a = body->local2World(shape->getPoint(j));
        float aDist = a.dot(normal) - distance;
        
        if (aDist < 0.0f)
        {
            points[pointsCount] = a;
            ++pointsCount;
        }
        
        Point b = body->local2World(shape->getPoint(i));
        float bDist = b.dot(normal) - distance;
        
        if (aDist*bDist < 0.0f)
        {
            float t = std::fabs(aDist)/(std::fabs(aDist) + std::fabs(bDist));
            Vec2 v2Tmp = a.lerp(b, t);
            points[pointsCount] = v2Tmp;
            ++pointsCount;
        }
    }
    
    Point center = PhysicsShape::getPolyonCenter(points, pointsCount);
    
    for (int i = 0; i < pointsCount; i++)
    {
        points[i] = body->world2Local(points[i]);
        vcPoints.push_back(Vector2d(points[i].x, points[i].y));
    }
    
    PhysicsBody* polyon = PhysicsBody::createPolygon(points, pointsCount);
    
    PRFilledPolygon* pNode = (PRFilledPolygon*)(body->getNode());
    std::string sName = pNode->getTextureName();
    //auto texture = Director::getInstance()->getTextureCache()->addImage("pineapple.png");
    PRFilledPolygon *filledPolygon = PRFilledPolygon::filledPolygonWithPointsAndTexture(vcPoints, sName.c_str());
    filledPolygon->setPhysicsBody(polyon);
    filledPolygon->setPosition(body->getPosition() + normal * -40);
    filledPolygon->getPhysicsBody()->setTag(_sliceTag);
    filledPolygon->getPhysicsBody()->setGravityEnable(false);
    
    polyon->setVelocity(body->getVelocityAtWorldPoint(center));
    polyon->setAngularVelocity(body->getAngularVelocity());
    polyon->setTag(_sliceTag);
    //polyon->applyImpulse(normal * -100);
    addChild(filledPolygon, 80);
    
    /*
    CPolygonSprite* pSprite = CPolygonSprite::create();
    pSprite->initWithFile("pineapple.png", polyon, false);
    pSprite->setPosition(body->getPosition());
    polyon->setTag(_sliceTag);
    addChild(pSprite);
    */
    
    /*
    Node* node = Node::create();
    PhysicsBody* polyon = PhysicsBody::createPolygon(points, pointsCount, PHYSICSBODY_MATERIAL_DEFAULT, -center);
    node->setPosition(center);
    node->setPhysicsBody(polyon);
    polyon->setVelocity(body->getVelocityAtWorldPoint(center));
    polyon->setAngularVelocity(body->getAngularVelocity());
    polyon->setTag(_sliceTag);
    addChild(node);
    */
    delete[] points;
}
예제 #15
0
void CCutScene::clipPoly(PhysicsShapePolygon* shape, Point normal, float distance)
{
    PhysicsBody* body = shape->getBody();
    int count = shape->getPointsCount();
    int pointsCount = 0;
    Point* points = new Point[count + 1];
    
    Vector2dVector vcPoints;
    vcPoints.clear();
    Vector2d v2Point(0, 0);
    
    for (int i=0, j=count-1; i<count; j=i, ++i)
    {
        Point a = body->local2World(shape->getPoint(j));
        float aDist = a.dot(normal) - distance;
        
        if (aDist < 0.0f)
        {
            points[pointsCount] = a;
            ++pointsCount;
        }
        
        Point b = body->local2World(shape->getPoint(i));
        float bDist = b.dot(normal) - distance;
        
        if (aDist*bDist < 0.0f)
        {
            float t = std::fabs(aDist)/(std::fabs(aDist) + std::fabs(bDist));
            Vec2 v2Tmp = a.lerp(b, t);
            points[pointsCount] = v2Tmp;
            ++pointsCount;
        }
    }
    
    Point center = PhysicsShape::getPolyonCenter(points, pointsCount);
    
    for (int i = 0; i < pointsCount; i++)
    {
        points[i] = body->world2Local(points[i]);
        vcPoints.push_back(Vector2d(points[i].x, points[i].y));
    }
    
    PhysicsBody* polyon = PhysicsBody::createPolygon(points, pointsCount);
    
    CFoodCut* pNode = (CFoodCut*)(body->getNode());
    std::vector<int> vMaterials;
    vMaterials.clear();
    vMaterials = pNode->getMaterials();
    MATERIAL_ID eId = MI_MAX;
    if (vMaterials.size() != 0)
    {
        eId = (MATERIAL_ID)vMaterials[0];
    }
    
    CFoodCut *filledPolygon = CFoodCut::create(eId, vcPoints, pNode->getPanziIndex(), pNode->getTouchedIndex());
    filledPolygon->setPhysicsBody(polyon);
    int nTmp = rand() % 50 + 50;
    int nTmpRotate = rand() % 30 - 60;
    filledPolygon->setPosition(body->getPosition());
    //filledPolygon->setRotation(filledPolygon->getRotation() + nTmpRotate);
    filledPolygon->getPhysicsBody()->setTag(_sliceTag);
    filledPolygon->getPhysicsBody()->setGravityEnable(false);
    
    
    polyon->setVelocity(body->getVelocityAtWorldPoint(center));
    //polyon->setAngularVelocity(body->getAngularVelocity());
    polyon->setTag(_sliceTag);
    
    float fMass = polyon->getMass();
    float fV = 80;
    float fImpulse = fMass * fV;
    float fTmpX = (float)(Random() % 30) / 100.0f - 0.15f;
    float fTmpY = (float)(rand() % 30) / 100.0f - 0.15f;
    polyon->applyImpulse((normal + Vec2(fTmpX, fTmpY)) * -fImpulse);
    polyon->setLinearDamping(0.8f);
    addChild(filledPolygon, 80);
    filledPolygon->setBirthTime(getCurTime());
    m_vCutFoods.push_back(filledPolygon);
    m_nSliceCount ++;
    delete[] points;
}
예제 #16
0
bool Triangulate::Process(const Vector2dVector &contour,Vector2dVector &result)
{
  /* allocate and initialize list of Vertices in polygon */

  int n = contour.size();
  if ( n < 3 ) return false;

  int *V = new int[n];

  /* we want a counter-clockwise polygon in V */

  if ( 0.0 < Area(contour) )
    for (int v=0; v<n; v++) V[v] = v;
  else
    for(int v=0; v<n; v++) V[v] = (n-1)-v;

  int nv = n;

  /*  remove nv-2 Vertices, creating 1 triangle every time */
  int count = 2*nv;   /* error detection */

  for(int m=0, v=nv-1; nv>2; )
  {
    /* if we loop, it is probably a non-simple polygon */
    if (0 >= (count--))
    {
      //** Triangulate: ERROR - probable bad polygon!
      return false;
    }

    /* three consecutive vertices in current polygon, <u,v,w> */
    int u = v  ; if (nv <= u) u = 0;     /* previous */
    v = u+1; if (nv <= v) v = 0;     /* new v    */
    int w = v+1; if (nv <= w) w = 0;     /* next     */

    if ( Snip(contour,u,v,w,nv,V) )
    {
      int a,b,c,s,t;

      /* true names of the vertices */
      a = V[u]; b = V[v]; c = V[w];

      /* output Triangle */
      result.push_back( contour[a] );
      result.push_back( contour[b] );
      result.push_back( contour[c] );

      m++;

      /* remove v from remaining polygon */
      for(s=v,t=v+1;t<nv;s++,t++) V[s] = V[t]; nv--;

      /* resest error detection counter */
      count = 2*nv;
    }
  }



  delete V;

  return true;
}
예제 #17
0
파일: Trampolim.cpp 프로젝트: brNX/H.E.A.D
Trampolim::Trampolim(float x, float y)
{
	moveangle=0.0f;
	realangle=0.0f;
	type = O_TRAMPOLIN;
	pX=x;
	pY=y;

	sm = ScreenManager::getInstance();

	

	/**********codigo Box2D***************/

	//define o tipo e a posição 
	bodydef.position.Set(x,y);
	bodydef.type = b2_kinematicBody;

	//cria o corpo usando as definições
	body=NULL;
	b2World * world = ((Level*)sm->getCurrentScreen())->getWorld();
	body = world->CreateBody(&bodydef);

	b2FixtureDef fd;
	fd.density = 1.0f;
	fd.friction = 0.3f;
	fd.restitution = 0.9f;

	//uso do triangulate.cpp -> pega num poligono e divide em triangulos
	Vector2dVector a;

	a.push_back(b2Vec2(-2.0f,	0.0f));	
	a.push_back(b2Vec2(0.0f,	0.0f));	
	a.push_back(b2Vec2(2.0f,	0.0f));	
	a.push_back(b2Vec2(2.0f,	2.0f));	
	a.push_back(b2Vec2(1.442f,	1.636f));
	a.push_back(b2Vec2(1.676f,	1.36f	));
	a.push_back(b2Vec2(0.0f,	1.251f	));
	a.push_back(b2Vec2(-0.67f,	1.36f	));
	a.push_back(b2Vec2(-1.442f,	1.636f));
	a.push_back(b2Vec2(-2.0f,	2.0f));	

	Vector2dVector result;

	//  Invoke the triangulator to triangulate this polygon.
	Triangulate::Process(a,result);



	// print out the results.
	int tcount = result.size()/3;

	for (int i=0; i<tcount; i++)
	{
		b2Vec2 vect[3];

		vect[0] = result[i*3+0];
		vect[1] = result[i*3+1];
		vect[2] = result[i*3+2];

		b2PolygonShape shape;

		shape.Set(vect,3);

		fd.shape = (b2Shape*)&shape;

		body->CreateFixture(&fd);

		printf("Triangle %d => (%f,%f) (%f,%f) (%f,%f)\n",i+1,vect[0].x,vect[0].y,vect[1].x,vect[1].y,vect[2].x,vect[2].y);
	}



	//usa o userdata para guardar um ponteiro no objecto body do Box2D (usado nas colisões)
	body->SetUserData(this);


	/**************************************/


	/***********codigo CLanlib***************/
	//criar a sprite
	sprite = sm->getSprite("trampolim");
	sprite->set_linear_filter(true);

}
예제 #18
0
bool Triangulate::Process(const Vector2dVector &contour,Vector2dVector &result)
{
#if defined(PRECISE_TRIANGULATION)
    std::vector<p2t::Point*> _polyline;
    for (auto _p : contour){
        _polyline.push_back(new p2t::Point(_p.GetX(), _p.GetY()));
    }
    p2t::CDT* _cdt = new p2t::CDT(_polyline);
    _cdt->Triangulate();
    std::vector<p2t::Triangle*> _triangles = _cdt->GetTriangles();
    for (auto _t : _triangles){
        auto _pt1 = _t->GetPoint(0);
        auto _pt2 = _t->GetPoint(1);
        auto _pt3 = _t->GetPoint(2);
        result.push_back(Vector2d(_pt1->x, _pt1->y));
        result.push_back(Vector2d(_pt2->x, _pt2->y));
        result.push_back(Vector2d(_pt3->x, _pt3->y));
    }
    delete _cdt;
    for (auto _p : _polyline)
        delete _p;
    return true;
#else
    /* allocate and initialize list of Vertices in polygon */
    
    int n = (int)contour.size();
    if ( n < 3 ) return false;
    
    int *V = new int[n];
    
    /* we want a counter-clockwise polygon in V */
    
    if ( 0.0f < Area(contour) )
        for (int v=0; v<n; v++) V[v] = v;
    else
        for(int v=0; v<n; v++) V[v] = (n-1)-v;
    
    int nv = n;
    
    /*  remove nv-2 Vertices, creating 1 triangle every time */
    int count = 2*nv;   /* error detection */
    
    for(int m=0, v=nv-1; nv>2; )
    {
        /* if we loop, it is probably a non-simple polygon */
        if (0 >= (count--))
        {
            //** Triangulate: ERROR - probable bad polygon!
            return false;
        }
        
        /* three consecutive vertices in current polygon, <u,v,w> */
        int u = v  ; if (nv <= u) u = 0;     /* previous */
        v = u+1; if (nv <= v) v = 0;     /* new v    */
        int w = v+1; if (nv <= w) w = 0;     /* next     */
        
        if ( Snip(contour,u,v,w,nv,V) )
        {
            int a,b,c,s,t;
            
            /* true names of the vertices */
            a = V[u]; b = V[v]; c = V[w];
            
            /* output Triangle */
            result.push_back( contour[a] );
            result.push_back( contour[b] );
            result.push_back( contour[c] );
            
            m++;
            
            /* remove v from remaining polygon */
            for(s=v,t=v+1;t<nv;s++,t++) V[s] = V[t]; nv--;
            
            /* resest error detection counter */
            count = 2*nv;
        }
    }
    
    //Danilo S Carvalho Alteration from delete to delete[]
    delete[] V;
    
    return true;
#endif
}