smooth terrain

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

smooth terrain

Post by vkreal »

Hi iforce2d,

Can you please give any ideas or clues on how to create smooth terrain using RUBE? an example like your "Downhill Supreme" terrain

by the way your RUBE tool is AWESOME!


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

Re: smooth terrain

Post by iforce2d »

Those terrains were made by hand, from a single chain shape (a 'line' shape in RUBE), as in the screenshot here: http://www.box2d.org/forum/viewtopic.php?f=6&t=9314

I'm not exactly sure what you mean by smooth, but scripts can be used to manipulate vertices, eg. to make a line smoother you might move each vertex closer to the average of its neighbors. I am thinking to add a few default scripts like that to the action menu sometime, so if you could be a bit more specific about what you need, maybe I could make a script that does it.
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: smooth terrain

Post by vkreal »

Hi iforce2d,

You answered smooth question with "move each vertex closer to the average of its neighbors". Any scripts to aid with creating terrain would be nice :)

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

Re: smooth terrain

Post by iforce2d »

Again, you need to be a bit more specific about what you need. What should the script do exactly? From what you've said so far, all I can suggest is:

- create an edge shape
- select one vertex of the shape and hit: E, E
- go crazy clicking where you want the terrain surface to be
- hit ESC to finish

You can find more details on vertex editing in this video: RUBE editor tutorial 8/17 - Editing vertices

To generate more vertices between existing vertices, maybe you could use the action menu item "Vertices -> Split edges" which will create more vertices between adjacent selected vertices. You could also modify the script for that action menu a little so that the new vertices are placed a bit more randomly.

About the smoothing script I mentioned, you can try this one which I will add to the default action menu in the next release:

Code: Select all

void main() {

	vertex[] vs = sv();

	if ( vs.length < 1 ) {
		print( "Please select the vertices to smooth." );
		return;
	}

	float smoothFactor = queryNumericValue("Smooth factor:", 0.25);
	if ( smoothFactor < 0 ) smoothFactor = 0;
	if ( smoothFactor > 1 ) smoothFactor = 1;

	array<vec2> newPositions( vs.length );
	for (uint i = 0; i < vs.length; i++) {
		vec2 oldPos = vs[i].pos;
		vec2 prevPos = vs[i].prev().pos;
		vec2 nextPos = vs[i].next().pos;
		vec2 midPos = 0.5 * (prevPos + nextPos);
		newPositions[i] = smoothFactor * midPos + (1-smoothFactor) * oldPos;
	}

	for (uint i = 0; i < vs.length; i++) 
		vs[i].setPos( newPositions[i] );
	
}

Here is the result of that script after running a few times:
Attachments
smoothedverts.png
smoothedverts.png (68.57 KiB) Viewed 16189 times
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: smooth terrain

Post by vkreal »

Hi iforce2d,

Your smoothing script will due for now. Thank you! Any advice or clue on how to generate the terrain mesh with opengl se 1 or 2? I am new to opengl se and any ideas would be greatly appreciated.

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

Re: smooth terrain

Post by iforce2d »

Do you mean the 3d mesh in the game? It's a bit complicated actually, and has nothing to do with RUBE:

For each vertex in the original line, make a line that goes downward into the ground, in the direction of the normal of that vertex, for a fixed distance. Connect the ends of all these new lines to make another ground line below the original one. If the new line crosses over itself, make a new point where it crosses, discard all points between where it crosses, and connect any vertices in that span on the original line to the newly inserted point. Repeat this a few times with a gradually increasing distance. This can all be done in 2d. Then to make it 3d, just give an increasing z-value for each line that was created. The normals of the 3d mesh can then be used to calculate light values at each vertex.
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: smooth terrain

Post by vkreal »

Hi Iforce2d,

What's the best way to pull out terrain vertices and coords from RUBE to pass to opengl 2

example:

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


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

Re: smooth terrain

Post by iforce2d »

Since your example uses triangle strip, I'll assume you want to draw the terrain as polygons, like the debug draw? If so, the tex coord pointer is not relevant (that's for images). Is there some reason you can't use the regular debug draw of Box2D?
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: smooth terrain

Post by vkreal »

Forgive my newb questions as I am trying to learn.

I am trying to figure out how to draw an image (ground sprite) to terrain created by RUBE like below. I might be completely doing this wrong. if so can anyone point me in the right direction?

// Create the ground texture
self.ground = [CCSprite spriteWithFile:@"Ground.png"];
ccTexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_CLAMP_TO_EDGE};
[self.ground.texture setTexParameters:&tp];

// Get terrain vertices and coords from RUBE to pass to opengl 2 in draw method
//
// HERE I would want to grab terrain vertices and coords ????
//
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: smooth terrain

Post by iforce2d »

There is a sample project for cocos2d which loads RUBE scenes and renders them, and does touch interaction with the scene. It is included in the trial download, or you can get it as a separate download from the users section if you have purchased, or from here: https://www.iforce2d.net/rube/?panel=loaders

That sample project would be the best place to start, to see how loading and rendering is done. Some of the scenes use the debug draw, and other ones show images. It is the base that the Downhill supreme game was built on.

You can see a demo in this video, from about 27:45
http://www.youtube.com/watch?v=7GcgF7-Xutk#t=27m45s

There are a couple of follow-up videos too, search YouTube for "Using RUBE with Cocos2d".
Post Reply