# infinite heightmap tricks

a guest
Dec 5th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1.
2. //  hmap.halfww    is    world width / 2
3. //  hmap.ww        is    world width
4.
5. // get the wrapped coordinates  relative to   ax, az  (and store them in wp)
6. void setWrapPos(float ax,  float az,
7.                 float bx, float by, float bz,
8.                 gamex::xVec3f & wp)
9. {
10.   wp.x = bx; wp.z = bz; wp.y = by;
11.
12.   if ( (bx - ax) > hmap.halfww ) { wp.x -= hmap.ww;}
13.   else if ( (bx - ax) < -hmap.halfww ) { wp.x += hmap.ww;}
14.
15.   if ( (bz - az) > hmap.halfwh ) { wp.z -= hmap.wh;}
16.   else if ( (bz - az) < -hmap.halfwh ) { wp.z += hmap.wh;}
17.
18. }//setwrappos
19.
20.
21. //check if the coordinates need to be wrapped around
22. void checkWrapPos(float & ax, float & az)
23. {
24.  if (ax < 0.0f )    { ax += hmap.ww; }
25.  else if (ax > hmap.ww ) { ax -= hmap.ww; }
26.  if (az < 0.0f )    { az += hmap.wh; }
27.  else if (az > hmap.wh ) { az -= hmap.wh; }
28.
29. }//checkwrap
30.
31.
32. // mw   is terrain  width in number of vertices
33. // needs to be power of 2 ( 2, 4, 8, 16, 32 ..  256  etc)  for bitwise-AND  trick to work
34.
35. float
36. xHeightMap::getHeightAt(int ax, int az)
37. {
38.   ax &= (mw-1); //32 mw  -> 31
39.   az &= (mh-1);
40.   return vecHeight[ax+(az*mw)];
41. }//getheightat
42.
43. void
44. xHeightMap::setHeightAt(int ax, int az, float h)
45. {
46.   ax &= (mw-1); //32 mw  -> 31
47.   az &= (mh-1);
48.   vecHeight[ax+(az*mw)] = h;
49. }//getheightat
50.
51.
52.
53.
54.