Hi, this is my method to find height of my gamemap:
float world::getMapHeight(float posX){
video::SColorf c;
c= currentWorld->heightMap->getPixel(posX*5.0,64.0);
return c.b*50.0;
}
The map is viewed from the side and its a terrainnode derrived from a heightmap but used in a 2d-scroller view-game. So a unit can be placed at a correct height depending on its Xpos so it "stands on the ground". This works, but units walk very choppy since there is a distance between two "hills" in the heightmap-generated terrain. So if a unit is not standing exactly on a "hill" (where the line of the ground change "direction"), my function is not very accurate.
Any tips to make it better? I've been tipsed about the findGround function with some advanced 3Dfunctions to find height of terrain, but havent gotten it to work with what I need... That is give the coordinates on the map and get the height (or any of X,Y,Z which would be accuired in the same manner right?)
Maybe I explained this a bit over-complicated...
Thanks a bunch!
Beam
Get mapheight not exact...
You need to use a more accurate getheight function. Getting the height based on the pixel only gives you heights at whole numbers in the terrain data. If you want the height at say 3.5, 3.5, the traingle selector and getcollision functions will give you an intersection with the triangle at that point( which should actually be the average of the height values.
You could write your own function, which could calculate mid positions of a line, using slope formulas.
You could write your own function, which could calculate mid positions of a line, using slope formulas.

-
Beam
-
X_for_Extra
- Posts: 69
- Joined: Mon May 23, 2005 4:42 pm
y = mx + b
y is the y value.
x is the x value.
m is the slope of the line. ( the change in y coords between 2 points divided by the change in x coords between 2 points )
b is the y-intercept.
So, we have 2 points on the map,
p1 ( 3, 8 )
p2 ( 10, 10 )
The slope =
or in our case, 2/7 or 0.285714
The y-intercept can then be figured out by plugging in the slope, and the x and y from either of the points.
So, our line for the x-axis is
So you want to get the y value at the midpoint of our 2 points, you do
So, when X = 6.5, y = 9.
This is actually, very very fast math. You could look at how the getCollision functions get the precise y value, maybe there's a more efficient way to do it in 3D, i'm gonna look into it, as this example is for 2D.
y is the y value.
x is the x value.
m is the slope of the line. ( the change in y coords between 2 points divided by the change in x coords between 2 points )
b is the y-intercept.
So, we have 2 points on the map,
p1 ( 3, 8 )
p2 ( 10, 10 )
The slope =
Code: Select all
y2 - y1
______
x2 - x1
The y-intercept can then be figured out by plugging in the slope, and the x and y from either of the points.
Code: Select all
10 = 0.285714 * 10 + b
10 = 2.85714 + b
b = 7.14286
Code: Select all
y = 0.285714 * x + 7.14286
Code: Select all
y = 0.285714 * ( x1 + ( ( x2 - x1 ) * 0.5 ) ) + 7.14286
y = 0.285714 * ( 3 + ( ( 10 - 3 ) * 0.5 ) ) + 7.14286
y = 0.285714 * ( 3 + ( 7 * 0.5 ) ) + 7.14286
y = 0.285714 * ( 3 + ( 3.5 ) ) + 7.14286
y = 0.285714 * 6.5 + 7.14286
y = 9.000001
This is actually, very very fast math. You could look at how the getCollision functions get the precise y value, maybe there's a more efficient way to do it in 3D, i'm gonna look into it, as this example is for 2D.

-
Beam