# Letting the user draw a Polygon Body and Image

In my game, I want to provide the user with a drawing feature.

By free hand drawing, the user creates a polygon shape. Then, in my game implementation, I have to create a body for the found vertices and generate an image based on that polygon shape.

My problem is how to create an image that matches the user-provided vertices. I've heard that cocos2d has something called Image Masking. I don't understand how I could implement it in AndEngine. Could someone help?

• This is a 2d image I assume. What are you trying now? – joncodo Dec 19 '12 at 2:06
• Do you just draw your own character? Or is it more like drawing a map or something where the vertices have something to do with actual gameplay (other than defining what's visible)? If it's just visual, you could just put the user's drawing on a quad and move/transform that rather than creating actual vertices to bend. – Mario Dec 19 '12 at 10:39
• I have only one starting point to work is describe in the following link emanueleferonato.com/2011/08/05/…. So any other suggestion provide help to me. – Siddharth Dec 19 '12 at 18:58
• andengine.org/forums/tutorials/… – ClassicThunder Sep 23 '14 at 17:04

This is a really interesting question.

I think the way you could do this, using strictly box2d, would be store all the points in some kind of array, and then pop them into the polygon function.

I've never used a poly myself, but judging from this:

https://stackoverflow.com/questions/4360084/creating-complex-shapes-with-box2d

You may be able to use the function createShape()

What I do remember, is there is definitely a way to read vectors into a fixture, the most logical way for that to work would be in some kind of stack.

If you've already grabbed the points based on user input, you simply need to feed them into the box2d poly function, create a fixture/body or whatnot and then add it to your world.

As far as the image, I'm pretty sure you can do this using opengl, but I'm not sure what exactly you mean. If you just want them to be able to draw an amoeba shape and then fill it with a solid color, that should be easy.

If however you want to take an existing image and skew it to fit into the fixture shape, that will be hard. I'm sure you can do it though, here's an example of what blitting looks like using opengl (note, this is in Java)

                glPushMatrix();
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
dirPosd = i.shield.getPosition().mul(30);
glTranslatef(dirPosd.x, dirPosd.y, 0);
glRotated(Math.toDegrees(i.shield.getAngle()+52.9), 0, 0, 1);
glColor3f(1,1,1);
animationY.bind();
sizer = 40;
glTexCoord2f(0f, 0f);

glVertex2f( i.shield.getPosition().x - sizer-4, i.shield.getPosition().y - sizer-2);     //NW
glTexCoord2f(1, 0);
glVertex2f( i.shield.getPosition().x + sizer-4, i.shield.getPosition().y - sizer-2);   //NE
glTexCoord2f(1, 1);
glVertex2f( i.shield.getPosition().x + sizer-4, i.shield.getPosition().y + sizer-2); //SE
glTexCoord2f(0, 1);
glVertex2f( i.shield.getPosition().x - sizer-4, i.shield.getPosition().y + sizer-2);   //SW
glEnd();
glPopMatrix();


Your solution will be somewhere between the glBegin() and glEnd(), and will involve manipulating the glTexCoord2f() and glVertex2f(). I've played around with these a bit and achieved bizarre skews, and I'm sure you can use that to achieve what you trying to do.

The fact is, I have no clue how to actually do it, you'll probably have to take an in depth look into opengl, focusing on some of those functions I mentioned.

EDIT>>>

Note: Yes, that code I posted is obviously a mess, but the purpose is to provide a rough example (but it does work). For the functionality you require, you will need a competent understanding of the opengl library.

Why don't you simply let the user draw directly onto an image? In that case, there wouldn't be any need for a conversion. You could then put that image onto a quad and render it like that in your game.

• Well, thx for the unexplained downvote. – Tara Oct 13 '14 at 16:21