Search code examples
objective-cxcodecocos2d-iphoneopengl-es-2.0

Cocos2d / CCDrawNode - How to draw a line?


I see there is functionality to draw circles, polys, dot and segments. I dont see one for drawing an A-B line (with given thickness), like ccDrawLine() (which seems to be deprecated).

I need to draw a 'network' between connected nodes. I have the code to draw the network, however ccDrawLine doesn't seem to support aliasing or opacity, like CCDrawNode. It also, without manual intervention, doesn't seem to support batching.

Any suggetions? Would I need to do a load of maths to draw a 2 tri-poly rectangle at the right angle between points?

UPDATE: Based on comments below... I have an idea on how to do a 'Line' 0,0 to 10,0 with thickness 2, I'd have to do a rect at {0,0.5}, {10,0.5}, {10,-0.5}, {0,-0.5}... I can work out the clockwise triangle points to make a polygon from that easily. I, therefore, could even do horiz/vert ones easily. But how do you do that between {4,5}, {10,7}? Would you do a normal rectangle and apply a transformation matrix to it? Or would you still precalculate each 4 points and then make 2 triangles from it?

UPDATE: Maybe it'd be better to use a scaled "line" sprite?! Eg: https://stackoverflow.com/a/8760462/224707

UPDATE: How about a Ribbon? Would that work? Eg: https://stackoverflow.com/a/8178729/224707

Not sure a Ribbon would work for a "network" of points though...

CLARIFICATION: Imagine this image, but with straight lines and no intersections... Something like this:
(source: relenet.com)

UPDATE: Apparantly, my post to the Forum did go though last night just before it went down... http://www.cocos2d-iphone.org/forum/topic/224498


Solution

  • A line is a segment. You can take it from here... ;)

    Update:

    CCDrawNode can draw segments. Segments are lines with defined start and end points.