Jump to content

[SOLVED] How to find current terrain point?


photo

Recommended Posts

Many functions in ObjectTerrain class need to use the grid point 

such as 

  • int x - X coordinate of the point on the terrain grid in range from 0 to the maximum terrain widwth.
  • int y - Y coordinate of the point on the terrain grid in range from 0 to the maximum terrain length.

how can I know which gird point in the terrain that my current position belongs to ?

Thanks

Link to comment

Hello de-Roo.Lukas,

Check out the code sample below (C++ ). It should help you to get the coordinates you need.

 

void updateHeight(ObjectTerrainPtr terrain, Vec3 player_position) {
	vec4 local_position = vec4(terrain->getIWorldTransform() * Vec4(player_position, 1.0f));

  	// checking if the player is above the terrain
	if (local_position.x > 0 && local_position.y > 0){
		// calculating x,y coordinates of terrain point under the player and updating height value
		int x = ftoi(local_position.x / terrain->getStep());
		int y = ftoi(local_position.y / terrain->getStep());
		terrain->setHeight(x, y, 1.0f);
	}
}

To get current player's position you can use:

Vec3 player_position = Game::get()->getPlayer()->getWorldPosition());

Thanks!

Link to comment
  • 6 months later...

Hi de-Roo.Lukas,

Guess the ObjectTerrainGlobal::fetchTopologyData() method should help in your case.

E.g, to get terrain height under the current player's position you  can use the following code:

double getTerrainHeightAt(ObjectTerrainGlobalPtr terrain, Vec3 player_position) {

	// auxiliary variables to store topology data
	Vec3 t_point;
	vec3 normal;
	vec3 up;
	
	// getting local position of the player
	vec4 local_position = vec4(terrain->getIWorldTransform() * Vec4(player_position, 1.0f));

	// fetching topology data for the point that corresponds to player's position
	terrain->fetchTopologyData(local_position.x, local_position.y, t_point, normal, up, 1);
	
	// returning terrain height at player's position
	return t_point.z;
}

Current player's position is obtained the same way:

Vec3 player_position = Game::get()->getPlayer()->getWorldPosition());

Thanks!

Link to comment
6 hours ago, de-Roo.Lukas said:

Now I update to 2.7.1 there is no objectTerrain

Is this true??? Or is it being removed in 2.8 as scheduled? I couldn't find any reference to it in the change logs.

We intend to port our terrain generator and various rendering modifications to TerrainGlobal on the final 2.7.x because 2.6 doesn't have the migration tools. But 2.7 sounds very buggy and we were planning to skip it.

Link to comment
  • silent changed the title to [SOLVED] How to find current terrain point?
×
×
  • Create New...