Skip to main content
GameDev.net gamedev.net
🔒 Locked

Finding the height

Started by Aypok Jun 1, 2003 at 6:00 AM 5 replies 450+ views
Original Post
Aypok
Aypok
Hi, I''ve been playing around with the code from lesson 10 and trying new maps with it - nothing complex so far. I wrote a little app that creates a random 10x10 map made from triangles, and it gives each point a random height. I loaded it into my modified version of the lesson 10 code and tried adding some code to make the character (I converted it to a 3rd person engine) move up and down according to the height of the point (on the map) it was standing on. I tried for ages and ages (with 3 slightly different methods), but I couldn''t get it to work correctly. Could someone out there give me some help with this? Code, ideas, tips, links - anything would be appreciated. Thanks in advance. Aypok... -- "''''Bastardman!'''' The Chief cries as we slip into a parking space outside the station with the grace and ease of a car accident. Which in fact it was..." - BOFH
Aypok...-- "''Bastardman!'' The Chief cries as we slip into a parking space outside the station with the grace and ease of a car accident. Which in fact it was..." - BOFH
circuit
circuit
First you need to know, on witch triangle the character is standing on. When you know that, getting the height is easy. Use triangle - ray intersection algorithm or solve it from the plane equation.

For example:
If the triangle''s normal is "normal" and "x0, y0, z0" is a point on the triangle, and the character is standing on point "x, z" then the height "y" is...

y=(-normal.i*(x-x0)-normal.k*(z-z0))/normal.j+y0;

Aypok
Aypok
Hi,

Are "x0", "y0" and "z0" the first point of the triangle, or can they be any of its 3 points?

When you say the triangle''s "normal", do you mean (to quote the tutorial) "a line pointing straight out of the middle of a polygon at a 90 degree angle"? So, if the triangle''s normal was set up thus:

glNormal3f(0.0f, 1.0f, 0.0f);

Would this mean (in this example) that "normal.i" would be 0.0, "normal.k" would be 1.0, and "normal.j" would be 0.0?

Forgive me if these are stupid questions.

Thanks again.



Aypok...

--
"''''Bastardman!'''' The Chief cries as we slip into a parking space outside the station with the grace and ease of a car accident. Which in fact it was..." - BOFH
Aypok...-- "''Bastardman!'' The Chief cries as we slip into a parking space outside the station with the grace and ease of a car accident. Which in fact it was..." - BOFH
circuit
circuit
>Are "x0", "y0" and "z0" the first point of the triangle, or
>can they be any of its 3 points?

It can be ANY point that lies on the triangle. The first point would be a logical choice.

>When you say the triangle's "normal", do you mean
>(to quote the tutorial) "a line pointing straight
>out of the middle of a polygon at a 90 degree angle"?

Yes. However location of the normal does not make any diffense.
If you don't know the normal you can calculate it by using cross product.

>So, if the triangle's normal was set up thus:
>glNormal3f(0.0f, 1.0f, 0.0f);
>Would this mean (in this example) that "normal.i"
>would be 0.0, "normal.k" would be 1.0, and "normal.j"
>would be 0.0?

normal.i=0.0
normal.j=1.0
normal.k=0.0

I use i, j and k instead of x, y and z for vectors.



[edited by - circuit on June 1, 2003 12:18:38 PM]

[edited by - circuit on June 1, 2003 12:20:06 PM]
Aypok
Aypok
Hi,

I don't see how that formular could work - I've tried it too, and it didn't work (even with some alterations).

The main problem is a divide by zero; as (in my case) "normal.j" is 0.0, and divides get carried out before additions. Because adding 0 to "y0" makes no difference, I removed "normal.j" from the equation, yet it still didn't work. Including it in parenthesis, so that the addition is carried out before the division, doesn't make it to work either.

Another problem could be that "normal.i" is also 0.0, making the first part of the equation 0.

Are the first 2 parts of the equation, "-normal.i*(x-x0)" and "-normal.k*(z-z0)", supposed to be multiplied together, or is the latter meant to be subtracted from the former? I tried converting the equation to:

y = ((-normal.i * (x - x0)) * (-normal.k * (z - z0))) / (normal.j + y0)

But that didn't work either. What am I doing wrong?

Are there any alternative equations that I could use to get the same result?

Thanks for the help.

Aypok...

--
"''Bastardman!'' The Chief cries as we slip into a parking space outside the station with the grace and ease of a car accident. Which in fact it was..." - BOFH

[edited by - Aypok on June 2, 2003 3:07:41 AM]
Aypok...-- "''Bastardman!'' The Chief cries as we slip into a parking space outside the station with the grace and ease of a car accident. Which in fact it was..." - BOFH
circuit
circuit
>The main problem is a divide by zero; as (in my
>case) "normal.j" is 0.0, and divides get carried out before
>additions.

In my case the the y-axel was pointing up so the normal.j can NOT be 0. If it is the triangle would be vertical and the character can not stand on it.

>Another problem could be that "normal.i" is also 0.0, making
>the first part of the equation 0.

-normal.i*(x-x0)-normal.k*(z-z0) is 0 only if...

normal.i=0 and normal.k=0 then the triangle is horizontal and the height is same as y0.

x0=x and z0=z then the character is standing on the known point and the height is same as y0.

>What am I doing wrong?
You may be using different coordinate system than I am.
In my case the y-axel is pointing UP and the xz-plane is the horizontal plane. So vector |0 1 0| (i=0, j=1, k=0) would point up.

>Are there any alternative equations that I could use to get
>the same result?
Use the plane equation and solve the height your self.
lopatsch
lopatsch
to my mind it is a bit too complicated to calcutate the height by doing a ray-plane intersection. when you have a regular height grid you can just interpolate the height. fur further infos take a look at www.gametutorials.com, they have a series of heightmaping tuts.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.