#### Archived

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

# Height in a triangle

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

## Recommended Posts

hi, say I have a landscape in 3D, made up of triangles... Each triangle is at a regular interval (2 make up a square, 500x500 squares), and each corner/vertex has a height (0 to 255). Say I''m standing/camera is in the middle of a triangle, how high up would the camera/point be? I have the values at integer coordinates, but not at decimal coordinates... I really really need some help with this, two maths teachers haven''t been able to get it yet, but I see it in every game that exists (make sure the player is on the correct level/height)... even Direct3D lighting must use a similiar function (blending colours across a triangle)... please can someone help Jack

##### Share on other sites
Find the normal (a,b,c) of the triangle using the cross product, take any vertex (x,y,z) of the triangle and calculate d = -(ax+by+cz).

Now the equation ax+by+cz+d=0 is true for any point (x,y,z) which lies in the triangle''s plane.

If you know the (x,z) of your camera then you can work out your height (y) at that point.

y = -(ax+cz+d/b)

This should work aslong as the y component (b) of the normal is non-zero, i.e. the triangle is not vertical.

Andy.

##### Share on other sites
thanks very much;

I think I follow it all - I''ll play around till I do

Jack;

##### Share on other sites
This question comes up so much there should be a FAQ (no offense.

I really suggest you check out this site: http://www.math.hmc.edu/calculus/tutorials/

Many of the vector topics will be extrememly useful if you get into other areas of 3D programming; like BSP trees or collision detection.

##### Share on other sites
I''ve been doing stuff like this for quite a while, 3D isn''t that new to me...

but I''ll have a look at that site....

And the idea previously suggested doesn''t seem to work, I also read the thread on "Triangle Interpolation" and none of the ideas there seem to work for me; they either work slightly, or not at all - in the case of the one above everything goes white (when using a 0-255 height scale and representing it as a greyscale image)...

any ideas? or am I just not very good at this?

Jack;

##### Share on other sites
Step through your code. It''s possible that a correct formula isn''t working for another reason. Calculus has been around for a while, so I don''t think you''ll find too many bugs in it

##### Share on other sites
hehe, I doubt I''d be popular in the mathematics community if I found a bug with calculus

either way, I''ve tried 4 different methods for finding the height and all of them have bugs; They are all almost identical copies of the posted/written up method (I had to change them so I could test it in VB first), for example, one method works fine, but if you set the 0 vertex to higher than all the others it overpowers them and they all become that value ???

any further ideas? I will help you with anything you may need help with/shower you with praise if you can solve this - it''s driving me up the wall

Jack;

##### Share on other sites
"either way, I've tried 4 different methods for finding the height and all of them have bugs;"

They are most likely not bugs. The reason they may not work for you is because you haven't adapted them to your own code.

"They are all almost identical copies of the posted/written up method (I had to change them so I could test it in VB first), for example, one method works fine, but if you set the 0 vertex to higher than all the others it overpowers them and they all become that value ???"

This sounds like a good reason to step through your code with a debugger and monitor the formula as it works. However, if you don't understand the formula, it will still be confusing

"any further ideas? I will help you with anything you may need help with/shower you with praise if you can solve this - it's driving me up the wall"

Honestly, this is not a hard problem. But I don't think you understand why the solution works. First you need to understand that the point in plane formula works with a displacement vector. If you leave out that small fact your result will be buggered as you move away from the origin. So understand that the formula ax+by+cz = d is actually a simplification of the formula a(x-x0)+ b(y-y0)+c(z-z0) = 0. Note the displacement values!

All you need is a) the displacement value (D or x,y,z), the normal, and the X,Z point on the plane. The rest is just algebra.

Edited by - Sydan on February 16, 2001 1:54:23 AM

##### Share on other sites
thanks,

I''m doing advanced maths 1st year at the moment - I aint a god at maths (although I did correct our teacher on the use of Dijkstra''s algorithm)...

Jack;

1. 1
Rutin
69
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633421
• Total Posts
3011797
• ### Who's Online (See full list)

There are no registered users currently online

×