Post by DJLinux on May 12, 2009 20:25:29 GMT -5
last days are saw the questions how to get y collision detection from terrain ground
normaly you can detect from your players position x,z the terrain triangle under it.
now you need the y coord from the player position over the triangle
p0#(),p1#(), and p2#() are the 3 points of the triangle (under the player)
q#(0) are the player x position
q#(1) are the player y position unknow at the moment
q#(2) are the player z position
here are one slow way:
here are the second faster way ;D
and here you can compare the results of GetY and GetY2

Joshy
normaly you can detect from your players position x,z the terrain triangle under it.
now you need the y coord from the player position over the triangle
p0#(),p1#(), and p2#() are the 3 points of the triangle (under the player)
q#(0) are the player x position
q#(1) are the player y position unknow at the moment
q#(2) are the player z position
here are one slow way:
function GetY2#(p0#(),p1#(),p2#(),Q#())
' calculate plane equation: Ax+By+Cz+D=0
dim A# = p0#(1) *(p1#(2) - p2#(2)) + p1#(1) *(p2#(2) - p0#(2)) + p2#(1) *(p0#(2) - p1#(2))
dim B# = p0#(2) *(p1#(0) - p2#(0)) + p1#(2) *(p2#(0) - p0#(0)) + p2#(2) *(p0#(0) - p1#(0))
dim C# = p0#(0) *(p1#(1) - p2#(1)) + p1#(0) *(p2#(1) - p0#(1)) + p2#(0) *(p0#(1) - p1#(1))
dim D# = -(p0#(0) *(p1#(1) * p2#(2) - p2#(1) * p1#(2)) + p1#(0) *(p2#(1)* p0#(2) - p0#(1)* p2#(2)) + p2#(0) *(p0#(1) *p1#(2) - p1#(1) *p0#(2)))
if B#=0.0 then B#=0.1:end if
' do the intersection with the plane
return -(D#+A#*Q#(0)+C#*Q#(2))/B#
end function
here are the second faster way ;D
function GetY#(p0#(),p1#(),p2#(),Q#())
' calculate plane normale
dim N#(2)=Normalize(CrossProduct(p1#-p0#,p2#-p0#))
dim y# = -N#(0)*Q#(0) - N#(2)*Q#(2) + (N#*p0#)
if N#(1)=0.0 then N#(1)=0.1:end if
return y#/N#(1)
end function
and here you can compare the results of GetY and GetY2
function GetY#(p0#(),p1#(),p2#(),Q#())Happy cooding
' calculate plane normale
dim N#(2)=Normalize(CrossProduct(p1#-p0#,p2#-p0#))
dim y# = -N#(0)*Q#(0) - N#(2)*Q#(2) + (N#*p0#)
if N#(1)=0.0 then N#(1)=0.1:end if
return y#/N#(1)
end function
function GetY2#(p0#(),p1#(),p2#(),Q#())
' calculate plane equation: Ax+By+Cz+D=0
dim A# = p0#(1) *(p1#(2) - p2#(2)) + p1#(1) *(p2#(2) - p0#(2)) + p2#(1) *(p0#(2) - p1#(2))
dim B# = p0#(2) *(p1#(0) - p2#(0)) + p1#(2) *(p2#(0) - p0#(0)) + p2#(2) *(p0#(0) - p1#(0))
dim C# = p0#(0) *(p1#(1) - p2#(1)) + p1#(0) *(p2#(1) - p0#(1)) + p2#(0) *(p0#(1) - p1#(1))
dim D# = -(p0#(0) *(p1#(1) * p2#(2) - p2#(1) * p1#(2)) + p1#(0) *(p2#(1)* p0#(2) - p0#(1)* p2#(2)) + p2#(0) *(p0#(1) *p1#(2) - p1#(1) *p0#(2)))
if B#=0.0 then B#=0.1:end if
' do the intersection with the plane
return -(D#+A#*Q#(0)+C#*Q#(2))/B#
end function
dim a#(2),b#(2),c#(2)
dim player#(2)
' x, y, z
data -5,0, 5
data 5,1,-5
data -5,0,-5
read a#(0),a#(1),a#(2)
read b#(0),b#(1),b#(2)
read c#(0),c#(1),c#(2)
player#(0) = -5 ' x position
player#(2) = 5 ' z position
printr "GetY#=" + GetY#(a#,b#,c#,player#) + ", GetY2#=" + GetY2#(a#,b#,c#,player#)
player#(0) = 3 ' x position
player#(2) = 2 ' z position
printr "GetY#=" + GetY#(a#,b#,c#,player#) + ", GetY2#=" + GetY2#(a#,b#,c#,player#)
player#(0) = 5 ' x position
player#(2) = -3 ' z position
printr "GetY#=" + GetY#(a#,b#,c#,player#) + ", GetY2#=" + GetY2#(a#,b#,c#,player#)

Joshy