I've recently had problems making a function which figures out the height of a point on a 3D triangle from its three vertices' height values (
talking about a surface element, not a triangle rotated at any angle!), and the X&Z position of the object i want to place onto the triangle (ground...)
Couldn't find a place anywhere which would have told me straight forward what to do, and rarely, what tried to tell me gave me tons of equations with a dozen unexplained variables... so i came up with my own solution and here it is:
(important detail: the triangle must have the same lenghts on all three sides, at least it was built for that)
The triangle's three verts are:
vertAx#, vertAy#, vertAz#
vertBx#, vertBy#, vertBz#
vertCx#, vertCy#, vertCz#
The point's coordinates where we want to get the height of the tri:
edTERRAIN3dmouseX#, edTERRAIN3dmouseZ#
We get the height in this variable:
HeightAt_ABC#
The code:
triangleHeight# = abs(vertCz# - vertAz#)
if vertCz# < vertAz#
Cangle# = atanfull( edTERRAIN3dmouseX#-vertCx# , edTERRAIN3dmouseZ#-vertCz# ) : AB_Pos# = vertAx# - (vertAx#-vertBx#)*0.5 + tan(Cangle#)*triangleHeight# : HeightAt_AB# = vertAy# - ( vertAy#-vertBy# ) * ((vertAx#-AB_Pos#) / (vertAx# - vertBx#))
else
Cangle# = atanfull( vertCx#-edTERRAIN3dmouseX# , vertCz#-edTERRAIN3dmouseZ# ) : AB_Pos# = vertAx# - (vertAx#-vertBx#)*0.5 - tan(Cangle#)*triangleHeight# : HeightAt_AB# = vertAy# - ( vertAy#-vertBy# ) * ((vertAx#-AB_Pos#) / (vertAx# - vertBx#))
endif
MaxDa# = abs(AB_Pos#-vertCx#) : MaxDb# = abs(vertAz#-vertCz#) : MaxDistFrom_AB# = sqrt( MaxDa#*MaxDa# + MaxDb#*MaxDb# ) : CurrDa# = abs(edTERRAIN3dmouseX#-vertCx#) : CurrDb# = abs(edTERRAIN3dmouseZ#-vertCz#) : CurrentDistFrom_AB# = sqrt( CurrDa#*CurrDa# + CurrDb#*CurrDb# ) : HeightAt_ABC# = vertCy# + (CurrentDistFrom_AB# / MaxDistFrom_AB#) * (HeightAt_AB# - vertCy#)
Good for not relying on any collision methods.
-In.Dev.X: A unique heavy story based shoot'em ~35%
-CoreFleet: An underground commander unit based RTS ~15%
-TailsVSEggman: An Sonic themed RTS under development for idea presentation to Sega ~15%