Jump to content
  • Advertisement
Sign in to follow this  
Josheir

Very Easy Beginner's Question (OpenGL)

Recommended Posts

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

Edited by Josheir

Share this post


Link to post
Share on other sites
Advertisement

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.

Edited by fleabay

Share this post


Link to post
Share on other sites

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

Share this post


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

Share this post


Link to post
Share on other sites

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 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Edited by fleabay

Share this post


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

Edited by Josheir

Share this post


Link to post
Share on other sites
1 hour ago, Josheir said:

I'm coding in C++ and this was my first try with help from Java.

That's good to know. I was worried that you may have burned out on C++.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!