Jump to content

Create nodes according to the ground.


Recommended Posts

When creating a node by calling a file from c++ code, is there a way to automatically create a node so that it touches the ground even if the position is specified as setWorldPosition and created higher than the ground?

Link to comment


Yes, you surely can do it. You need to do an intersection test (cast ray from top to the ground level) and get Z coordinate in the specific point. After that you can place your node in this point with an offset.

What are you currently using as ground (ObjectTerrainGlobal / ObjectLandscapeTerrain or regular mesh)?


How to submit a good bug report
FTP server for test scenes and user uploads:

Link to comment

this is a simple example of how to set the height of an object to ground level.

bool drop_to_ground(const ObjectTerrainGlobalPtr &ground, const NodePtr &node, bool up_to_normal = true)
	// get original transform
	Mat4 node_world_transform = node->getWorldTransform();
	// world node position to local terrain position
	Vec3 position = ground->getIWorldTransform() * node_world_transform.getTranslate();
	ObjectIntersectionNormalPtr wi = ObjectIntersectionNormal::create();
	// get vertical intersection
	if (ground->getIntersection(position + UNIGINE_VEC3::UP  * 1000, position - UNIGINE_VEC3::UP  * 1000, wi, 0))
		// terrain local position to world position
		position = ground->getWorldTransform() * wi->getPoint();
		// change z of original transform
		node_world_transform.m23 = position.z;
		if (up_to_normal)
			vec3 normal;
			mul3(normal, ground->getWorldTransform(), wi->getNormal());
			node->setWorldTransform(setTo(node_world_transform.getTranslate(), node->getWorldTransform() * Vec3::FORWARD, normal, AXIS_Y));
		return true;
	return false;

I'm not sure if this works 100%. ask if there are any problems

Link to comment
  • Create New...