Jump to content

Help with terrain following (Threejs)


Rhuno
 Share

Recommended Posts

Hi all,

 

I recently started playing around with threejs and am looking for suggestions on implementing terrain following. 

 

I have a simple demo here: http://rhuno.com/tests/webgl/height/

 

This works to a degree, but it isn't very smooth. The box just kind of pops to a new height. My current technique just combines triangles into quads, checks to see which quad the box/player is within and then sets the box's y position to the average y of the quad's vertices. The code is below.

for(var j = 0, len = geometry.faces.length; j < len; j+=2){    face1 = geometry.faces[j];    face2 = geometry.faces[j+1];    if( pos.x >= verts[face1.a].x && pos.x <= verts[face2.b].x)    {        if(pos.z >= verts[face1.a].z && pos.z <= verts[face2.b].z)	{            var avgY   = verts[face1.a].y + verts[face1.b].y + verts[face1.c].y;            avgY       += verts[face2.a].y + verts[face2.b].y + verts[face2.c].y;            targetY    = avgY * 0.166 + 4.5;				        }	    }}// increase y positionif(targetY != undefined && Math.abs(targetY - pos.y) > 0.5){        if(pos.y < targetY)        pos.y += 0.5;    else    if(pos.y > targetY)        pos.y -= 0.5;}    

I was just wondering if anyone had any tips or good resources for a better implementation and smoother results.

 

Cheers.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...