Ejemplo n.º 1
0
bool RigSegmentCollides(const Rig& targetRig, const CCPoint& newVertex, const CCPoint& oldVertex)
{
    if(targetRig.size() < 3)
    {
        return false;
    }
    
    for(int v = 0; v < targetRig.size(); v++)
    {
        CCPoint edgeStart = targetRig[v];
        CCPoint edgeEnd = targetRig[ (v+1) % targetRig.size() ];
        
        if(edgeStart.equals(oldVertex) || edgeEnd.equals(oldVertex))
        {
            continue;
        }
        
        if(ccpSegmentIntersect(edgeStart, edgeEnd, newVertex, oldVertex))
        {
            return true;
        }
    }
    
    return false;
}
void ItemFireString::collision()
{
	if (_mario->_dead) return;
	// »ñÈ¡fireStringÏ߶Î
	struct Line
	{
		CCPoint pt1;
		CCPoint pt2;
	} lineFireString;

	CCRect rcItem = boundingBox();
	int angle = getRotation();
	angle %= 360;
	if (angle > 270)
	{
		lineFireString.pt1 = ccp(rcItem.getMinX(), rcItem.getMinY());
		lineFireString.pt2 = ccp(rcItem.getMaxX(), rcItem.getMaxY());
	}
	else if (angle > 180)
	{
		lineFireString.pt1 = ccp(rcItem.getMinX(), rcItem.getMaxY());
		lineFireString.pt2 = ccp(rcItem.getMaxX(), rcItem.getMinY());
	}
	else if (angle > 90)
	{
		lineFireString.pt1 = ccp(rcItem.getMinX(), rcItem.getMinY());
		lineFireString.pt2 = ccp(rcItem.getMaxX(), rcItem.getMaxY());
	}
	else
	{
		lineFireString.pt1 = ccp(rcItem.getMinX(), rcItem.getMaxY());
		lineFireString.pt2 = ccp(rcItem.getMaxX(), rcItem.getMinY());
	}


	// MarioÏ߶Î
	Line lineMario[2];
	CCRect rcMario = _mario->boundingBox();
	lineMario[0].pt1 = ccp(rcMario.getMinX(), rcMario.getMinY());
	lineMario[0].pt2 = ccp(rcMario.getMaxX(), rcMario.getMaxY());
	lineMario[1].pt1 = ccp(rcMario.getMaxX(), rcMario.getMinY());
	lineMario[1].pt2 = ccp(rcMario.getMinX(), rcMario.getMaxY());

	for (int i = 0; i < 2; ++i)
	{
		if (ccpSegmentIntersect(lineMario[i].pt1, lineMario[i].pt2, lineFireString.pt1, lineFireString.pt2))
			_mario->Die(false);
	}

}
Ejemplo n.º 3
0
void RigRandom(Rig& targetRig, int vertexes)
{
    //Other implementations can be found at:
    //http://pastebin.com/wdKmSrL3
    
    //Let's create the goal polygon first.
    //Due to preventing intersections, one more vertex maybe created that stated over here.
    unsigned int vertexNum = vertexes;
    
    //Now that we have the number of vertexes, let's create the number of angles needed.
    std::vector<float> angles;
    angles.reserve(vertexNum);
    for(int angleIter = 0; angleIter < vertexNum; angleIter++)
    {
        //angles.push_back(angleIter * (angleSize + CCRANDOM_0_1()));
        angles.push_back(GetRandomAngle());
    }
    
    std::sort(angles.begin(), angles.end());
    
    //Iterate over the angles, create a vertexes stack.
    targetRig.reserve(vertexNum);
    for(std::vector<float>::iterator iter = angles.begin(); iter != angles.end(); iter++)
    {
        float angle = (*iter);
        CCPoint highPoint = ccp(0, RIG_MIN_RADIUS() + (RIG_MAX_RADIUS()-RIG_MIN_RADIUS()) * CCRANDOM_0_1());
        targetRig.push_back(ccpRotateByAngle(highPoint, CCPointZero, angle));
    }
    
    //Make sure the first and last point doesn't intersect.
    for(int i = 0; i < targetRig.size()-1; i++) {
        CCPoint startPoint = targetRig[i];
        CCPoint endPoint = targetRig[i+1];
        if(ccpSegmentIntersect(startPoint, endPoint, targetRig.front(), targetRig.back())) {
            targetRig.push_back(CCPointZero);
            break;
        }
    }
}
Ejemplo n.º 4
0
bool RigSplit(const CCPoint& startPoint, const CCPoint& endPoint, const Rig& originalRig, std::vector<Rig>& finalRigs)
{
    std::vector<std::pair<Rig, CCPoint> > splitPolygons;
    std::pair<Rig, CCPoint>* currentRig = NULL;

    Rig intersections;
    Rig ignoreIntersections;
    CCPoint lastVert = originalRig[originalRig.size()-1];
    for(int i = 0; i < originalRig.size(); i++)
    {
        CCPoint intersectPoint = ccpIntersectPoint(startPoint, endPoint, lastVert, originalRig[i]);
        
        if( ccpSegmentIntersect(startPoint, endPoint, lastVert, originalRig[i]) )
        {
            intersections.push_back(intersectPoint);
        }
        
        lastVert = originalRig[i];
    }
    
    std::sort(intersections.begin(), intersections.end(), PositionSort(startPoint));
    
    //Check if the split line is valid.
    if(intersections.size() < 2)
    {
        return false;
    }
    
    if(RigCheckPoint(originalRig, startPoint))
    {
        ignoreIntersections.push_back(intersections[0]);
        intersections.erase(intersections.begin());
    }
    
    if(intersections.size() % 2 != 0)
    {
        ignoreIntersections.push_back(intersections[intersections.size()-1]);
        intersections.pop_back();
    }
    
    std::vector<std::pair<CCPoint, CCPoint> > intersectionPairs;
    intersectionPairs.reserve(intersections.size() / 2);
    for(int i = 0; i < intersections.size(); i += 2)
    {
        intersectionPairs.push_back(std::make_pair(intersections[i], intersections[i+1]));
    }
    
    splitPolygons.push_back(std::make_pair(Rig(), CCPoint()));
    currentRig = &splitPolygons[0];
    
    for(int y = 0; y < originalRig.size(); y++)
    {
        CCPoint edgeStart = originalRig[y];
        CCPoint edgeEnd = originalRig[ (y+1) % originalRig.size()];
        
        if(!RigContain(currentRig->first, edgeStart))
        {
            currentRig->first.push_back(edgeStart);
        }
        
        //Check for intersections with the split line:
        bool intersects = ccpSegmentIntersect(startPoint, endPoint, edgeStart, edgeEnd);
        
        if(intersects)
        {
            CCPoint intersectionPoint = ccpIntersectPoint(startPoint, endPoint, edgeStart, edgeEnd);
         
            //These points should be ignored.
            if(RigContain(ignoreIntersections, intersectionPoint))
            {
                continue;
            }
            
            //Add the intersection point to the current polygon.
            if(!RigContain(currentRig->first, intersectionPoint))
            {
                currentRig->first.push_back(intersectionPoint);
            }
            
            CCPoint pairs;
            //Find the intersction point in the intersection pairs.
            for(int f = 0; f < intersectionPairs.size(); f ++)
            {
                if(intersectionPairs[f].first.equals(intersectionPoint))
                {
                    pairs = intersectionPairs[f].second;
                    break;
                }
                
                if(intersectionPairs[f].second.equals(intersectionPoint))
                {
                    pairs = intersectionPairs[f].first;
                    break;
                }
            }
            
            assert(!pairs.equals(CCPointZero));
            
            if(!RigContain(currentRig->first, pairs))
            {
                currentRig->first.push_back(pairs);
            }
            
            //Set its partner as the crossback point for the current polygon.
            currentRig->second = pairs;
            
            
            bool found = false;
            for(int x = 0; x < splitPolygons.size(); x ++)
            {
                //If there is an existing polygon with a crossback for this edge
                if(&splitPolygons[x] != currentRig && (splitPolygons[x].second.equals(pairs) || splitPolygons[x].second.equals(intersectionPoint)) )
                {
                    //Set the current polygon to be that polygon
                    currentRig = &splitPolygons[x];
                    found = true;
                    break;
                }
            }
            
            //Else
            if(!found)
            {
                //Append a new polygon and new crossback point to the output lists and make it current
                splitPolygons.push_back(std::make_pair(Rig(), CCPoint()));
                currentRig = &splitPolygons[splitPolygons.size()-1];
            }
            
            if(!RigContain(currentRig->first, edgeEnd))
            {
                currentRig->first.push_back(edgeEnd);
            }
        }
    }
    
    for(int i = 0; i < splitPolygons.size(); i++)
    {
        Rig rig = splitPolygons[i].first;
        
        finalRigs.push_back(rig);
    }
    
    return true;
}