REM ***********************************************
REM Title: Mouse to Random Matrix
REM Author: Dmitry
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
`*******************************************************************************
`*                    By: DMiTR0S  dmitr0s@newmail.ru                        *
`*******************************************************************************
 
`*******************************************************************************
`*                            Free License Agreement.                          *
`*                                                                            *
`*  The user/developer agrees to include a visible credit to "Dmitri Kuschev" *
`*  within your programs documentation and or web site.                      *
`*                                                                            *
`*  The user/developer agrees to NOT offer the this source code for Sale.    *
`*                                                                            *
`*  Providing the user/Developer agrees in full to the previous Free          *
`*  License Agreement clauses, the user/developer may use this source        *
`*  code FREELY in all their products, commercial or otherwise.              *
`*                                                                            *
`*******************************************************************************
Set Display Mode 1024, 768, 32
 
Autocam off
Randomize Timer()
 
Global Distance# As Float
Global IntersectionPointX# As Float
Global IntersectionPointY# As Float
Global IntersectionPointZ# As Float
 
MoveVector=1
MatrixClone=1
Matrix=1
Step#=5.0
width=500*2.0
depth=500*2.0
xTiles=30
zTiles=30
r=Make Vector3(MoveVector)
 
Make Matrix Matrix ,width, depth, xTiles, zTiles
Randomize Matrix Matrix, 50
Update Matrix Matrix
Position Matrix Matrix, RND(10), RND(10), RND(10)
 
Position Camera width/2, 200.0, -500.0
 
MakeObjectFromMatrix(Matrix, MatrixClone, xTiles, zTiles, width/(xTiles*1.0), depth/(zTiles*1.0), xTiles+1)
 
Make Object Sphere 10, 5.0
Color Object 10, RGB(0,255,0)
 
Make Object Sphere 11, 10.0
Color Object 11, RGB(255,0,0)
Position Object 11, Matrix Position X(Matrix)+RND(width), 0.0, Matrix Position Z(Matrix)+RND(height)
 
Sync Rate 0
Sync On
Do
  Gosub Camera
  Gosub NewDestinationPoint
  Gosub MoveSphere
  Gosub PrintInfo
 
  Sync
Loop
 
NewDestinationPoint:
  GetMatrixCloneIntersection(MatrixClone)
  If GetDistanceToObject()>0.0
      Show Object 10
      Position Object 10, GetIntersectionPointX(), GetIntersectionPointY(), GetIntersectionPointZ()
  EndIf
Return
 
MoveSphere:
  If UpKey()
      Set Vector3 MoveVector, GetIntersectionPointX()-Object Position X(11), GetIntersectionPointY()-Object Position Y(11), GetIntersectionPointZ()-Object Position Z(11)
      If Length Vector3(MoveVector) > Step#
        Normalize Vector3 MoveVector, MoveVector
        Multiply Vector3 MoveVector, Step#
        NewX#=Object Position X(11)+X Vector3(MoveVector)
        NewZ#=Object Position Z(11)+Z Vector3(MoveVector)
        NewY#=Matrix Position Y(Matrix)+Get Ground Height(Matrix, NewX#-Matrix Position X(Matrix), NewZ#-Matrix Position Z(Matrix))
        Position Object 11, NewX#, NewY#, NewZ#
      Else
        Position Object 11, GetIntersectionPointX(), GetIntersectionPointY(), GetIntersectionPointZ()
      EndIf
  EndIf
Return
 
Camera:
  If MouseClick()=1
      cay#=cay#+(Mousemovex()/10.0)
      cax#=cax#+(Mousemovey()/10.0)
      If cax#<-90.0 Then cax#=-90.0
      If cax#>90.0 Then cax#=90.0
      ax#=Curveangle(cax#,ax#,2.0)
      ay#=Curveangle(cay#,ay#,2.0)
      Rotate Camera Wrapvalue(ax#*-1),Wrapvalue(ay#),Wrapvalue(caz)
  EndIf
 
  If Mouseclick()=2
      cy#=cy#+MousemoveY()
      Move Camera MousemoveX()
      Position Camera Camera Position X(), cy#, Camera Position Z()
  EndIf
return
 
PrintInfo:
  Text 10, 10, "Code by: Dmitri Kuschev"
  Text 10, 30, "E-mail: dmitr0s@newmail.ru"
  Text 10, 50, "FPS "+Str$(Screen FPS())
  Text 10, 70, "Press Up to move the red sphere towards the destination point"
  Text 10, 90, "Step: "+Str$(Step#)
  Text 10, 110, "Distance from the camera to the destination point: "+Str$(GetDistanceToObject())
  Text 10, 130, "Position of the red sphere on the matrix in world coordinates:"
  Text 10, 150, "X = "+Str$(Object Position X(11))
  Text 10, 170, "Y = "+Str$(Object Position Y(11))
  Text 10, 190, "Z = "+Str$(Object Position Z(11))
  Text 10, 210, "Position of the red sphere on the matrix in matrix coordinates:"
  Text 10, 230, "X = "+Str$(Object Position X(11)-Matrix Position X(1))
  Text 10, 250, "Y = "+Str$(Object Position Y(11)-Matrix Position Y(1))
  Text 10, 270, "Get Ground Height( Matrix, X, Z ) = "+Str$(Get Ground Height(Matrix, Object Position X(11)-Matrix Position X(1), Object Position Z(11)-Matrix Position Z(1)))
  Text 10, 290, "Z = "+Str$(Object Position Z(11)-Matrix Position Z(1))
  Text 10, 310, "Destination point in world coordinates:"
  Text 10, 330, "GetIntersectionPointX() X = "+Str$(GetIntersectionPointX())
  Text 10, 350, "GetIntersectionPointY() Y = "+Str$(GetIntersectionPointY())
  Text 10, 370, "GetIntersectionPointZ() Z = "+Str$(GetIntersectionPointZ())
  Text 10, 390, "Destination point in matrix coordinates:"
  Text 10, 410, "X = "+Str$(GetIntersectionPointX()-Matrix Position X(1))
  Text 10, 430, "Y = "+Str$(GetIntersectionPointY()-Matrix Position Y(1))
  Text 10, 450, "Z = "+Str$(GetIntersectionPointZ()-Matrix Position Z(1))
Return
 
 
Function MakeObjectFromMatrix(Matrix, Object, ,MXSegments, MZSegments, MXSegmentSize#, MZSegmentSize#, VIR)
  VerticlesCount=(MXSegments)*(MZSegments)*6
  FVFFormat=338
  FVFSize=36
  If Memblock Exist(1) Then Delete Memblock 1
  Make Memblock 1, 12+VerticlesCount*FVFSize
  Write Memblock Dword 1, 0, FVFFormat
  Write Memblock Dword 1, 4, FVFSize
  Write Memblock Dword 1, 8, VerticlesCount
  For SX=1 to MXSegments
      For SZ=1 To MZSegments
        Num=MXSegments*(SZ-1)+SX
        Poly1X1#=Matrix Position X(Matrix)+(SX-1)*MXSegmentSize#
        Poly1Y1#=Matrix Position Y(Matrix)+Get Matrix Height(1, SX-1, SZ-1)
        Poly1Z1#=Matrix Position Z(Matrix)+(SZ-1)*MZSegmentSize#
 
        Poly1X2#=Matrix Position X(Matrix)+(SX-1)*MXSegmentSize#
        Poly1Y2#=Matrix Position Y(Matrix)+Get Matrix Height(1, SX-1, SZ)
        Poly1Z2#=Matrix Position Z(Matrix)+SZ*MZSegmentSize#
 
        Poly1X3#=Matrix Position X(Matrix)+SX*MXSegmentSize#
        Poly1Y3#=Matrix Position Y(Matrix)+Get Matrix Height(1, SX, SZ)
        Poly1Z3#=Matrix Position Z(Matrix)+SZ*MZSegmentSize#
 
        Poly2X1#=Matrix Position X(Matrix)+(SX-1)*MXSegmentSize#
        Poly2Y1#=Matrix Position Y(Matrix)+Get Matrix Height(1, SX-1, SZ-1)
        Poly2Z1#=Matrix Position Z(Matrix)+(SZ-1)*MZSegmentSize#
 
        Poly2X2#=Matrix Position X(Matrix)+SX*MXSegmentSize#
        Poly2Y2#=Matrix Position Y(Matrix)+Get Matrix Height(1, SX, SZ)
        Poly2Z2#=Matrix Position Z(Matrix)+SZ*MZSegmentSize#
 
        Poly2X3#=Matrix Position X(Matrix)+SX*MXSegmentSize#
        Poly2Y3#=Matrix Position Y(Matrix)+Get Matrix Height(1, SX, SZ-1)
        Poly2Z3#=Matrix Position Z(Matrix)+(SZ-1)*MZSegmentSize#
 
        Write Memblock Float 1, 12+216*(Num-1), Poly1X1#
        Write Memblock Float 1, 16+216*(Num-1), Poly1Y1#
        Write Memblock Float 1, 20+216*(Num-1), Poly1Z1#
        Write Memblock Float 1, 24+216*(Num-1), 0.0
        Write Memblock Float 1, 28+216*(Num-1), 0.0
        Write Memblock Float 1, 32+216*(Num-1), -1.0
        Write Memblock Dword 1, 36+216*(Num-1), -1
        Write Memblock Float 1, 40+216*(Num-1), 0.0
        Write Memblock Float 1, 44+216*(Num-1), 1.0
 
        Write Memblock Float 1, 48+216*(Num-1), Poly1X2#
        Write Memblock Float 1, 52+216*(Num-1), Poly1Y2#
        Write Memblock Float 1, 56+216*(Num-1), Poly1Z2#
        Write Memblock Float 1, 60+216*(Num-1), 0.0
        Write Memblock Float 1, 64+216*(Num-1), 0.0
        Write Memblock Float 1, 68+216*(Num-1), -1.0
        Write Memblock Dword 1, 72+216*(Num-1), -1
        Write Memblock Float 1, 76+216*(Num-1), 0.0
        Write Memblock Float 1, 80+216*(Num-1), 0.0
 
        Write Memblock Float 1, 84+216*(Num-1), Poly1X3#
        Write Memblock Float 1, 88+216*(Num-1), Poly1Y3#
        Write Memblock Float 1, 92+216*(Num-1), Poly1Z3#
        Write Memblock Float 1, 96+216*(Num-1), 0.0
        Write Memblock Float 1, 100+216*(Num-1), 0.0
        Write Memblock Float 1, 104+216*(Num-1), -1.0
        Write Memblock Dword 1, 108+216*(Num-1), -1
        Write Memblock Float 1, 112+216*(Num-1), 1.0
        Write Memblock Float 1, 116+216*(Num-1), 1.0
 
        Write Memblock Float 1, 120+216*(Num-1), Poly2X1#
        Write Memblock Float 1, 124+216*(Num-1), Poly2Y1#
        Write Memblock Float 1, 128+216*(Num-1), Poly2Z1#
        Write Memblock Float 1, 132+216*(Num-1), 0.0
        Write Memblock Float 1, 136+216*(Num-1), 0.0
        Write Memblock Float 1, 140+216*(Num-1), -1.0
        Write Memblock Dword 1, 144+216*(Num-1), -1
        Write Memblock Float 1, 148+216*(Num-1), 1.0
        Write Memblock Float 1, 152+216*(Num-1), 0.0
 
        Write Memblock Float 1, 156+216*(Num-1), Poly2X2#
        Write Memblock Float 1, 160+216*(Num-1), Poly2Y2#
        Write Memblock Float 1, 164+216*(Num-1), Poly2Z2#
        Write Memblock Float 1, 168+216*(Num-1), 0.0
        Write Memblock Float 1, 172+216*(Num-1), 0.0
        Write Memblock Float 1, 176+216*(Num-1), -1.0
        Write Memblock Dword 1, 180+216*(Num-1), -1
        Write Memblock Float 1, 184+216*(Num-1), 1.0
        Write Memblock Float 1, 188+216*(Num-1), 1.0
 
        Write Memblock Float 1, 192+216*(Num-1), Poly2X3#
        Write Memblock Float 1, 196+216*(Num-1), Poly2Y3#
        Write Memblock Float 1, 200+216*(Num-1), Poly2Z3#
        Write Memblock Float 1, 204+216*(Num-1), 0.0
        Write Memblock Float 1, 208+216*(Num-1), 0.0
        Write Memblock Float 1, 212+216*(Num-1), -1.0
        Write Memblock Dword 1, 216+216*(Num-1), -1
        Write Memblock Float 1, 220+216*(Num-1), 0.0
        Write Memblock Float 1, 224+216*(Num-1), 1.0
 
      Next SZ
 
  Next SX
 
  Make Mesh From Memblock 1, 1
  If Object Exist(Object) Then Delete Object Object
  Make Object Object, 1, 22
  Hide Object Object
EndFunction
 
Function GetMatrixCloneIntersection(Obj)
  DestPoint=2
  ViewMatrix=101
  ProjectionMatrix=102
  r=Make Matrix4(ViewMatrix)
  r=Make Matrix4(ProjectionMatrix)
  r=Make Vector3(DestPoint)
  X#=2.0*(Mousex()*1.0)/(Screen Width()*1.0)-1.0
  Y#=1.0-2.0*(MouseY()*1.0)/(Screen Height()*1.0)
  Set Vector3 DestPoint, X#, Y#, 1.0
  View Matrix4 ViewMatrix
  Projection Matrix4 ProjectionMatrix
  r=Inverse Matrix4(ViewMatrix, ViewMatrix)
  r=Inverse Matrix4(ProjectionMatrix, ProjectionMatrix)
  Transform Coords Vector3 DestPoint, DestPoint, ProjectionMatrix
  Normalize Vector3 DestPoint, DestPoint
  Multiply Vector3 DestPoint, 1000.0
  Transform Coords Vector3 DestPoint, DestPoint, ViewMatrix
 
  Distance#=Intersect Object(Obj, Camera Position X(), Camera Position Y(), Camera Position Z(), X Vector3(DestPoint), Y Vector3(DestPoint), Z Vector3(DestPoint))
 
  Set Vector3 DestPoint, (X Vector3(DestPoint)-Camera Position X()), (Y Vector3(DestPoint)-Camera Position Y()), (Z Vector3(DestPoint)-Camera Position Z())
  Normalize Vector3 DestPoint, DestPoint
  Multiply Vector3 DestPoint, Distance#
  Set Vector3 DestPoint, X Vector3(DestPoint)+Camera Position X(), Y Vector3(DestPoint)+Camera Position Y(), Z Vector3(DestPoint)+Camera Position Z()
 
  If Distance#>0.0
      IntersectionPointX#=X Vector3(DestPoint)
      IntersectionPointY#=Y Vector3(DestPoint)
      IntersectionPointZ#=Z Vector3(DestPoint)
  Else
      IntersectionPointX#=0.0
      IntersectionPointY#=0.0
      IntersectionPointZ#=0.0
  EndIf
EndFunction
 
Function GetIntersectionPointX()
EndFunction IntersectionPointX#
 
Function GetIntersectionPointY()
EndFunction IntersectionPointY#
 
Function GetIntersectionPointZ()
EndFunction IntersectionPointZ#
 
Function GetDistanceToObject()
EndFunction Distance#
 
Delete Memblock 1