custom scenenode

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

custom scenenode

Post by Seven »

I now have 3 days invested in this. sigh......
Looking for some help with my custom scenenode please.
I am trying to create a terrain editor brush gizmo that sets itself to the height of the terrain.

I have posted a lot of code so sorry about that

derived from IMeshSceneNode and utilizes
Mesh = SceneManager->getGeometryCreator()->createPlaneMesh(dimension2df(1,1),dimension2du(SizeX,SizeZ));
to create a 'plane' mesh with sizex, sizez sections.
this all works fine and I can move it to the mouse picked location fine.

my trouble is in the code for calculating the vertex's real world position.
for some reason I get (randomly) 'nan' values for the vertices[].Pos.X or Z values

Code: Select all

		void IGE_TerrainSelectorSceneNode::OnAnimate(u32 timeMs)
		{
			///////////////////////////////////////////////////////////
			// if the terrain is valid
			if (Terrain)
			{
				updateAbsolutePosition();

				printf("----------------------------------\n");

				// this node's meshbuffer
				IMeshBuffer* mb = Mesh->getMeshBuffer(0);

				// this node's vertices
				S3DVertex2TCoords* pVertices = (S3DVertex2TCoords*)mb->getVertices();

				// terrain bounding box
				aabbox3df tb = Terrain->getTransformedBoundingBox();

				// minimum and maximum terrain boundaries
				// could use the tb directly but this is easier to read
				float minx = tb.MinEdge.X;
				float minz = tb.MinEdge.Z;
				float maxx = tb.MaxEdge.X;
				float maxz = tb.MaxEdge.Z;

				// this nodes position
				vector3df p = getPosition();

				// this node's scale
				vector3df scale = getScale();

				printf("position = %f %f %f\n", p.X, p.Y, p.Z);

				// for each vertex in this nodes meshbuffer
				for (int v = 0; v < mb->getVertexCount(); ++v)
				{
					// get the vertex real world position x and z
					// (since the node's position is set by the mouse position)
					//   scale the vertex pos.x and pos.z to make real world relative to this node's scale
					//      (because vertex pos is like 1,1,1 and then scaled by 10)
					//   and adding this node's position to make real relative to this node's position
					// this should give us the vertex position in real world coordinates
[b]					float x = p.X+(scale.X * pVertices[v].Pos.X);
					float z = p.Z+(scale.Z * pVertices[v].Pos.Z);
					printf("ScaleX = %f ScaleZ = %f\n", scale.X, scale.Z);
					printf("pVertices[%d].X = %f pVertices[%d].Z = %f\n", v,pVertices[v].Pos.X, v, pVertices[v].Pos.Z);
					printf("scaledX = %f scaledZ = %f\n", x, z);
[/b]

					// if the real world coordinates are within the terrain boundaries
					// this keeps us from trying to calculate anything outside the terrain
					if (inRange(x, z, minx, minz, maxx, maxz))
					{
						// get the height of the terrain at the real world coordinates
						float th = Terrain->getHeight(x, z);

						// convert the terrain height to this node's unscaled height
						float h = th;

						// set this node's vertex height to the terrain's height 
						//pVertices[v].Pos.Y = h;

						printf("v = %d  x = %f z = %f  h = %f\n\n", v, x, z, h);
					}
					else
					{
						printf("vertex %d is out of range %f %f\n\n", v, pVertices[v].Pos.X, pVertices[v].Pos.Z);
					}
				}
			}
			///////////////////////////////////////////////////////////


			IMeshSceneNode::OnAnimate(timeMs);
		}
my output with this code

Code: Select all

----------------------------------
position = 246.743134 148.209915 386.808624
ScaleX = 10.000000 ScaleZ = 10.000000
pVertices[0].X = -1.000000 pVertices[0].Z = -1.000000
scaledX = 236.743134 scaledZ = 376.808624
v = 0  x = 236.743134 z = 376.808624  h = 137.810287

ScaleX = 10.000000 ScaleZ = 10.000000
pVertices[1].X = 0.000000 pVertices[1].Z = 1.000000
scaledX = 246.743134 scaledZ = 396.808624
v = 1  x = 246.743134 z = 396.808624  h = 133.138596

[b]ScaleX = 10.000000 ScaleZ = 10.000000
pVertices[2].X = 1.000000 pVertices[2].Z = -nan
scaledX = 256.743134 scaledZ = -nan
v = 2  x = 256.743134 z = -nan  h = -340282346638528859811704183484516925440.000000
[/b]
ScaleX = 10.000000 ScaleZ = 10.000000
pVertices[3].X = -nan pVertices[3].Z = 1.000000
scaledX = -nan scaledZ = 396.808624
v = 3  x = -nan z = 396.808624  h = -340282346638528859811704183484516925440.000000

ScaleX = 10.000000 ScaleZ = 10.000000
pVertices[4].X = 0.500000 pVertices[4].Z = 0.000000
scaledX = 251.743134 scaledZ = 386.808624
v = 4  x = 251.743134 z = 386.808624  h = 141.680679

ScaleX = 10.000000 ScaleZ = 10.000000
pVertices[5].X = 0.000000 pVertices[5].Z = -0.000000
scaledX = 246.743134 scaledZ = 386.808624
v = 5  x = 246.743134 z = 386.808624  h = 140.209915

ScaleX = 10.000000 ScaleZ = 10.000000
pVertices[6].X = -0.000000 pVertices[6].Z = 0.000000
scaledX = 246.743134 scaledZ = 386.808624
v = 6  x = 246.743134 z = 386.808624  h = 140.209915

ScaleX = 10.000000 ScaleZ = 10.000000
pVertices[7].X = 0.000000 pVertices[7].Z = 1.000000
scaledX = 246.743134 scaledZ = 396.808624
v = 7  x = 246.743134 z = 396.808624  h = 133.138596

ScaleX = 10.000000 ScaleZ = 10.000000
pVertices[8].X = -0.000000 pVertices[8].Z = 0.000000
scaledX = 246.743134 scaledZ = 386.808624
v = 8  x = 246.743134 z = 386.808624  h = 140.209915
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: custom scenenode

Post by CuteAlien »

createPlaneMesh uses S3DVertex, not S3DVertex2TCoords. So you are working with the wrong type here.
But easiest way to get exact collision coordinates in Irrlicht is usually to create a triangle-selector and use the collision functions.
Thought I never tested with Terrain and I remember the terrainscenenode had some troubles about ignoring transformations for collisions (open todo, sadly I never worked with that node myself, so I haven't looked at this yet).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: custom scenenode

Post by Seven »

Unbelievable. Sigh. You have no idea how much hair I pulled out in this one. Thanks again for all your help!
Post Reply