Advertisement

Blast...triangle interpolation...

Started by April 18, 2002 02:46 PM
5 comments, last by pollier 22 years, 10 months ago
I''ve tried this with several different implementations and it hasn''t worked for any of them, at least the way I coded them. I''ve got a 3D triangle points (CVector3s) a, b, and c, and the X,Z coordinates of a point on the triangle. How do I get the Y co-ordinate of the point? I''ve searched around, both on Google and this site, but every time I''ve tried to implement it, it didn''t work. eg. float PointOnTriangleY(CVector3 a, CVector3 b, CVector3 c, float pX, float pY) { //huh? } Thanks!
"No break 'till you die!" - Mr. Foster, my English teacher


1)Find the plane equation of the triangle (say your Vectors are P1, P2, P3)

a) Find the vectors of two sides of the triangle that share a common point, eg the vectors between P1P2 and P1P3

b) Get the normal of the triangle plane by taking the cross product of the vectors P1P2 and P1P3.

N=P1P2 X P1P3

c) Normalise this normal.

d) Equation of a plane is aX+bY+cZ=d,

Where (a,b,c)is your plane Normal vector calulated above
Use this and a point on your triangle P1=(X1,Y1,Z1) to find d

ie d= aX1+bY1+xZ1

2) substitute your X,Z coordinates into the plane equation above to get Y

ie Y = (d - aX -cZ)/b


Cheers

Keef
-----------------------Current Project: The Chromatic Game Engine
Advertisement
Thanks, that''s one way

Is it possible you could write it in code? I tried using this method before, and my code was horribly unsuccessful...

Thanks again!
"No break 'till you die!" - Mr. Foster, my English teacher
I usually fudge it somewhere around the cross-products.
(Sad, isn''t it?)
"No break 'till you die!" - Mr. Foster, my English teacher
Hmm, tried it. Got wacky results

Here''s my code:
float PointOnTriangle(CVector3 a, CVector3 b, CVector3 c, float Cx, float Cz){	CVector3 nVect = vNormalize(Cross(VectorDiff(a,b),VectorDiff(a,c)));	float d = (nVect.x * a.x) + (nVect.y * a.y) + (nVect.z + a.z);	return((d - (nVect.x * Cx) - (nVect.z * Cz)) / nVect.y);} 


Anything wrong?
"No break 'till you die!" - Mr. Foster, my English teacher


1)
This bit
(nVect.x * a.x) + (nVect.y * a.y) + (nVect.z + a.z);

should I think be
(nVect.x * a.x) + (nVect.y * a.y) + (nVect.z * a.z);

(you are adding the z component, not multiplying!)

Your code is nice and concise but hard to debug. Try to spread things out a bit to give yourself a chance of debugging. Cramped code is hard to debug! Once you''ve got it working then optimise.

2) Try drawing the normal for the triangle when you render the triangle. I found this very useful for debugging all sorts of stuff like this (you''ll probably need to scale up the normal then draw it as a line from point on you triangle)


Cheers

Keef
-----------------------Current Project: The Chromatic Game Engine
Advertisement
Woo hoo! It works! Thank you very much! I squished my code together for the post, I suppose that''s a bad thing

This topic is closed to new replies.

Advertisement