"Hard" maths: angle between 2 triangles ??

Blitz3D Forums/Blitz3D Programming/"Hard" maths: angle between 2 triangles ??

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Hmm it works but the problem is it is always giving a positive angle because of Squareing and Squarerooting. I worked out a similar problem before so i will work this one out. It's just a matter of comparing signs of the vectors. Thanks Stevie!

Here's the code i have now(mess left over from previous tests):
Use WASDFC to move camera, and insert,home,pgup,pgdn,delete,end to move the 4th vertex.

Graphics3D 800,600,0,2



Global vectorx#
Global vectory#
Global vectorz#
Global vectorw#

Const UPS=60

cam=CreateCamera()
PositionEntity cam, 0,5,-15

l=CreateLight()

period=1000/UPS
time=MilliSecs()-period

v1=CreateSphere()
ScaleEntity v1,0.5,0.5,0.5
v2=CreateSphere()
ScaleEntity v2,0.5,0.5,0.5
v3=CreateSphere()
ScaleEntity v3,0.5,0.5,0.5
v4=CreateSphere()
ScaleEntity v4,0.5,0.5,0.5

cp1=CreateSphere();cross product marker
ScaleEntity cp1,0.5,0.5,0.5
EntityColor cp1, 0,0,250
cp2=CreateSphere()
ScaleEntity cp2,0.5,0.5,0.5
EntityColor cp2, 0,0,250


smesh = CreateMesh()
ssurf = CreateSurface(smesh)

v1x#=-6;left
v1y#=4
v1z#=2
v2x#=0;middle front
v2y#=1
v2z#=4
v3x#=0;middle back
v3y#=1
v3z#=0
v4x#=6;right
v4y#=1
v4z#=2

v1v = AddVertex (ssurf, v1x,v1y,v1z)
v2v = AddVertex (ssurf, v2x,v2y,v2z)
v3v = AddVertex (ssurf, v3x,v3y,v3z)
v4v = AddVertex (ssurf, v4x,v4y,v4z)

AddTriangle (ssurf, v1v,v2v,v3v)
AddTriangle (ssurf, v2v,v4v,v3v)

PositionEntity v1, v1x,v1y,v1z
PositionEntity v2, v2x,v2y,v2z
PositionEntity v3, v3x,v3y,v3z
PositionEntity v4, v4x,v4y,v4z

Repeat
	Repeat
		elapsed=MilliSecs()-time
	Until elapsed
	ticks=elapsed/period
	tween#=Float(elapsed Mod period)/Float(period)

msp# = 0.2

If KeyDown(17) Then MoveEntity cam, 0,0,msp
If KeyDown(31) Then MoveEntity cam, 0,0,-msp
If KeyDown(33) Then MoveEntity cam, 0,msp,0
If KeyDown(46) Then MoveEntity cam, 0,-msp,0
If KeyDown(32) Then MoveEntity cam, msp,0,0
If KeyDown(30) Then MoveEntity cam, -msp,0,0
If KeyDown (203) Then TurnEntity cam, 0,msp*5,0
If KeyDown (205) Then TurnEntity cam, 0,-msp*5,0
If KeyDown (200) Then TurnEntity cam, msp*5,0,0
If KeyDown (208) Then TurnEntity cam, -msp*5,0,0

If KeyDown (199) Then v4y = v4y + msp
If KeyDown (207) Then v4y = v4y - msp
If KeyDown (210) Then v4z = v4z + msp
If KeyDown (201) Then v4z = v4z - msp
If KeyDown (209) Then v4x = v4x + msp
If KeyDown (211) Then v4x = v4x - msp


;update verts
VertexCoords ssurf, v4v,v4x,v4y,v4z


;update spheres representing verts
PositionEntity v1, v1x,v1y,v1z
PositionEntity v2, v2x,v2y,v2z
PositionEntity v3, v3x,v3y,v3z
PositionEntity v4, v4x,v4y,v4z


ax#=v1x-v2x
ay#=v1y-v2y
az#=v1z-v2z
bx#=v1x-v3x
by#=v1y-v3y
bz#=v1z-v3z
CrossProduct(ax,ay,az,bx,by,bz)
t1vx#=vectorx
t1vy#=vectory
t1vz#=vectorz

ax#=v4x-v3x
ay#=v4y-v3y
az#=v4z-v3z
bx#=v4x-v2x
by#=v4y-v2y
bz#=v4z-v2z
CrossProduct(ax,ay,az,bx,by,bz)
t2vx#=vectorx
t2vy#=vectory
t2vz#=vectorz

px# = t1vx
py# = t1vy
pz# = t1vz
qx# = t2vx
qy# = t2vy
qz# = t2vz

MagP# = Sqr( px * px + py * py + pz * pz )
MagQ# = Sqr( qx * qx + qy * qy + qz * qz )

Theta# = ACos( ( px*qx + py*qy + pz*qz ) / ( MagP * MagQ ) )

wtf=False
If t1vx <= 0 And t2vx > 0 Then
wtf=True
ElseIf t1vx >= 0 And t2vx < 0 Then
wtf=True
ElseIf t1vy <= 0 And t2vy > 0 Then
wtf=True
ElseIf t1vy >= 0 And t2vy < 0 Then
wtf=True
ElseIf t1vz <= 0 And t2vz > 0 Then
wtf=True
ElseIf t1vz >= 0 And t2vz < 0 Then
wtf=True
EndIf

If Theta = NaN Then
	djhfsdf$="No Edge"
Else
	djhfsdf$="Edge"
End If

;update the cross product results
PositionEntity cp1, v1x+t1vx,v1y+t1vy,v1z+t1vz
PositionEntity cp2, v4x+t2vx,v4y+t2vy,v4z+t2vz

;show projected lines of cross product
CameraProject (cam, v1x,v1y,v1z)
p1x=ProjectedX()
p1y=ProjectedY()
CameraProject (cam, v1x+t1vx,v1y+t1vy,v1z+t1vz)
pt1x=ProjectedX()
pt1y=ProjectedY()
CameraProject (cam, v4x,v4y,v4z)
p2x=ProjectedX()
p2y=ProjectedY()
CameraProject (cam, v4x+t2vx,v4y+t2vy,v4z+t2vz)
pt2x=ProjectedX()
pt2y=ProjectedY()


	For k=1 To ticks
		time=time+period	
		If KeyHit(1) End
		UpdateWorld	
	Next
	
	RenderWorld tween
	
	Color 255,255,255
Text 10,10,	t1vx + ", " + t1vy + ", " + t1vz
Text 10,20,	t2vx + ", " + t2vy + ", " + t2vz
Text 10,30, Theta
Text 10,40, djhfsdf
Text 10,50, "wtf:" + wtf
	Color 255,00,0
;Line p1x,p1y,pt1x,pt1y
;Line p2x,p2y,pt2x,pt2ya
	Flip

Forever





Function TriangleNormal#(Ax#,Ay#,Az#,Bx#,By#,Bz#,Cx#,Cy#,Cz#)
SubVector Bx#,By#,Bz#,Ax#,Ay#,Az#
ux#=VectorX()
uy#=VectorY()
uz#=VectorZ()
SubVector Cx#,Cy#,Cz#,Bx#,By#,Bz#
vx#=VectorX()
vy#=VectorY()
vz#=VectorZ()
CrossProduct vx#,vy#,vz#,ux#,uy#,uz#
;Normalize vectorx,vectory,vectorz
Return Ax#*vectorx+Ay#*vectory+Az#*vectorz
End Function

Function VectorX#()
Return vectorx
End Function

Function VectorY#()
Return vectory
End Function

Function VectorZ#()
Return vectorz
End Function

Function VectorW#()
Return vectorw
End Function

Function SubVector(Ax#,Ay#,Az#,Bx#,By#,Bz#)
vectorx#=ax#-bx#
vectory#=ay#-by#
vectorz#=az#-bz#
End Function

Function Normalize(nx#,ny#,nz#)
If nx=0 And ny=0 And nz=0 Return
m#=Magnitude(nx#,ny#,nz#)
vectorx#=nx#/m#
vectory#=ny#/m#
vectorz#=nz#/m#
End Function

Function CrossProduct(Ax#,Ay#,Az#,Bx#,By#,Bz#)
vectorx#=Ay#*Bz#-Az#*By#
vectory#=Az#*Bx#-Ax#*Bz#
vectorz#=Ax#*By#-Ay#*Bx#
End Function


Function Magnitude(nx#,ny#,nz#) 
m#= Sqr( (nx*nx) + (ny*ny) + (nz*nz) ) 
Return m# 
End Function