smooth terrain

General discussion about the R.U.B.E editor
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: smooth terrain

Post by vkreal »

Awesome! Thanks for the quick reply. I did purchased RUBE it's a pretty nice tool!
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: smooth terrain

Post by vkreal »

Here's another question. say i wanted to create dynamic textures like in http://www.raywenderlich.com/33266/how- ... ocos2d-2-x
and apply it to a terrain create by RUBE using Edge Shapes. Given RUBE scene been successfully loaded into cocos2d-x.

ANy suggestions on how i can apply texture to terrain?

I was thinking get vertices from edge shapes (not sure best way to get them) and do something like below

glColor4f(1, 1, 1, 1);
glVertexPointer(2, GL_FLOAT, 0, terrainVertices);
glTexCoordPointer(2, GL_FLOAT, 0, terrainTexCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nTerrainVertices);

I might be going down the wrong direction though


Much thanks!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: smooth terrain

Post by iforce2d »

You can iterate over the vertices of a fixture after loading, like this:

Code: Select all

b2Shape::Type shapeType = fixture->GetType();

if ( shapeType == b2Shape::e_polygon )
{
    b2PolygonShape* polygonShape = (b2PolygonShape*)fixture->GetShape();

    int numVertices = polygonShape->GetVertexCount();
    for (int i = 0; i < numVertices; i++) {
        b2Vec2 vertex = polygonShape->GetVertex( i );
        ... do something with the vertex ...
    }
}
else if ( shapeType == b2Shape::e_chain )
{
    b2ChainShape* chainShape = (b2ChainShape*)fixture->GetShape();

    int numEdges = chainShape->GetChildCount();
    int numVertices = chainShape->m_hasNextVertex ? numEdges : numEdges + 1;

    b2EdgeShape edgeShape;
    for (int i = 0; i < numEdges; i++) {
        chainShape->GetChildEdge( &edgeShape, i );
        b2Vec2 vertex = edgeShape.m_vertex1;
        ... do something with the vertex ...
    }

    if ( !chainShape->m_hasNextVertex ) {
        b2Vec2 vertex = edgeShape.m_vertex2;
        ... do something with the vertex ...
    }
}
As you can see, unfortunately chain shapes are a bit more complicated because they only give you access to the child edges, not the vertices directly.

Using that code, you could create an array of length 'numVertices', and then each place that says 'do something with the vertex' you could put the vertex into that array. Then you would have an array of vertices to use as shown in the tutorial on Ray's site.

One thing to keep in mind though, is that the technique on Ray's site assumes that the x-coord of each successive point is always increasing, so there can be no concave sections, and it's easy to draw with a triangle strip. For concave sections you could use poly2tri or some other polygon decomposition library to create the triangles to draw.

Another problem you may face is that the order of the vertices you get like this might not be starting from left and going toward the right. Especially for a loop shape, the first vertex could be anywhere. It's probably best to use a 'line' type (fixture type selection in RUBE) for this fixture, so you can clearly see where the ends are.
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: smooth terrain

Post by vkreal »

Man i didn't account for concave, this throw a monkey wrench. I guess i can try without concave and see if i can get it working correctly and than try out poly2tri. anything special i should pay attention to with TexCoord?

Thanks!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: smooth terrain

Post by iforce2d »

I did not really look at all the details of the tutorial on Ray's site, but I was assuming that it shows you how to set texcoords to render the generated texture. It's not something that RUBE can help with, because the texture is not part of the saved scene, it's generated by your program right?
btw just in case you misssed it there are two parts to that tutorial, iirc the second part shows how to use the generated texture.
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: smooth terrain

Post by vkreal »

Hi iforce2d

Yes I saw the second part of tutorial on Ray's site, thanks for pointing that out. I am trying to learn making terrain with your tool and applying dynamic texture to it. What games doesn't need some kind of terrain?? :)

Can you please explain what you mean by "It's probably best to use a 'line' type (fixture type selection in RUBE) for this fixture, so you can clearly see where the ends are." ?? I am trying to understand what you mean.

I wanted to thank you again for your very generous help!
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: smooth terrain

Post by iforce2d »

Sorry, I should have said 'shape type'. From the help, under Editing items -> Editing fixtures:
lineloop.png
lineloop.png (28.6 KiB) Viewed 16152 times
As you can see, it's much easier to find the first/last vertex of a line shape, which I guess will be important for this.
ifMike
Posts: 6
Joined: Mon Sep 02, 2013 9:53 am

Re: smooth terrain

Post by ifMike »

Did you fix this vkreal?

I'm able to loop through my vertices and get the total number.
Did you manage to combine Ray Wenderlich tutorial with the vertices from RUBE and apply textures to it?

This project pretty much do what I want: http://www.justcode.it/cocos2d/EndlessTerrain.zip
(from this forum thread: http://www.cocos2d-iphone.org/forums/to ... formances/)

But I want my line to be made of "wood" (png file) and have stone texture below - but WITH RUBE :)
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: smooth terrain

Post by iforce2d »

iirc Ray's tutorial explains about applying textures under a sine curve. The only difference with a RUBE scene is that you get the vertices from the loaded scene instead of calculating a curve. As for how to actually draw the texture, that is not really the domain of RUBE and will depend on many other factors like what rendering API you are using.
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: smooth terrain

Post by vkreal »

ifMike,

Like iforce2d said, all you need to do is adapt Ray's tutorial to RUBE vertices instead of the sine curves. cocos2d-x 2 use opengl 2.0 which you would need to fix. I posted the fixed somewhere on this forum. I can't provide any code samples since i am way from home.


EDIT:

glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, _hillVertices);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, _hillTexCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nHillVertices);
Post Reply