About Store Forum Documentation Contact

Post Reply 
Get slope at Pos
Author Message
Pherael Offline

Post: #1
Get slope at Pos
Hi, is there function that return terrain slope in given pos?
01-28-2015 12:06 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline

Post: #2
RE: Get slope at Pos

No, but you can calculate it manually using:
you can collect multiple samples around a Vec pos, and do calculations based on that.
01-28-2015 10:28 PM
Find all posts by this user Quote this message in a reply
georgatos7 Offline

Post: #3
RE: Get slope at Pos
If you can get the index of the triangle you want the slope of you might wanna try an altered version of this. I bet there will be a more optimized version for it code-wise but i can't get "base.tri.nrm(triangle_Index)" to work since it always crashes.

void Draw()
   VecI2 areaOfInterest = VecI2(1, 0);
      MeshBase base;
      base.createPhys(Game.World.areaActive(VecI2(1, 0)).data().mesh.meshes.data().setBase());      
      FREP(Game.World.areaActive(VecI2(1, 0)).data().mesh.tris())
         int triangle_Index = i; // The triangle we want to get the slope of.
         Vec posEdge1Vtx1 = base.vtx.pos(base.edge.ind(base.tri.adjEdge(triangle_Index).x).x);
         Vec posEdge1Vtx2 = base.vtx.pos(base.edge.ind(base.tri.adjEdge(triangle_Index).x).y);
         Vec posEdge2Vtx1 = base.vtx.pos(base.edge.ind(base.tri.adjEdge(triangle_Index).y).x);
         Vec posEdge2Vtx2 = base.vtx.pos(base.edge.ind(base.tri.adjEdge(triangle_Index).y).y);
         Vec triNRM = CrossN(posEdge2Vtx2-posEdge2Vtx1, posEdge1Vtx2-posEdge1Vtx1);
         flt xCentroid = (base.vtx.pos(base.tri.ind(triangle_Index).x).x + base.vtx.pos(base.tri.ind(triangle_Index).y).x + base.vtx.pos(base.tri.ind(triangle_Index).z).x)/3.0f;
         flt yCentroid = (base.vtx.pos(base.tri.ind(triangle_Index).x).y + base.vtx.pos(base.tri.ind(triangle_Index).y).y + base.vtx.pos(base.tri.ind(triangle_Index).z).y)/3.0f;
         flt zCentroid = (base.vtx.pos(base.tri.ind(triangle_Index).x).z + base.vtx.pos(base.tri.ind(triangle_Index).y).z + base.vtx.pos(base.tri.ind(triangle_Index).z).z)/3.0f;
         D.line(GREEN, Vec(xCentroid, yCentroid, zCentroid), Vec(xCentroid, yCentroid, zCentroid)+triNRM);
      FREP(Game.World.areaActive(VecI2(1, 0)).data().mesh.vtxs())
      D.line(RED, base.vtx.pos(i), base.vtx.pos(i)+base.vtx.nrm(i)/2.0f);

   D.text(0, 0.9, "GREEN: Triangle Normal");
   D.text(0, 0.8, "RED: Vertex Normal");
   D.text(0, 0.7, "Press ~ for Wireframe!");
   D.text(0, 0.6, S+"Active Areas: "+Game.World.areaActiveNum());      

This is drawing the Vertex Normals and the Triangle Normals (placed on the center of mass or centroid of the triangle).
If you wanna do this to apply texture by slope, the vertex normals will be enough.

[Image: Normals.png]
(This post was last modified: 01-29-2015 12:38 AM by georgatos7.)
01-29-2015 12:34 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline

Post: #4
RE: Get slope at Pos
There is a faster simpler way:

This is from the Editor Source
Vec HmNormal(C Heightmap &hm, flt area_size, C VecI2 &area_xy, C Vec2 &xz )
   flt fx=(xz.x/area_size-area_xy.x)*(hm.resolution()-1),

   Vec nrm;

   flt l, r;
   l=hm.heightI(fx-1, fy);
   r=hm.heightI(fx+1, fy);

   flt b, f;
   b=hm.heightI(fx, fy-1);
   f=hm.heightI(fx, fy+1);

   nrm.x*=hm.resolution(); nrm.z*=hm.resolution(); nrm.y=2; nrm.normalize();
   return nrm;
You may however need to get rid of 'resolution' 'area_size' or tweak the scale, because game data may be scaled already.
But this operates on unscaled editor data.

The idea is to get 4 samples of height (left/right/back/forward), and calculate the normal vector based on that.
01-29-2015 03:07 AM
Find all posts by this user Quote this message in a reply
Post Reply