A quick question regarding Downhill Supreme

General discussion about the R.U.B.E editor
banarne
Posts: 19
Joined: Fri Aug 23, 2013 1:30 pm

Re: A quick question regarding Downhill Supreme

Post by banarne »

I can get it to work under a sine curve.

However I can't get it to work with vertices from RUBE, and since you fixed it I was just wondering how you applied the technique using RUBE vertices.

Was just looking for some helpful pointers :)
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: A quick question regarding Downhill Supreme

Post by vkreal »

what problems are you having? which cocos2d version?
banarne
Posts: 19
Joined: Fri Aug 23, 2013 1:30 pm

Re: A quick question regarding Downhill Supreme

Post by banarne »

cocos2d v.2

I am able to store my vertices in an array(_hillVertices = CGPointMake(x, y);) and draw it as a line using ccDrawLine (not with the opengl method you supplied) in my draw method.

I'm not able to use a texture below my b2ChainShape.

What I wanna do:

1. loop through my vertices and save their coordinates (done)
2. use glVertexAttribPointer to show my line being drawn (so that I won't have to use ccDrawLine).
3. render with a texture using open gl es 2(should be ready when step 2 is done?)

the tutorial at wenderlichs site is great for dynamically creating curves but I can't get the same effect using my RUBE vertices. I've tried all day, but you know what it's like when you get stuck on a problem, you can't think straight - at least I can't :)

any help is greatly appreciated. thanks.

EDIT: what's confusing me most right now is

_hillVertices <- the vertex coordinates?
_hillTexCoords <- ? same coordinates converted to opengl points?
_nHillVertices <- the convertex vertex points?


The code from Wenderlich that messes it up is this:

Code: Select all

 if (prevFromKeyPointI != _fromKeyPointI || prevToKeyPointI != _toKeyPointI) {
        
        // vertices for visible area
        _nHillVertices = 0;
        _nBorderVertices = 0;
        CGPoint p0, p1, pt0, pt1;
        p0 = _hillKeyPoints[_fromKeyPointI];
        for (int i=_fromKeyPointI+1; i<_toKeyPointI+1; i++) {
            p1 = _hillKeyPoints[i];
            
            // triangle strip between p0 and p1
            int hSegments = floorf((p1.x-p0.x)/kHillSegmentWidth);
            float dx = (p1.x - p0.x) / hSegments;
            float da = M_PI / hSegments;
            float ymid = (p0.y + p1.y) / 2;
            float ampl = (p0.y - p1.y) / 2;
            pt0 = p0;
            _borderVertices[_nBorderVertices++] = pt0;
            for (int j=1; j<hSegments+1; j++) {
                pt1.x = p0.x + j*dx;
                pt1.y = ymid + ampl * cosf(da*j);
                _borderVertices[_nBorderVertices++] = pt1;
                
                _hillVertices[_nHillVertices] = CGPointMake(pt0.x, 0 + minY);
                _hillTexCoords[_nHillVertices++] = CGPointMake(pt0.x/512, 1.0f);
                _hillVertices[_nHillVertices] = CGPointMake(pt1.x, 0 + minY);
                _hillTexCoords[_nHillVertices++] = CGPointMake(pt1.x/512, 1.0f);
                
                _hillVertices[_nHillVertices] = CGPointMake(pt0.x, pt0.y);
                _hillTexCoords[_nHillVertices++] = CGPointMake(pt0.x/512, 0);
                _hillVertices[_nHillVertices] = CGPointMake(pt1.x, pt1.y);
                _hillTexCoords[_nHillVertices++] = CGPointMake(pt1.x/512, 0);
                
                pt0 = pt1;
            }
            
            p0 = p1;
        }
I'm trying to rewrite this to make it work with RUBE. I'm aware that it's somewhat off-topic. But since you asked the same question and seem to have solved the problem - any help from you would be great.

How did you solve it?
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: A quick question regarding Downhill Supreme

Post by vkreal »

try, i am using cocos2d-x 2.1.4 c++

if (_nHillKeyPoints >= _toKeyPointI && (prevFromKeyPointI != _fromKeyPointI || prevToKeyPointI != _toKeyPointI)) {
// vertices for visible area
_nHillVertices = 0;
_nBorderVertices = 0;
CCPoint p0, p1, pt0, pt1;
p0 = _hillKeyPoints[_fromKeyPointI];
//for (int i=1; i<_nHillKeyPoints; i++) {
for (int i=_fromKeyPointI+1; i<_toKeyPointI+1; i++) {
p1 = _hillKeyPoints;

// triangle strip between p0 and p1
int hSegments = floorf((p1.x-p0.x)/kHillSegmentWidth);
float dx = (p1.x - p0.x) / hSegments;


float dy_total_distance = p0.y - p1.y;
float dy = abs((dy_total_distance) / hSegments);

pt0 = p0;

_borderVertices[_nBorderVertices++] = pt0;
for (int j=1; j<hSegments+1; j++) {
int vSegments = 1;
pt1.x = p0.x + j*dx;
if(p0.y < p1.y)
{
pt1.y = p0.y + dy *j;
}
else
{
pt1.y = p0.y - dy *j;
}

float sprite_h = 1024.0; // 512

_hillVertices[_nHillVertices] = CCPointMake(pt0.x, -m_nMaxHillVerticeHeight);
_hillTexCoords[_nHillVertices++] = CCPointMake(pt0.x/sprite_h, 1.0f);
_hillVertices[_nHillVertices] = CCPointMake(pt1.x, -m_nMaxHillVerticeHeight);
_hillTexCoords[_nHillVertices++] = CCPointMake(pt1.x/sprite_h, 1.0f);

_hillVertices[_nHillVertices] = CCPointMake(pt0.x, pt0.y);
_hillTexCoords[_nHillVertices++] = CCPointMake(pt0.x/sprite_h, 0);
_hillVertices[_nHillVertices] = CCPointMake(pt1.x, pt1.y);
_hillTexCoords[_nHillVertices++] = CCPointMake(pt1.x/sprite_h, 0);



pt0 = pt1;
}
p0 = p1;
}
banarne
Posts: 19
Joined: Fri Aug 23, 2013 1:30 pm

Re: A quick question regarding Downhill Supreme

Post by banarne »

thanks, but that's basically the exact same code from Ray? I might be thick but I don't get it.

I'm guessing you're using the code iforce2d supplied to loop through the vertices?

Code: Select all

 
 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 ...
}
the code you posted, are you using it in your -(void)afterLoadProcessing:(b2dJson*)json method?
to me it seems like there's a lot of obsolete code - I could be wrong of course.

vkreal, can you post a sample of a stripped down project showing your code in action? thanks again
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: A quick question regarding Downhill Supreme

Post by iforce2d »

I'm wondering if you have actually made vertices for a triangle strip, instead of just the line that you get from RUBE. You mention that you can draw the line segments from RUBE with ccDrawLine, but did you then create another lower line of vertices necessary to make triangles from that? Putting textures aside for a moment, can you draw the triangles you need with just plain color?

The simplest method of creating a triangle strip from the RUBE line would have one vertex underneath every vertex in the RUBE line, and these would need to be at every second position in the vertex array. The overall vertex array used to draw should have twice as many vertices as were loaded from the RUBE line. Have you done anything like that?

Note that this technique only works if the vertices in the original line are consistently going from left to right (or vice versa), otherwise the resulting shape will be concave and you will need more complex methods of generating triangles for it.
vkreal
Posts: 66
Joined: Sun Jan 13, 2013 7:29 pm

Re: A quick question regarding Downhill Supreme

Post by vkreal »

sorry can't give out code belong to others, but i can point you in right direction. Code up above works, it's use to fill RUBE terrain vertices and texCoords for draw method, this code should be inside your update method. _hillKeyPoints should be filled with your rube terrain points. Like stated before its almost exactly like Ray's tut instead use terrain vertices.

Hope that helps!
banarne
Posts: 19
Joined: Fri Aug 23, 2013 1:30 pm

Re: A quick question regarding Downhill Supreme

Post by banarne »

iforce2d wrote:I'm wondering if you have actually made vertices for a triangle strip, instead of just the line that you get from RUBE. You mention that you can draw the line segments from RUBE with ccDrawLine, but did you then create another lower line of vertices necessary to make triangles from that? Putting textures aside for a moment, can you draw the triangles you need with just plain color?

The simplest method of creating a triangle strip from the RUBE line would have one vertex underneath every vertex in the RUBE line, and these would need to be at every second position in the vertex array. The overall vertex array used to draw should have twice as many vertices as were loaded from the RUBE line. Have you done anything like that?

Note that this technique only works if the vertices in the original line are consistently going from left to right (or vice versa), otherwise the resulting shape will be concave and you will need more complex methods of generating triangles for it.

thanks buddy, this pointed me in the right direction - problem solved!
Post Reply