Advertisement

Very Easy Beginner's Question (OpenGL)

Started by February 13, 2019 10:49 PM
9 comments, last by fleabay 5 years, 6 months ago

This is a very easy question concerning OpenGL.  The start of the function in question is below.

 

public float getHeightOfTerrain(float worldX, float worldZ)

{

float terrainX= worldX - this.x;

float terrainZ = worldZ - this.z;

 

I am not understanding what the terrain values are being set to.  I'm missing something and could really use some help.

 

Thank you,

Josheir

That code is originally (maybe) from a ThinMatrix tutorial series. First video below is setting up the terrain and second video is about the terrain function you asked about.

https://www.youtube.com/watch?v=O9v6olrHPwI

https://www.youtube.com/watch?v=6E2zjfzMs7c

TL;DW The code is from a function that determines the Y height location of an entity based on his location (worldX, worldZ from your example) on the terrain. this.x and this.z are member variables of the terrain class.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Advertisement

Well, yes it's from there.  I don't understand the code, however.  What is happening with these two lines of code?  What exactly is this.x and for that matter what is terrainX and I am assuming worldX is a value like one or fifteen, something like that?

Thanks,

Josheir

7 minutes ago, Josheir said:

Well, yes it's from there.  I don't understand the code, however.  What is happening with these two lines of code?  What exactly is this.x and for that matter what is terrainX and I am assuming worldX is a value like one or fifteen, something like that?

Thanks,

Josheir

I've been planning on watching the series but never got around to it. Let me check out a few of the terrain videos and I will reply in about 2/3 hours if you haven't gotten it sorted or answered otherwise.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Sure thanks, I'm watching the first one you recommended right now.  I was not understanding for what reason terrain would be located to the left of WorldX, too.

 

Josheir 

2 minutes ago, Josheir said:

Sure thanks, I'm watching the first one you recommended right now.  I was not understanding for what reason terrain would be located to the left of WorldX, too.

Actually I had to find an earlier video to start on...

https://www.youtube.com/watch?v=yNYwZMmgTJk

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

Advertisement

Most importantly is the this.x and this.y to use this function.  this.x is equal gridx * size.  Size is 800.  However, I don't know what gridx is yet.  

Josheir

11 minutes ago, Josheir said:

I don't know what gridx is yet.   

Each terrain is just part of the whole terrain. Each terrain is placed on a grid, from the video 800 units apart. this.x and this.z are the actual terrain section you are currently located on. You set the terrain.x and terrain.z when you create that terrain section.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

OK, I was confusing myself some in that last reply. this.x/this.y is the location of the corner of that terrain section in world coordinates. In the example, the sections are 800 X 800 so the corner of the first section at (0,0) ie (gridX, gridY) is at world coordinates (0,0). If you had another section next to it, it would be at (1,0) located at world coordinates(800,0). To determine where you are standing on a given terrain section, you would subtract your (x,y) world coordinates from the section's world coordinates that you are on.


public float getHeightOfTerrain(float worldX, float worldZ)
{
	float terrainX= worldX - this.x;
	float terrainZ = worldZ - this.z; 

So you pass in your players world coordinates and it subtracts the size of the this.x/this.y of the terrain section to determine exactly where you are on that that section. If you were on section (0,0) then nothing is changed. If you were on section (1,0) then it would subtract (800,0) from the players location. So if your player was at world coordinates (1200,400), then it would work out (1200 - 800, 400 - 0) thus you are on (400,400) of that section (1,0)

Hope this helps. I'm going to implement this in my engine. :)

BTW, gridX/gridY are set from the parameters for the constructor of terrain.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

17 hours ago, fleabay said:

OK, I was confusing myself some in that last reply. this.x/this.y is the location of the corner of that terrain section in world coordinates. In the example, the sections are 800 X 800 so the corner of the first section at (0,0) ie (gridX, gridY) is at world coordinates (0,0). If you had another section next to it, it would be at (1,0) located at world coordinates(800,0). To determine where you are standing on a given terrain section, you would subtract your (x,y) world coordinates from the section's world coordinates that you are on.



public float getHeightOfTerrain(float worldX, float worldZ)
{
	float terrainX= worldX - this.x;
	float terrainZ = worldZ - this.z; 

So you pass in your players world coordinates and it subtracts the size of the this.x/this.y of the terrain section to determine exactly where you are on that that section. If you were on section (0,0) then nothing is changed. If you were on section (1,0) then it would subtract (800,0) from the players location. So if your player was at world coordinates (1200,400), then it would work out (1200 - 800, 400 - 0) thus you are on (400,400) of that section (1,0)

Hope this helps. I'm going to implement this in my engine. :)

BTW, gridX/gridY are set from the parameters for the constructor of terrain.

 

17 hours ago, fleabay said:

Hope this helps. I'm going to implement this in my engine. :)

Yes, I was looking for help for determining height, cool site!  I'm coding in C++ and this was my first try with help from Java.  

Thank you fleabay; your help is super,

Josheir

This topic is closed to new replies.

Advertisement