CCDrawNode.DrawPolygon border overdraw bug(?)

I've found something strange which looks like a bug to me with the way that CCDrawNode.DrawPolygon works. I've simplified the problem down to drawing a triangle within an enclosing rectangle (I've had similar problems when using complex polygon shapes).

The problem is that a very narrow triangle has it's end drawn way outside the rectangle even though all points of the triangle are easily within the rectangle.

Here is the code that shows the problem:

CCLayer layer = new CCLayer();
CCDrawNode node = new CCDrawNode();
List<CCPoint> points = new List<CCPoint>();

float left = 50.0f;
float right = 600.0f;
float bottom = 200.0f;
float top = 900.0f;
float inset = 50.0f;
float triWidth = 85.0f;

node.DrawRect(new CCRect(left, bottom, right - left, top - bottom), CCColor4B.Transparent, 5.0f, CCColor4B.Red);

points.Add(new CCPoint(left + inset, top - inset));
points.Add(new CCPoint(right - inset, bottom + inset));
points.Add(new CCPoint(left + inset, top - triWidth - inset));

node.DrawPolygon(points.ToArray(), points.Count, CCColor4B.Gray, 5.0f, CCColor4B.White);


I've attached three screenshots where the only difference is the variable triWidth, at a value of 20 the end is way offscreen, at a value of 85 it's outside the rectangle but onscreen and a value of 200 does what I'd expected and is completely within the rectangle.

The triangle apex point "new CCPoint(right - inset, bottom + inset)" is the same in all examples towards the bottom right of the rectangle. It is the base width of the triangle at the upper left of the rectangle that is being changed.

Is this a bug or is this down to some sort of border join problem as if I change the border from 5.0f to 0.0f then the triangle is within the rectangle even with a triWidth set at 20.0f which with the border goes way offscreen.

Thanks, Adrian


  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai


    Not sure I can actually say it is a bug per-se as it is the drawing routine for trying to join the lines. It is just that we do not have the concept of line joins and it tries to work out a nice join itself (although I do see your point and what it does is not nice looking in your case). Basically the way Cocos2d-x does. Remember we do not deliver a full 2D drawing stack but with usage feedback we are getting there step by step :-)

    We would have to open an issue for this and add joins and endings to the drawing routines.

    I have set up that issue here.

    This will not help you right now but it is on the growing list :-)

  • No problem - I found this while trying to debug my polygon filling as I wanted to see what the outline was and it looked very different in some cases from the filled in version as the outline version used a border with no fill!

    So not a problem right now for me and nice to know it's being tracked for future consideration.

    I do think that having a full drawing stack would be nice - but in no way essential - especially if later on CocosSharp integrates with Xamarin.Forms so we can create really snazzy cross-platform custom controls using CocosSharp.

    I'd ideally like to do some really nice vector type graphics in Xamarin.Forms using CocosSharp and using a full drawing stack in CocosSharp would be fantastic given it's hierarchical nodes.

    Thanks, Adrian

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai


    That sounds great and thanks for the feedback.

    I know the forum has quite a few posts of Xamarin.Forms and with it being looked at but please be sure it really is being looked at.

Sign In or Register to comment.