Issue with a point coordinates, which creates an unwanted triangle

Posted by Paul on Game Development See other posts from Game Development or by Paul
Published on 2012-07-05T12:31:10Z Indexed on 2012/07/05 15:24 UTC
Read the original article Hit count: 286

I would like to connect the points from the red path, to the y-axis in blue. I figured out that the problem with my triangles came from the first point (V0) : it is not located where it should be.

enter image description here

In the console, it says its location is at 0,0, but in the emulator, it is not.

The code :

for(int i = 1; i < 2; i++) {   
        CCLOG(@"_polyVertices[i-1].x : %f, _polyVertices[i-1].y : %f", _polyVertices[i-1].x, _polyVertices[i-1].y);
        CCLOG(@"_polyVertices[i].x : %f, _polyVertices[i].y : %f", _polyVertices[i].x, _polyVertices[i].y);
        ccDrawLine(_polyVertices[i-1], _polyVertices[i]);
    } 

The output :

_polyVertices[i-1].x : 0.000000, _polyVertices[i-1].y : 0.000000
_polyVertices[i].x : 50.000000, _polyVertices[i].y : 0.000000

And the result : (the layer goes up, i could not take the screenshot before the layer started to go up, but the first red point starts at y=0) :

enter image description here

Then it creates an unwanted triangle when the code continues :

enter image description here

Would you have any idea about this? (So to force the first blue point to start at 0,0, and not at 50,0 as it seems to be now)

Here is the code :

- (void)generatePath{
    float x = 50; //first red point
    float y = 0;
    for(int i = 0; i < kMaxKeyPoints+1; i++) {
        if (i<3){
            _hillKeyPoints[i] = CGPointMake(x, y);
            x = 150 + (random() % (int) 30);
            y += -40;
        } else if(i<20){ //going right
            _hillKeyPoints[i] = CGPointMake(x, y);
            x += (random() % (int) 30);
            y += -40;
        } else if(i<25){ //stabilize
            _hillKeyPoints[i] = CGPointMake(x, y);
            x = 150 + (random() % (int) 30);
            y += -40;
        } else if(i<30){ //going left
            _hillKeyPoints[i] = CGPointMake(x, y);
            //x -= (random() % (int) 10);
            x = 150 + (random() % (int) 30);
            y += -40;
        } else { //back to normal
            _hillKeyPoints[i] = CGPointMake(x, y);
            x = 150 + (random() % (int) 30);
            y += -40;
        }
    }
}

-(void)generatePolygons{
    static int prevFromKeyPointI = -1;
    static int prevToKeyPointI = -1;

    // key points interval for drawing
    while (_hillKeyPoints[_fromKeyPointI].y > -_offsetY+winSizeTop) {
        _fromKeyPointI++;
    }
    while (_hillKeyPoints[_toKeyPointI].y > -_offsetY-winSizeBottom) {
        _toKeyPointI++;
    }

    if (prevFromKeyPointI != _fromKeyPointI || prevToKeyPointI != _toKeyPointI) {

        _nPolyVertices = 0;
        float x1 = 0;
        int keyPoints = _fromKeyPointI;

    for (int i=_fromKeyPointI; i<_toKeyPointI; i++){

        //V0: at (0,0)
        _polyVertices[_nPolyVertices] = CGPointMake(x1, y1); //first blue point
        _polyTexCoords[_nPolyVertices++] = CGPointMake(x1, y1);

        //V1: to the first "point"
        _polyVertices[_nPolyVertices] = CGPointMake(_hillKeyPoints[keyPoints].x, _hillKeyPoints[keyPoints].y);
        _polyTexCoords[_nPolyVertices++] = CGPointMake(_hillKeyPoints[keyPoints].x, _hillKeyPoints[keyPoints].y);

        keyPoints++; //from point at index 0 to 1
        //V2, same y as point n°2:
        _polyVertices[_nPolyVertices] = CGPointMake(0, _hillKeyPoints[keyPoints].y);  
        _polyTexCoords[_nPolyVertices++] = CGPointMake(0, _hillKeyPoints[keyPoints].y);


        //V1 again
        _polyVertices[_nPolyVertices] = _polyVertices[_nPolyVertices-2];
        _polyTexCoords[_nPolyVertices++] = _polyVertices[_nPolyVertices-2];

        //V2 again
        _polyVertices[_nPolyVertices] = _polyVertices[_nPolyVertices-2];
        _polyTexCoords[_nPolyVertices++] = _polyVertices[_nPolyVertices-2];
        //CCLOG(@"_nPolyVertices V2 again : %i", _nPolyVertices);

        //V3 = same x,y as point at index 1
        _polyVertices[_nPolyVertices] = CGPointMake(_hillKeyPoints[keyPoints].x, _hillKeyPoints[keyPoints].y);
        _polyTexCoords[_nPolyVertices] = CGPointMake(_hillKeyPoints[keyPoints].x, _hillKeyPoints[keyPoints].y);


        y1 = _polyVertices[_nPolyVertices].y;
        _nPolyVertices++;
    }

        prevFromKeyPointI = _fromKeyPointI;
        prevToKeyPointI = _toKeyPointI; 
    }
}

- (void) draw {
    //RED
    glColor4f(1, 1, 1, 1);
    for(int i = MAX(_fromKeyPointI, 1); i <= _toKeyPointI; ++i) {
        glColor4f(1.0, 0, 0, 1.0); 
        ccDrawLine(_hillKeyPoints[i-1], _hillKeyPoints[i]);        
    }

    //BLUE
    glColor4f(0, 0, 1, 1);
    for(int i = 1; i < 2; i++) {   
        CCLOG(@"_polyVertices[i-1].x : %f, _polyVertices[i-1].y : %f", _polyVertices[i-1].x, _polyVertices[i-1].y);
        CCLOG(@"_polyVertices[i].x : %f, _polyVertices[i].y : %f", _polyVertices[i].x, _polyVertices[i].y);
        ccDrawLine(_polyVertices[i-1], _polyVertices[i]);
    } 

}

Thanks

© Game Development or respective owner

Related posts about iphone

Related posts about opengl-es