xcodecocos2d-iphoneantialiasing

Jagged Edges in Cocos2D - Antialiasing Issue?


We're currently developing a game involving graphs (nodes connected by edges), and we want to be able to move the nodes around. We have the nodes moving (responding to touch), but when the edges (initialized with .png images) are re-rendered as a result of the nodes moving, they look jagged. We've tried playing around with Aliasing and Anti-Aliasing, but the edges are jagged regardless of what we do.

Here's where we initialize the edge images (with image file):

-(id) initWithSet:(int)setNum Level:(int) levelNum
{
    if( (self=[super init]) ) {

           <code omitted>

           for (NSArray *e in self.level.targetGraph) {

               <code omitted>

               CCSprite *edgeOutline = [CCSprite spriteWithFile:@"EdgeOutline.png"];
               [edgeOutline setPosition:ccp((ax+bx)/2, (ay+by)/2)];
               edgeOutline.scaleX = (dist * edgeOutline.scaleX)/edgeOutline.contentSize.width;
               edgeOutline.scaleY = (self.nodeSize * 0.5 * edgeOutline.scaleY)/edgeOutline.contentSize.height;
               edgeOutline.rotation = -angle;
               [outlines addChild:edgeOutline];

               CCSprite *edgeFill = [CCSprite spriteWithFile:@"EdgeFill.png"];
               [edgeFill setPosition:ccp((ax+bx)/2, (ay+by)/2)];
               edgeFill.scaleX = (dist * edgeFill.scaleX)/edgeFill.contentSize.width;
               edgeFill.scaleY = (self.nodeSize * 0.4 * edgeFill.scaleY)/edgeFill.contentSize.height;
               edgeFill.rotation = -angle;
               [fills addChild:edgeFill];

         }

  return self;
}

And here's where we update the edge drawings (using the second half of the nextFrame method):

- (void) nextFrame:(ccTime)dt {

    for (NSArray *e in self.level.graph) {
        CCSprite *a = [self.graphNodeOutlines objectForKey:[e objectAtIndex:0]];
        double ax = a.position.x;
        double ay = a.position.y;
        CCSprite *b = [self.graphNodeOutlines objectForKey:[e objectAtIndex:1]];
        double bx = b.position.x;
        double by = b.position.y;

        double dx = ax-bx;
        double dy = ay-by;
        double angle = 90;
        if (dx != 0) {
            angle = atan2(ay-by, ax-bx)*180/M_PI;
        }
        double dist = sqrt(dx*dx+dy*dy);

        //TODO: get anti-aliasing to work in this next part!

        CCSprite *edgeOutline = [self.graphEdgeOutlines objectForKey:e];
        [edgeOutline setPosition:ccp((ax+bx)/2, (ay+by)/2)];
        edgeOutline.scaleX = (dist)/edgeOutline.contentSize.width;
        edgeOutline.scaleY = (self.nodeSize * 0.25)/edgeOutline.contentSize.height;
        edgeOutline.rotation = -angle;

        //The line we hoped would fix the jagged edge problem.
        [[edgeOutline texture] setAntiAliasTexParameters];

        CCSprite *edgeFill = [self.graphEdgeFills objectForKey:e];
        [edgeFill setPosition:ccp((ax+bx)/2, (ay+by)/2)];
        edgeFill.scaleX = (dist)/edgeFill.contentSize.width;
        edgeFill.scaleY = (self.nodeSize * 0.2)/edgeFill.contentSize.height;
        edgeFill.rotation = -angle;

        //The (same) line we hoped would fix the jagged edge problem.
        [[edgeFill texture] setAntiAliasTexParameters];

    }
}

We're using Cocos2D v2.0 and XCode v4.5.

Any help would be greatly appreciated. Thank you!

UPDATE: A picture showing the issue:

enter image description here


Solution

  • I have a solution! Follow the "Adding a MainWindow.xib" section of this tutorial: http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit

    Somehow, handing some of the graphics responsibility over to UIKit solves the problem. Ray is my savior.

    EDIT: This only seems to fix the problem on the ipad. No idea why.

    EDIT 2: Ignore all that; all antialiasing had something to do the simulator scaling. Instead, help me figure out what this post means: http://www.cocos2d-iphone.org/forum/topic/4773