Sign in to follow this  
Joesavage1

OpenGL The height function in the NeHe terrain code.

Recommended Posts

Joesavage1    100
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 function


maxBoundX = 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),

Thanks In Advance,

Joe

Share this post


Link to post
Share on other sites
Molle85    172
You posted about this one month ago.
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 this post


Link to post
Share on other sites
Joesavage1    100
Quote:

You posted about this one month ago.

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.



Please Help,

Regards,

Joe

Share this post


Link to post
Share on other sites
Wyrframe    2426
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 this post


Link to post
Share on other sites
Molle85    172
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.
Besides the solution has already been posted on the previous thread.

Quote:
Original post by Joesavage1
I 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 Joesavage1
I 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 this post


Link to post
Share on other sites
Joesavage1    100
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 function


maxBoundX = 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.


Please Help,

Thanks In Advance,

Joe

Share this post


Link to post
Share on other sites
_OskaR    144
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 this post


Link to post
Share on other sites
Joesavage1    100
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now