3D Radar like as in game Elite
`*******************************************************************************
`* Created: 06/07/2004 *
`* By: Dmitry K aka DMiTR0S dmitr0s@newmail.ru *
`*******************************************************************************
`*******************************************************************************
`* Free License Agreement. *
`* *
`* The user/developer agrees to include a visible credit to "Dmitry 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. *
`* *
`*******************************************************************************
#CONSTANT _2DVector 1
#CONSTANT _3DVector 2
#CONSTANT ProjectVector 3
#CONSTANT LookAtVector 4
#CONSTANT LookUpVector 5
#CONSTANT EyeVector 6
#CONSTANT TempVector1 7
#CONSTANT TempVector2 8
#CONSTANT TempVector3 9
#CONSTANT TempVector4 10
#CONSTANT TempVector5 11
#CONSTANT ViewMatrix 101
#CONSTANT ProjectionMatrix 102
#CONSTANT WorldMatrix 103
#CONSTANT TempMatrix 104
r=Make Vector3(_2DVector)
r=Make Vector3(_3DVector)
r=Make Vector4(ProjectVector)
r=Make Vector3(LookAtVector)
r=Make Vector3(LookUpVector)
r=Make Vector3(EyeVector)
r=Make Vector3(TempVector1)
r=Make Vector3(TempVector2)
r=Make Vector3(TempVector3)
r=Make Vector3(TempVector4)
r=Make Vector3(TempVector5)
r=Make Matrix4(ViewMatrix)
r=Make Matrix4(ProjectionMatrix)
r=Make Matrix4(WorldMatrix)
r=Make Matrix4(TempMatrix)
ShipCnt=30
Camera=1
RadarObj=1000
GLOBAL FOV As Float = 61.962139129638672
Type Vector3D
X As Double Float
Y As Double Float
Z As Double Float
EndType
Type ShipType
p1 As Vector3D
p2 As Vector3D
p3 As Vector3D
p4 As Vector3D
WeightingFactor As Integer
EndType
Dim ShipData(ShipCnt) As ShipType
Set Identity Matrix4 WorldMatrix
Randomize Timer()
Set Display Mode 1024, 768, 32
Sync Rate 0
Sync On
Hide Mouse
Make Camera Camera
Color Backdrop Camera, RGB(0,0,0)
Set Camera FOV Camera, FOV
For i=1 To ShipCnt
Make Object Cube i, Rnd(10)
Rotate Object i, Rnd(360), Rnd(360), Rnd(360)
ShipData(i).p1.X=1500.0-Rnd(3000.0)
ShipData(i).p1.Y=1500.0-Rnd(3000.0)
ShipData(i).p1.Z=1500.0-Rnd(3000.0)
ShipData(i).p2.X=1500.0-Rnd(3000.0)
ShipData(i).p2.Y=1500.0-Rnd(3000.0)
ShipData(i).p2.Z=1500.0-Rnd(3000.0)
ShipData(i).p3.X=1500.0-Rnd(3000.0)
ShipData(i).p3.Y=1500.0-Rnd(3000.0)
ShipData(i).p3.Z=1500.0-Rnd(3000.0)
ShipData(i).p4.X=1500.0-Rnd(3000.0)
ShipData(i).p4.Y=1500.0-Rnd(3000.0)
ShipData(i).p4.Z=1500.0-Rnd(3000.0)
ShipData(i).WeightingFactor=0
Position Object i, ShipData(i).p1.X, ShipData(i).p1.Y, ShipData(i).p1.Z
Next i
Load Image "RadarImage.bmp", 1, 0
Make Object Plain RadarObj, 20, 20
XRotate Object RadarObj, 90
Fix Object Pivot RadarObj
Texture Object RadarObj, 1
Set Object Light 1000, 60
Make Object Sphere 2000, 19, 50, 50
Color Object 2000, RGB(0,128,255)
Scale Object 2000, 100, 50, 100
Ghost Object On 2000
Lock Object On 2000
Position Object 2000, 0, -20, 45
Set Object Ambient 2000, 100
Do
`Camera control
Control Camera Using ArrowKeys Camera, 1, 1
cay#=cay#+Mousemovex()/2.0
cax#=cax#+Mousemovey()/2.0
If cax#>89.0 Then cax#=89.0
ax#=Curveangle(cax#,ax#,4)
ay#=Curveangle(cay#,ay#,4)
Rotate Camera Camera, Wrapvalue(ax#),Wrapvalue(ay#), 0.0
CopyCameraViewMatrix(ViewMatrix, Camera Position X(Camera), Camera Position Y(Camera), Camera Position Z(Camera), Camera Angle X(Camera), Camera Angle Y(Camera), Camera Angle Z(Camera))
`Position of the radar in camera space. Alternative of command Lock Object On
Set Vector3 _3DVector, 0, -20, 45
CameraSpaceToWorldSpace(_3DVector, _3DVector, ViewMatrix)
Position Object RadarObj, X Vector3(_3DVector), Y Vector3(_3DVector), Z Vector3(_3DVector)
Set Vector3 _3DVector, 0, -20, 0
CameraSpaceToWorldSpace(_3DVector, _3DVector, ViewMatrix)
Point Object RadarObj, X Vector3(_3DVector), Y Vector3(_3DVector), Z Vector3(_3DVector)
For i=1 To ShipCnt
`Move ship
MoveShip(i)
`Ship coordinates in camera space
Set Vector3 _3DVector, Object Position X(i), Object Position Y(i), Object Position Z(i)
WorldSpaceToCameraSpace(_3DVector, _3DVector, ViewMatrix)
Multiply Vector3 _3DVector, 0.005
ShipCameraSpaceX#=X Vector3(_3DVector)
ShipCameraSpaceY#=Y Vector3(_3DVector)
ShipCameraSpaceZ#=Z Vector3(_3DVector)
ScaledShipCameraSpaceY#=ShipCameraSpaceY#*2
Dist#=ShipCameraSpaceX#*ShipCameraSpaceX#+ScaledShipCameraSpaceY#*ScaledShipCameraSpaceY#+ShipCameraSpaceZ#*ShipCameraSpaceZ#
If Dist#<=9.5*9.5
`Draw mark of the ship on radar
Set Vector3 _3DVector, ShipCameraSpaceX#, ShipCameraSpaceY#-20, ShipCameraSpaceZ#+45
CameraSpaceToWorldSpace(_3DVector, _3DVector, ViewMatrix)
_3DTo2D(_2DVector, _3DVector, ViewMatrix)
ShipX=X Vector3(_2DVector)
ShipY=Y Vector3(_2DVector)
Set Vector3 _3DVector, ShipCameraSpaceX#, -20, ShipCameraSpaceZ#+45
CameraSpaceToWorldSpace(_3DVector, _3DVector, ViewMatrix)
_3DTo2D(_2DVector, _3DVector, ViewMatrix)
ShipOnRadarPlaneX=X Vector3(_2DVector)
ShipOnRadarPlaneY=Y Vector3(_2DVector)
Line ShipX, ShipY, ShipOnRadarPlaneX, ShipOnRadarPlaneY
Line ShipX, ShipY, ShipX+4, ShipY
EndIf
Next i
`Print Info
Text 10, 10, "3D Radar"
Text 10, 30, "Code by Dmitry K"
Text 10, 50, "E-mail: dmitr0s@newmail.ru"
Text 10, 70, "Move mouse to rotate camera / Arrowkeys to move camera"
Text 10, 90, "Fps: "+Str$(Screen Fps())
Sync
Loop
Function MoveShip(Ship)
Set Vector3 TempVector1, ShipData(Ship).p1.X, ShipData(Ship).p1.Y, ShipData(Ship).p1.Z
Set Vector3 TempVector2, ShipData(Ship).p2.X, ShipData(Ship).p2.Y, ShipData(Ship).p2.Z
Set Vector3 TempVector3, ShipData(Ship).p3.X, ShipData(Ship).p3.Y, ShipData(Ship).p3.Z
Set Vector3 TempVector4, ShipData(Ship).p4.X, ShipData(Ship).p4.Y, ShipData(Ship).p4.Z
s#=ShipData(Ship).WeightingFactor/10000.0
ShipData(Ship).WeightingFactor=ShipData(Ship).WeightingFactor+1
Catmullrom Vector3 TempVector5, TempVector1, TempVector2, TempVector3, TempVector4, s#
Position Object Ship, X Vector3(TempVector5), Y Vector3(TempVector5), Z Vector3(TempVector5)
If ShipData(Ship).WeightingFactor=10000
ShipData(Ship).p1.X=ShipData(Ship).p2.X
ShipData(Ship).p1.Y=ShipData(Ship).p2.Y
ShipData(Ship).p1.Z=ShipData(Ship).p2.Z
ShipData(Ship).p2.X=ShipData(Ship).p3.X
ShipData(Ship).p2.Y=ShipData(Ship).p3.Y
ShipData(Ship).p2.Z=ShipData(Ship).p3.Z
ShipData(Ship).p3.X=ShipData(Ship).p4.X
ShipData(Ship).p3.Y=ShipData(Ship).p4.Y
ShipData(Ship).p3.Z=ShipData(Ship).p4.Z
ShipData(Ship).p4.X=1500.0-Rnd(3000.0)
ShipData(Ship).p4.Y=1500.0-Rnd(3000.0)
ShipData(Ship).p4.Z=1500.0-Rnd(3000.0)
ShipData(Ship).WeightingFactor=0
EndIf
EndFunction
Function WorldSpaceToCameraSpace(VectorResult, VectorSource, CameraViewMatrix)
Transform Coords Vector3 VectorResult, VectorSource, CameraViewMatrix
If Z Vector3(VectorResult) < 0.0 Then Result=-1
If Z Vector3(VectorResult) > 0.0 Then Result=1
EndFunction Result
Function CameraSpaceToWorldSpace(VectorResult, VectorSource, CameraViewMatrix)
r=Inverse Matrix4(TempMatrix, CameraViewMatrix)
Transform Coords Vector3 VectorSource, VectorSource, TempMatrix
EndFunction
Function _3DTo2D(VectorResult, VectorSource, CameraViewMatrix)
Build FOV LHMatrix4 ProjectionMatrix, FOV*(3.1415926535897932384626433832795/180.0), (Screen Width()*1.0)/(Screen Height()*1.0), 1.0, 3000.0
Project Vector3 VectorResult, VectorSource, ProjectionMatrix, CameraViewMatrix, WorldMatrix
EndFunction
Function CopyCameraViewMatrix(ResultMatrix, cpX#, cpY#, cpZ#, caX#, caY#, caZ#)
CX#=Cos(caX#)
SX#=Sin(caX#)
CY#=Cos(caY#)
SY#=Sin(caY#)
CZ#=Cos(caZ#)
SZ#=Sin(caZ#)
Set Vector3 EyeVector, cpX#, cpY#, cpZ#
Set Vector3 LookAtVector, -1.0*SZ#*(-1.0*SX#)+CZ#*SY#*CX#+cpX#, CZ#*(-1.0*SX#)+SZ#*SY#*CX#+cpY#, CY#*CX#+cpZ#
Set Vector3 LookUpVector, -1.0*SZ#*CX#+CZ#*SY#*SX#, CZ#*CX#+SZ#*SY#*SX#, CY#*SX#
Build LookAt LHMatrix4 ResultMatrix, EyeVector, LookAtVector, LookUpVector
EndFunction
some media:
http://www.dannywartnaby.co.uk/rgt/attachments/RadarImage.zip
Specs: P4 2.4GHz, DIMM 256 266Mhz, 60GB HD 7200rpm ATA-133, GeForce 4 MX440, CD-RW+DVD, WinXP