What I want is for the user to draw a polygon and for a sprite to be made in the shape of the polygon. I already have all the programming done for the user to create the polygon, so I can read the vertices of any image drawn. However, I seem to only be able to make sprites that are rectangular, using CGRectMake. Is there a way to create a sprite image from a file that is polygonal in shape. Like a CGPolyMake where I give it the vertices or something like that.
I don't know what shape the user will draw so I can't premake polygon sprites. Thanks for any help!
Cocos2D has no dedicated CCNode class for this purpose. You need to create a CCNode subclass and implement the draw-method yourself. That requires some knowledge about OpenGL ES.
You'll also have to triangulate the polygon so that you can fill it with a given texture.
You could use this C++ code for that: http://www.flipcode.com/archives/triangulate.cpp
This thread in the cocos2d forum is very helpful: http://www.cocos2d-iphone.org/forum/topic/8142
Related
I want to be able to draw any 2d shape dynamically in 2d world. My games has moving elements, looking like circle when alone, that can fuse and make complex shapes when getting close together. The elements can still move and thus separate or modify the shape.
How can I draw that with Unity? I don't think the usual sprites can do the trick.
I believe you can do that with shaders (sounds complex to me, but utilises GPU).
Or...
Modify sprite's vertices to introduce streching of the texture.
There's plenty of tutorials which explain how to add and manage image objects. But what if I want to add simple rectangle with plain white color? May I do it similar to adding 3D objects? I see no rectangle option under 2D Object in GameObject menu.
I know that I may do it by script, but isn't there any simpler solution? Or maybe I should use 3D Object Cube instead?
For a rectangle (ie a 2D cuboid) you add a 3D object that is a Quad which is a 3D object with a size of 0 in one plane, in effect making it a 2D sheet.
See also:
http://docs.unity3d.com/Manual/PrimitiveObjects.html
Quads are most often used as backdrops to 2D games. Once you have the quad you can then change its material properties to set it as a single plain colour.
This is just a general question, but is there any way to populate the same sprite in a pattern?
For example, if you drew a shape, a circle, is there anyway you can get the sprites to outline that circle, where they would be slightly spaced apart?
I feel it would be easier to do this than programmatically set the location of each sprite around the circle.
Thank you
What is the proper way of creating 2D Mesh with collider?
All I want to do is create some shapes (not very simple like triangle or rectangle) like slide or rounded rectangle. And I want to define a repeating pattern inside it to fill and reduce memory. Lastly I will need a collider around my shape.
Create the entire thing in a modeling program like Blender... same as you would for a 3D model, but all of your shapes/meshes will be flat. Create your textures the same also... all in your modeling program. Then save your file (.blend for Blender) to your Unity assets folder. Now switch back to Unity and drag/drop the prefab that is created form the .blend file on to your scene. Change the renderer if you want (I use Mobile Unlit, or Unlit Transparent most of the time).
Works just like a normal 2D sprite now... but the texture will repeat and you can make whatever shape you want in your modeling program.
For the collider, you can add any type of collider you want. If your shape isn't standard, you'll probably want an Edge Collider 2D or a Polygon Collider 2D.
I would suggest you the following:
Create a polygonCollider2D directly in unity as you need it
Add a mesh filter and a mesh renderer
Add a custom script made by yourself, in which you convert the Collider polygon to a mesh (Mesh that will be used on your MeshFilter at runtime).
3.1. In order to do this, just copy the vertices of the polygon
3.2. Use the triangulator class in this link to help you create your mesh (http://wiki.unity3d.com/index.php?title=Triangulator)
Enjoy
If you want to see the custom script find it here: http://answers.unity3d.com/questions/835675/how-to-fill-polygon-collider-with-a-solid-color.html
I would like to draw 2d shapes like this in an iPhone app:
alt text http://www.shaggyfrog.com/junk/beveled-circle.jpg
I asked a similar question here to see if I could do it easily with Quartz, but didn't get a solution. So I got to thinking that I might be able to leverage an exsiting 2d library out there, and then I thought of cocos2d.
The goal is to draw these kinds of beveled shapes dynamically, i.e., using arbitrary colours, and possibly with the highlight/bevel drawn at an arbitrary position.
Is this possible with cocos2d?
As far as my knowledge of cocos2d goes, cocos2d will not enable you to do this in any other way than OpenGL would allow you to do. Cocos2d uses OpenGL under the hood. Cocos2d comes with no built-in set for creating such graphics.
Since the bevel is used to create a 3D effect, perhaps you shouldn't be looking at simulating it with 2D drawing, but instead use a 3D drawing library? OpenGL would certainly be capable of drawing such shapes. Cocos2d focuses on 2D drawing instead of 3D.
I'm not sure if Cocos2D would allow for a custom object to draw 3D using the underlying OpenGL mechanism. I have never tried.
Wouldn't it instead be easier to create the image in photoshop and adjust colors dynamicly? I'm not sure what you are trying to do.
You could also create a mask shape with a transparent "bevel effect" and scale that along with the image you need to have shine?
Aside from the bevel effect, if you want to "colorize" each semi-circle, you can use [sprite setColor:] or sprite.color = ccc3(r,g,b)
CCSprite *sprite = [CCSprite spriteWithSpriteSheet:sheet rect:CGRectMake(32 * idx,0,128,32)];
[sprite setColor:ccc3(CCRANDOM_0_1()*255,CCRANDOM_0_1()*255,CCRANDOM_0_1()*255)];
You would design a "white semicircle" with beveled (gray) edges. Then you can make sprites and color each separately.