OpenGL The height function in the NeHe terrain code.

This topic is 2786 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi,

I've created the program that is here: http://www.dev-hq.co.uk/10-C++-OpenGL/180-Lesson-14-Realistic-terrain-and-Camera-movement
As it says in the lesson in which the terrain code is introduced, it uses a lot of code from the NeHe terrain tutorial (including the height function).

I attempted to add some collision detection to the program:

float CollisionDetection(float camX, float camY, float camZ){ //Our collision functionmaxBoundX = camX + 20;minBoundX = camX - 20;maxBoundY = camY - 20;minBoundY = camY + 20;maxBoundZ = camZ - 20;minBoundZ = camZ + 20;float x = camX/SCALE;float y = (camY/1.8f)/SCALE;float z = camZ/SCALE;FloorY = Height(HeightMap, x, z);FloorX = camX;FloorZ = camZ;minBoundY = (minBoundY/1.8f)/SCALE;if(FloorY >= minBoundY){cout << "Y COLLISION!\n minBoundY = " << minBoundY << "\nFloorY = " << FloorY << endl << endl;camY = FloorY;} else {cout << "No collision\n minBoundY = " << minBoundY << "\nFloorY = " << FloorY << endl << endl;}return camY;}

The camY value that is returned is then set to the players camY value in the Draw function.

I thought that this code should work, however it does not (because the Height function doesn't function as I thought). I've tried dividing things (for example I divided height by 1.8 because it's multiplied by 1.8 earlier in the program) but it still will not work.

Can anyone help with this (I really want collision detection to work),

Joe

Share on other sites
Bump?!
Can anyone help me with this issue?

Share on other sites
You are the author of the lesson and you want us to fix it for you.
You are not suited to teach other people.
In my opinion take down the site or make a blog about how you fail to teach others what you yourself don't understand.

Share on other sites
Quote:

Yes I did - however the thread got very off topic.

Quote:
 You are the author of the lesson and you want us to fix it for you.

I am the author of the lesson. I want to figure out why my code doesn't work both for personal and business reasons - I want you to "fix it" in the same sense that anyone who asks for help wants help.

Quote:
 You are not suited to teach other people.

I strongly disagree. I get a lot of positive feedback from my teachings (in fact I have never received any negative feedback) - teaching people while I learn means that bits that the bits I have difficulty with can be highlighted.

Quote:
 In my opinion take down the site or make a blog about how you fail to teach others what you yourself don't understand.

All feedback I have received so far is positive, and people tell me that they learn better than they do anywhere else.
I admit that the OpenGL section is not my strongest section, however it does help people learn (including myself).

I really don't want this to turn into an argument thread - I will continue making tutorials whatever happens as it IS helping people.

I just want to know myself why the code isn't working, and I want to be able to share this knowledge with others.

Regards,

Joe

Share on other sites
You're passing x,z into Height() after multiplying them by SCALE. The Height() function expects these values pre-SCALE. I deemed this from the DrawVertex() function.

You also are not adding HALF_SIZE to your input coordinates before querying them. Also looking to DrawVertex(), you'll note that coordinates (0,0) correspond to the center coordinate in your heightmap, not to (0,0) in your heightmap.

I'm guessing you also don't know your collision function will snap to each terrain data height, and not interpolate smoothly between heights. This means your camera can still end up underneath some slopes, and unable to descend all the way to ground level over some slopes.

Lastly, your function goes undefined and possibly segmentation-faulty if your camera wanders off the bounds of the map.

These aren't "highlighting your errors"; these are basic beginner's mistakes that took less time to spot than to type out and explain. You shouldn't be writing tutorials.

Share on other sites
Quote:
Original post by Joesavage1
Quote:
 You are the author of the lesson and you want us to fix it for you.

I am the author of the lesson. I want to figure out why my code doesn't work both for personal and business reasons - I want you to "fix it" in the same sense that anyone who asks for help wants help.

Well, when you state that you have tried dividing "things" and it still doesn't work, it tells me that you are not sure at all what your code is actually doing.

Quote:
Original post by Joesavage1
Quote:
 You are not suited to teach other people.

I strongly disagree. I get a lot of positive feedback from my teachings (in fact I have never received any negative feedback) - teaching people while I learn means that bits that the bits I have difficulty with can be highlighted.

Firstly:
Consider this your first negative feedback then.

Secondly:
People looking at tutorials expect that the author of it knows exactly what he is doing, which you don't seem to do.
If you want your lessons to be some sort of work group where you collectivly discuss and practice, I'm all for it.
But you don't state anywhere on your lessons about your expertise or how the lessons should be interpreted.

Quote:
Original post by Joesavage1
Quote:
 In my opinion take down the site or make a blog about how you fail to teach others what you yourself don't understand.

All feedback I have received so far is positive, and people tell me that they learn better than they do anywhere else.
I admit that the OpenGL section is not my strongest section, however it does help people learn (including myself).

Okay, it might have been abit harsh.
But this problem you are posting about has nothing to do with OpenGL, it's about your alghorithm.

Quote:
 Original post by Joesavage1I really don't want this to turn into an argument thread - I will continue making tutorials whatever happens as it IS helping people.

Hey, I can't stop you from doing what you want, but you could be atleast be honest to your viewers, or have you planned to redirect them here every time they need help with your lessons ?

Quote:
 Original post by Joesavage1I just want to know myself why the code isn't working, and I want to be able to share this knowledge with others.

The code is fully functional, it is not just doing what you had planned.
The problem is PEBKAC and you are not sure what the functions you made actually returns/does.
I would recommend using more comments so you know what a piece of code does and name your functions better than "Height" and "CollisionDetection".
Since functions are doing something you should name them as verbs (i.e. DoSomething or GetSomething).
Not to mention since you are using an OOP language like C++ perhaps you should try and write it more object oriented.

Share on other sites
Quote:
 You're passing x,z into Height() after multiplying them by SCALE. The Height() function expects these values pre-SCALE. I deemed this from the DrawVertex() function.You also are not adding HALF_SIZE to your input coordinates before querying them. Also looking to DrawVertex(), you'll note that coordinates (0,0) correspond to the center coordinate in your heightmap, not to (0,0) in your heightmap.

Ok.. So what you're saying is I shouldn't times the x and z values by the scale until I have already used the Height function, and that I need to add HALF_SIZE to all the coords as well?
This is the code I came up with using what I think these instructions mean:

float CollisionDetection(float camX, float camY, float camZ){ //Our collision functionmaxBoundX = camX + 20;minBoundX = camX - 20;maxBoundY = camY - 20;minBoundY = camY + 20;maxBoundZ = camZ - 20;minBoundZ = camZ + 20;float x = camX*SCALE;float y = camY*SCALE*1.8f;float z = camZ*SCALE;minBoundY = minBoundY*1.8f*SCALE;FloorY = Height(HeightMap, camX + HALF_SIZE, camZ + HALF_SIZE)*SCALE*1.8f;FloorX = camX*SCALE;FloorZ = camZ*SCALE;if(FloorY >= minBoundY){cout << "Y COLLISION!\n minBoundY = " << minBoundY << "\nFloorY = " << FloorY << endl << endl;camY = FloorY;} else {cout << "No collision\n minBoundY = " << minBoundY << "\nFloorY = " << FloorY << endl << endl;}return camY;}

This doesn't work (same problem as usual, collision in strange places) - I'm sorry if I mis-understood your instructions, I am new to making games, and these types of programs.

Quote:
 I'm guessing you also don't know your collision function will snap to each terrain data height, and not interpolate smoothly between heights. This means your camera can still end up underneath some slopes, and unable to descend all the way to ground level over some slopes.Lastly, your function goes undefined and possibly segmentation-faulty if your camera wanders off the bounds of the map.

I'm not bothered about completely smooth collision detection, I just want basic collision detection.
I know about the going off the map thing, I'm planning to add bounderies after I have the regular collision working..

Quote:
 These aren't "highlighting your errors"; these are basic beginner's mistakes that took less time to spot than to type out and explain. You shouldn't be writing tutorials.

I'm very sorry but I am new to creating small games, and haven't worked with code like this before. I won't be publishing a tutorial on this until I completely understand it anyway.

Joe

Share on other sites
For example - we have 4 points - (0,0,0), (0,1,1), (1,0,0), (1,0,1) and location of the camera - (0.3,y,0.4). U want to check the "y". It's impossible to do it using "int Height(BYTE *pointHeightMap, float x, float z)" function only. U need values between the vertices.

Share on other sites
Quote:
 For example - we have 4 points - (0,0,0), (0,1,1), (1,0,0), (1,0,1) and location of the camera - (0.3,y,0.4). U want to check the "y". It's impossible to do it using "int Height(BYTE *pointHeightMap, float x, float z)" function only. U need values between the vertices.

Ah ok, so what you are basically saying is that my collision code is completely flawed because the Height function doesn't get values between the verticies?
Hmm.. I guess I'll have to try to figure out how to get values between the verticies ^_^

Regards,

Help would be appreciated,

Joe

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631775
• Total Posts
3002275
×