Get mapheight not exact...

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Beam

Get mapheight not exact...

Post by Beam »

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
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

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.
Image
Beam

Post by Beam »

But would that be quick enough? I need to correct height-pos of all my units all the time, as they walk on sloping terrain pretty much all the time. Besides, I don't really know how to do that.
X_for_Extra
Posts: 69
Joined: Mon May 23, 2005 4:42 pm

Post by X_for_Extra »

Wild stab:

say X = 10.76

X = X - int(X);

X is now 0.76

H1 = height at X
H2 = height at X + 1

Final_Height = ((H1*(1-X)) + (H2*X));
Should put something witty here I suppose.
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

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 =

Code: Select all

y2 - y1
______
x2 - x1
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.

Code: Select all

10 = 0.285714 * 10 + b
10 = 2.85714 + b
b = 7.14286
So, our line for the x-axis is

Code: Select all

y = 0.285714 * x + 7.14286
So you want to get the y value at the midpoint of our 2 points, you do

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
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.
Image
Beam

Post by Beam »

Thanks!

I understand the maths there, but were in my image do I find the corresponding values to include in my function?

Thank you
Beam
Post Reply