• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Terrain height

This topic is 6583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I made a terrain in OpenGL loaded from a bitmap, and now I would like to have a character walk on it. The question is, how could I calculate the character''s y coordinate? If it''s standing on a vertex, then it''s easy, but what about when it''s standing between vertexes with different heights? Also, could you give me it in C code? Thanks.

##### Share on other sites
a planes equation is Ax + By + Cz + D = 0

u know the planes normal and the x and z position.
y is then easy to work out.

http://members.xoom.com/myBollux

##### Share on other sites
I''m a newbie, so correct me if I''m wrong. I think this has to do with collision detection. You have to define a bounding box for your character and then calculate if it is not in the terrain. If it isn''t, let it be affected by gravity. To get more info about that you should search gamedev for more info on collision detection - I found lotsa interesting stuff about that topic..

##### Share on other sites
What you''re talking about is ''interpolation''. Interpolation is the process of determining a point between two points.

There are various forms of interpolation. There is linear interpolation:

function Linear_Interpolate(a, b, x)
return a*(1-x) + b*x
end of function

Where x is a float value between 0 and 1. If x is 0, then it will return a, if x is 1 then it will return b. If x is 0.5 it will return a value halfway between a and b.. get the idea?

However linear interpolation looks ugly in a terrain sense because the points of hills are not joined by straight lines, they are curved. The solution to this is either cosine interpolation (using the cosine function) or a cubic interpolation routine (bit more cpu hungry i''ve found).

Here is the code (pseudo.. choose your language )for a cosinal interpolation algorithm:

function Cosine_Interpolate(a, b, x)
ft = x * 3.1415927
f = (1 - cos(ft)) * .5

return a*(1-f) + b*f
end of function

Again, where ''a'' and ''b'' are your points and x is the distance along between those points between 0 and 1. This will give a nice curved line between the two points.

Good luck with it all . Remember, since you''re working in a 3D environment, you''ll have to do 3 sets of interpolation. Just say you have a square of points:

P1 P2

P3 P4

Each of these points has a height. You''ll need to interpolate between P1 and P2, then between P3 and P4, and then between those two points.

Hopefully this is enough to at least get you started, if not, just have a search around for ''interpolation'' and see if you can get a better description than mine

Have fun, happy coding!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015493
×