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?

  • \$\begingroup\$ This is a 2d image I assume. What are you trying now? \$\endgroup\$ – joncodo Dec 19 '12 at 2:06
  • \$\begingroup\$ 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. \$\endgroup\$ – Mario Dec 19 '12 at 10:39
  • \$\begingroup\$ 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. \$\endgroup\$ – Siddharth Dec 19 '12 at 18:58
  • \$\begingroup\$ andengine.org/forums/tutorials/… \$\endgroup\$ – 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:


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)

                dirPosd = i.shield.getPosition().mul(30);
                glTranslatef(dirPosd.x, dirPosd.y, 0);
                glRotated(Math.toDegrees(i.shield.getAngle()+52.9), 0, 0, 1);
                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

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.


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.

| improve this answer | |

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.

| improve this answer | |
  • \$\begingroup\$ Well, thx for the unexplained downvote. \$\endgroup\$ – Tara Oct 13 '14 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.