I have been slowly figuring out the math behind triangle and line intersection. Finally, I have reached a point where I can raytrace a point onto a plane. My problem is that the plane isn't the one I want; It's a parallel plain offset by the second point vector. I'm not all that sure of what I'm doing. Help would be incredibly appreciated!
Here's my code:
Rem Project: Pick Triangle
Rem ***** Main Source File *****
set display mode desktop width(),desktop height(),16
sw=screen width()
sh=screen height()
autocam off
V1=1
V2=2
V3=3
P=4
V=13
Dif=14
Dif2=15
null=make vector3(V1)
null=make vector3(V2)
null=make vector3(V3)
Null=make vector3(P)
Null=make vector3(V)
Null=make vector3(Dif)
Null=make vector3(Dif2)
Av=5
Bv=6
Cv=7
PAv=8
PBv=9
PCv=10
Cam=11
Norm=12
Hold1=13
Hold2=14
null=make vector3(Av)
null=make vector3(Bv)
null=make vector3(Cv)
null=make vector3(PAv)
null=make vector3(PBv)
null=make vector3(PCv)
null=make vector3(Hold1)
null=make vector3(Hol2)
null=make vector3(LNorm)
null=make vector3(Cam)
null=make vector3(Norm)
rem offset second point vector
x#=1
y#=1
z#=1
rem asign triangle verteces
set vector3 V1,-20,-20,-20
set vector3 V2,0,20,0
set vector3 V3,20,0,-20
make object triangle 1,x vector3(V1),y vector3(V1),z vector3(V1),x vector3(V2),y vector3(V2),z vector3(V2),x vector3(V3),y vector3(V3),z vector3(V3)
set object cull 1,0
for v=2 to 4
make object sphere v,4
MyColorObj(v,RGB(0,0,255))
next v
make object sphere 5,4
MyColorObj(5,RGB(255,128,0))
make object sphere 7,7
MyColorObj(7,RGB(255,0,0))
position object 2,x vector3(V1),y vector3(V1),z vector3(V1)
position object 3,x vector3(V2),y vector3(V2),z vector3(V2)
position object 4,x vector3(V3),y vector3(V3),z vector3(V3)
Rem Plane Normal
subtract vector3 Dif,V3,V2
subtract vector3 Dif2,V2,V1
Cross product vector3 Norm,Dif,Dif2
Normalize vector3 Norm,Norm
sync on
sync rate 30
color backdrop RGB(0,0,0)
rem point P Speed
speed#=1
do
rem prepare to rotate camera
ay#=Wrapvalue(camera angle y()+mousemovex())
ax#=Wrapvalue(camera angle x()+mousemovey())
rem camera
if lock=0 then rotate camera ax#,ay#,0
position camera 0,0,0
move camera -100
Rem Verteces
x1#=x vector3(V1)
y1#=y vector3(V1)
z1#=z vector3(V1)
x2#=x vector3(V2)
y2#=y vector3(V2)
z2#=z vector3(V2)
x3#=x vector3(V3)
y3#=y vector3(V3)
z3#=z vector3(V3)
rem Vectors for Heron's formula
subtract vector3 Av,V2,V1
A=length vector3(Av)
subtract vector3 Bv,V3,V2
B=length vector3(Bv)
subtract vector3 Cv,V1,V3
C=length vector3(Cv)
rem distance calculations
subtract vector3 PBv,P,V2
PB=length vector3(PBv)
subtract vector3 PAv,P,V1
PA=length vector3(PAv)
subtract vector3 PCv,P,V3
PC=length vector3(PCv)
rem Heron's Formula in use
S=(A+B+C)/2.0
ABC# = sqrt(s*(s-a)*(s-b)*(s-c))
S=(A+PB+PA)/2.0
ABP# = sqrt(s*(s-a)*(s-pb)*(s-pa))
S=(C+PC+PA)/2.0
APC# = sqrt(s*(s-c)*(s-pc)*(s-pa))
rem Decide Plane Collision
set vector3 to camera position Cam,0
DP1#=dot product vector3(Norm,Cam)
DP2#=dot product vector3(Norm,P)
PCollision#=DP1#*DP2#
if PCollision#=>0
Print "No Plain Collision found with raytrace"
else
Print "Plain Collision found with raytrace"
`T={D + Aa + Bb + Cc}/{Ak + Bl + Cm}
rem get line normals
subtract vector3 LNorm,Cam,P
normalize vector3 LNorm,LNorm
rem Find Distance
d#=x vector3(A1)*x vector3(Norm)-y vector3(A1)*y vector3(Norm)-z vector3(A1)*z vector3(Norm)
dis#=(d# + x vector3(Norm)*x vector3(P) + y vector3(Norm)*y vector3(P) + z vector3(Norm)*z vector3(P))/(x vector3(Norm)*x vector3(LNorm)+y vector3(Norm)*y vector3(LNorm)+z vector3(Norm)*z vector3(LNorm))
rem add to line formula
scale vector3 V,LNorm,-dis#
Add vector3 V,P,V
VX#=x vector3(V)
VY#=y vector3(V)
VZ#=z vector3(V)
endif
rem Print Information
Print "Ray Trace Point Dot prod. :"+STR$(PCollision#)
Print "DP1# (Normal&Camera) "+STR$(DP1#)
Print "DP2# (Normal&Ref.Point)"+STR$(DP2#)
Print "Normal X"+STR$(x vector3(Norm))+" Y "+STR$(y vector3(Norm))+" Z "+STR$(z vector3(Norm))
Print "Intersection Vector X"+STR$(x vector3(V))+" Y "+STR$(y vector3(V))+" Z "+STR$(z vector3(V))
Print "PVector X"+STR$(x vector3(P))+" Y "+STR$(y vector3(P))+" Z "+STR$(z vector3(P))
Print "Hold3#" ;hold3#
Print "The distance read is ",dis#,"."
rem position sphere at vertex
position object 7,VX#,VY#,VZ#
rem Barycentric Coordinates
G#=ABP#/ABC#
F#=APC#/ABC#
if (f#>=0.0 And g#>=0.0 And (1.0-f#-g#)>=0.0) then Print "Point is inside triangle"
set cursor 0,0
Print "F# :"+STR$(F#)
Print "G# :"+STR$(G#)
rem mouse clicking
if mouseclick()=1 then inc mc else mc=0
if mouseclick()=2 then inc rc else rc=0
if rc=1 then inc lock : if lock>1 then lock=0
rem display camera lock toggle
if lock=1
Center text sw/2,sh-20,"Right click to unlock"
else
Center text sw/2,sh-20,"Right click to lock"
endif
rem move point P
if keystate(17)=1
x#=newxvalue(x#,ay#,speed#)
z#=newzvalue(z#,ay#,speed#)
endif
if keystate(30)=1
x#=newxvalue(x#,Wrapvalue(ay#-90),speed#)
z#=newzvalue(z#,Wrapvalue(ay#-90),speed#)
endif
if keystate(31)=1
x#=newxvalue(x#,Wrapvalue(ay#-180),speed#)
z#=newzvalue(z#,Wrapvalue(ay#-180),speed#)
endif
if keystate(32)=1
x#=newxvalue(x#,Wrapvalue(ay#+90),speed#)
z#=newzvalue(z#,Wrapvalue(ay#+90),speed#)
endif
rem finish Point P
position object 5,x vector3(P),y vector3(P),z vector3(P)
set vector3 P,x#,y#,z#
sync
loop
Rem Heron's_Formula
`Where A,B,&C are the 3 lengths of the triangle
`And s = (a+b+c)/2
`Heron's formula states:
`Area = sqrt(s*(s-a)*(s-b)*(s-c))
Function MyColorObj(Obj,Color)
set object diffuse Obj,Color
set object Ambient Obj,Color
set object emissive Obj,Color
set object specular Obj,RGB(255,255,255)
set object specular power Obj,4
endfunction
Randomnosity is a special influence upon man kind allowing one to think outside the usual bouderies and into a fantastic world created by oneself. Elephants don't cut it