-1

I am making a game in OpenGL with C++. I have a terrain that has hills and such and I want the character to be able to walk up and down the hill. To do this I have made a function that tries to find the the closest coordinates and return the corresponding y coordinates but its isn't working, my character is just staying at the same height. Here is my function:

float ViewPort::comparePosition(float xPos, float zPos) {
int closestValSoFar = 0;

for (int unit = 0; unit < sizeof(desert_scene_plainVerts)/sizeof(desert_scene_plainVerts[0]); unit++){
    int xDifference = terrainxPos[unit] - xPos;
    int zDifference = terrainzPos[unit] - zPos;
    int combinedDifferece = xDifference + zDifference;

    if (unit == 0) {
        closestValSoFar = unit;
    }
    if (combinedDifferece < (terrainxPos[unit-1] - xPos) + (terrainzPos[unit-1] - zPos)) {
        closestValSoFar = unit - 1;
    }
    else {
        closestValSoFar = unit;
    }
    if ((unit - 1) < sizeof(desert_scene_plainVerts)/sizeof(desert_scene_plainVerts[0])) {
        return terrainyPos[closestValSoFar];
    }

}

return terrainyPos[closestValSoFar];

}

I am calling this and using it with this code:

float yPos = ViewPort::comparePosition(Camera::position.x, Camera::position.z);
Camera::position.y = yPos+1.65;

Does anybody know how I can fix my code?

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • You have a really weird way of using `sizeof` ... If the size of the array is stack then enter that instead of `sizeof` ... If the size isn't static then `sizeof` isn't going to work (Give the correct "length"). – vallentin Dec 24 '13 at 14:37
  • But i thought that the length was sizeof(verts)/sizeof(verts[0]) – David Graovac Dec 24 '13 at 14:51
  • If the size of your `verts` array, is static, then yes that should return the size, but as said, if your array isn't static then it will give you problems. – vallentin Dec 24 '13 at 14:53
  • Can you please elaborate on what you mean by "closest coordinates"? Coordinates of what? Closest to what? To your character? – Valentin Dec 24 '13 at 17:55
  • Closest coordinates of the terrain to the character – David Graovac Dec 24 '13 at 18:19

2 Answers2

0

If I understood you correctly, you're trying to perform terrain clamping by comparing the position of the object and the terrain height at the given position; but the most common way to do this is by performing ray casting from the object's position to the terrain. Then you check if the ray intercepts the terrain and where it happens.

But making a game from openGL is really hard, why don't you try a 3d engine like OGRE?

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
  • Questions as comments please. Welcome to SO. – Felix K. Dec 24 '13 at 15:29
  • 1
    Not all questions must be comments. This question is another way of saying "You might want to consider this other way of doing it," which is a valid (if not favored) form of answer. – Wayne Conrad Dec 24 '13 at 19:29
0

If you are planning to do it this way, as I do like this simple method, I would simply rewrite the function a little bit. Honestly I think your method can be simplified a little more.

I made a few modifications and placed it here. I changed the adding to Pythagorean Theorem for a slight increase in accuracy, but if it causes a significant loss in performance, I'm sure that the old adding would work nearly as well as the new method.

float ViewPort::comparePosition(float xPos, float zPos) {
int closestValSoFar = 0;

for (int unit = 0; unit < sizeof(desert_scene_plainVerts)/sizeof(desert_scene_plainVerts[0]); unit++){
    int xDifference = terrainxPos[unit] - xPos;
    int zDifference = terrainzPos[unit] - zPos;
    int combinedDifferece = sqrt(xDifference*xDifference + zDifference*zDifference);

    if (unit == 0) {
        closestValSoFar = unit;
    }
    else if (combinedDifferece < closesValSoFar) {
        closestValSoFar = combinedDifference;
    }

}

return terrainyPos[closestValSoFar];

}

Honestly I really liked that old code and this is virtually the same, I simply checked whether the new distance is less than the closestValSoFar instead of the vertex before. I hope this helps!

I appreciate your endeavor to make a game with plain OpenGL, as I too deny engines (for what reason I don't know, I just find them boring. I'm not an artist, I'm a programmer, and engines are into artists and simplifying stuff)! I'd love to see your finished product, sounds cool!

Cosine
  • 572
  • 4
  • 18
  • Cheers for the help and completely agree with you about engines, would much rather try and do things myself, without an engine. – David Graovac Dec 27 '13 at 23:04