Manta

A way to add a nice looking cursor on a landscape surface or even to add one or more 2D meshes to your landscape is to follow the manta example. As probably you know then manta is a fish.. It looks like following the floor heights while swimming

In this tutorial we will add to the landscape a mesh which will cover the soil following the landscape heights. Without loss of generality we can assume the 2D object (the manta) is square. The following code is Visual Basic 6 (still use!). First of all we need a mesh:

Set mesh = Scene.CreateMeshBuilder(name)

Second we will add a FloorGrid:

dd = dx / 2
mesh.AddFloorGrid it, -dd, -dd, dd, dd, NumOfCells, NumOfCells

Where:

Let us note as the floor is created at the origin on the x/z plane (y=0). Meshes are based on triangles and vertices. Our floorgrid has made of vertices at given x,y,z coordinates. Later calls to setpostion, setrotation and setscale will NOT change the mesh coordinates but only apply the transformation matricides for translation, scaling and rotation.

To follow the landscape what we have to do is to change the y coordinate of the mesh in a proper manner. So, when we have to set the mesh position, we change the vertices Y coordinates on the fly to follow the landscape shape. If the mesh was not scaled and/or rotated this can be done with a simple loop getting and setting the vertices:

n = mesh.GetVertexCount - 1
For i = 0 To n
 ' get the vertex
 mesh.GetVertex i, fx, fy, fz, fnx, fny, fnz, fu1, fv1, fu2, fv2, color
xx= fx + x
zz = fz + z
 ' get the landscape height
 fy = Land.GetHeight(xx, zz) - y + 0.2
 ' and normal
 v = Land.GetNormal(xx, zz)
 fxn = v.x
 fyn = v.y
 fzn = v.z
 ' set the vertex
 mesh.SetVertex i, fx, fy, fz, fnx, fny, fnz, fu1, fv1, fu2, fv2, color
Next i

Where: x and z are the new position of the mesh

fx,fy,fz are the vertex coordinates (careful: when the mesh was added)

fu1,fu2,fv1,fu2 are the UV coordinates to apply the texture (we don’t use them)

fnx,fny,fnz are the vertex normal (careful: when the mesh was added)

color: the color of the mesh (we do not change that)

Vertices are indexed 0 to Vertexcount-1.

As probably you know the Land.GetHeight(xx, zz) function will give the height of the landscape at a given position. Land.GetNormal(xx, zz) gives the 3d vector of the landscape normal at the given coordinates.

The 0.2 factor is a small number (20 cm) to accommodate the mesh on the landscape without strange effects due to the approximation we are following (number of grid cells).

What the for/next loop change are the y coordinates and the normals.

As far you understand the above loop we have done. We only need to add some code to manage the scale and the rotation if needed.

The for/next loop will take into account scale and rotation:

r = mesh.GetRotation
s = mesh.GetScale
' sin and cos for the rotation
cosa = Cos(r.y / 180 * PI)
sina = Sin(r.y / 180 * PI)
n = mesh.GetVertexCount - 1
For i = 0 To n
 ' get the vertex
 mesh.GetVertex i, fx, fy, fz, fnx, fny, fnz, fu1, fv1, fu2, fv2, color
 ' scale and rotate the vertex
 xx = s.x * fx * cosa + s.z * fz * sina + x
 zz = -s.x * fx * sina + s.z * fz * cosa + z
 ' get the landscape height
 fy = Land.GetHeight(xx, zz) - y + 0.2
 ' and normal
 v = Land.GetNormal(xx, zz)
 fxn = v.x
 fyn = v.y
 fzn = v.z
 ' set the vertex
 mesh.SetVertex i, fx, fy, fz, fnx, fny, fnz, fu1, fv1, fu2, fv2, color
Next i

If you want you can test the MantaTemplate example. What you need is a Visual Basic 6 and, of course, the Truevision 6.5. Click here

musmeci@casaccia.enea.it

 
tutorialsarticlesandexamples/manta.txt · Last modified: 2013/11/22 13:32