Well, I've ripped apart my code to make a working demo of these two functions with the vehicle system. It may still have some unnecessary code fragments, but I'm about to leave on a vacation and I'm not certain if I'll have good internet access (my ethernet connecter is broken so I have to use wi-fi). It's still VERY long.
Graphics3D 1024,768,32,1
Global mode=1
Global visibility=3000
Global counter
Color 0,255,0
Global sight=CreateImage(44,44)
SetBuffer ImageBuffer(sight)
Oval 21,21,2,2,0
Oval 18,18,8,8,0
Global radar=CreateImage(200,200)
SetBuffer ImageBuffer(radar)
Color 0,50,0
Oval 0,0,200,200
For a=-1 To 1
Color 0,255,0
Line 100+a,0,100+a,200
Line 0,100+a,200,100+a
Color 0,255,100
Next
Oval 75,75,50,50,0
Oval 50,50,100,100,0
Oval 25,25,150,150,0
Global player
Global cam
Global terrain
Global type_player=1
Global type_terrain=2
Dim box(200)
Type vehicle
Field model
Field active
Field speed#
Field opmode
Field prompt$
End Type
SetBuffer BackBuffer()
light=CreateLight()
LightRange light,10000
PositionEntity light,0,100,0
Global def_font=LoadFont("arial")
Global main_font=LoadFont("arial",24,True)
Collisions type_player,type_player,2,2
Collisions type_player,type_terrain,2,3
setup()
Color 0,0,0
While Not KeyDown(1)
If mode=1
loop1(1)
ElseIf mode=2
loop2(1)
ElseIf mode=3
loop3(1)
EndIf
UpdateWorld
If mode=1
loop1(2)
ElseIf mode=2
loop2(2)
ElseIf mode=3
loop3(2)
EndIf
RenderWorld
If mode=1 loop1(3)
If mode=2 loop2(3)
If mode=3 loop3(3)
Flip
Wend
;---------------------------------------------------------------------------------------------------
ClearWorld
Cls
End
Function setup()
player=CreateCone()
PositionEntity player,0,2,0
EntityRadius player,1
EntityType player,type_player
cam=CreateCamera()
ScaleEntity cam,.13,.13,.1
CameraRange cam,1,visibility
CameraFogMode cam,1
CameraFogColor cam,125,125,250
CameraFogRange cam,1,visibility
CameraClsColor cam,150,150,255
PositionEntity cam,EntityX#(player),EntityY#(player),EntityZ#(player)
RotateEntity cam,EntityPitch#(player),EntityYaw#(player),EntityRoll#(player)
ufo.vehicle=New vehicle
ufo\model=CreateSphere()
ScaleEntity ufo\model,2,2,2
PositionEntity ufo\model,-25,2,10
EntityType ufo\model,type_player
EntityRadius ufo\model,2
EntityPickMode ufo\model,2
ufo\opmode=2
ufo\prompt="Press Enter to fly this thing."
car.vehicle=New vehicle
car\model=CreateCube()
ScaleEntity car\model,3,3,3
PositionEntity car\model,0,5,10
EntityType car\model,type_player
EntityRadius car\model,3
EntityPickMode car\model,2
car\opmode=3
car\prompt="Press Enter to drive this thing."
terrain=CreatePlane()
EntityColor terrain,0,200,0
EntityType terrain,type_terrain
EntityPickMode terrain,2
For a=1 To 200
box(a)=CreateCube()
EntityColor box(a),50,50,50
PositionEntity box(a),Rand(-300,300),0,Rand(-300,300)
ScaleEntity box(a),Rand(1,10),Rand(1,10),Rand(1,10)
EntityType box(a),type_terrain
EntityPickMode box(a),2
Next
End Function
Function loop1(id)
Select id
Case 1
ShowEntity player
If KeyDown(200) MoveEntity player,0,0,0.2
If KeyDown(208) MoveEntity player,0,0,-0.2
If KeyDown(203) TurnEntity player,0,3,0
If KeyDown(205) TurnEntity player,0,-3,0
TranslateEntity player,0,-0.4,0
For v.vehicle=Each vehicle
TranslateEntity v\model,0,-.4,0
Next
;------------------------------------------------------------------
Case 2
PositionEntity cam,EntityX#(player),EntityY#(player),EntityZ#(player)
RotateEntity cam,pitch1#,EntityYaw#(player),EntityRoll#(player)
MoveEntity cam,0,1.5,-4
Case 3
hud(player)
SetFont(main_font)
For vhcle.vehicle=Each vehicle
If EntityDistance(player,vhcle\model)<=5
Text 726,0,vhcle\prompt,False,False
If KeyHit(28)
mode=vhcle\opmode
vhcle\active=True
counter=0
EndIf
EndIf
Next
FlushKeys()
End Select
End Function
Function loop2(id)
For craft.vehicle=Each vehicle
If craft\opmode=2 And craft\active=True
Select id
Case 1
If KeyHit(28)
mode=1
craft\active=False
craft\speed=0
PositionEntity player,EntityX(craft\model),EntityY(craft\model)+1.5,EntityZ(craft\model)
RotateEntity player,0,EntityYaw(craft\model),0
EndIf
HideEntity player
If KeyDown(200) TurnEntity craft\model,5,0,0
If KeyDown(208) TurnEntity craft\model,-5,0,0
If KeyDown(203) TurnEntity craft\model,0,0,5
If KeyDown(205) TurnEntity craft\model,0,0,-5
If KeyDown(201) craft\speed=craft\speed+.06
If KeyDown(209) craft\speed=craft\speed-.06
MoveEntity craft\model,0,0,craft\speed
;------------------------------------------------------------------
Case 2
PositionEntity cam,EntityX#(craft\model),EntityY#(craft\model),EntityZ#(craft\model)
RotateEntity cam,EntityPitch#(craft\model),EntityYaw#(craft\model),EntityRoll#(craft\model)
MoveEntity cam,0,2.5,-12
Case 3
hud(craft\model)
End Select
Else
TranslateEntity craft\model,0,-0.4,0
EndIf
Next
End Function
Function loop3(id)
For car.vehicle=Each vehicle
If car\opmode=3 And car\active=True
Select id
Case 1
If KeyHit(28)
mode=1
car\active=False
car\speed=0
PositionEntity player,EntityX(car\model),EntityY(car\model)+5,EntityZ(car\model)
RotateEntity player,0,EntityYaw(car\model),0
EndIf
HideEntity player
If KeyDown(200) MoveEntity car\model,0,0,.7
If KeyDown(208) MoveEntity car\model,0,0,-.7
If KeyDown(203) TurnEntity car\model,0,2,0
If KeyDown(205) TurnEntity car\model,0,-2,0
TranslateEntity car\model,0,-.4,0
;------------------------------------------------------------------
Case 2
PositionEntity cam,EntityX#(car\model),EntityY#(car\model),EntityZ#(car\model)
RotateEntity cam,EntityPitch#(car\model),EntityYaw#(car\model),EntityRoll#(car\model)
MoveEntity cam,0,5,-12
Case 3
hud(car\model)
End Select
Else
TranslateEntity car\model,0,-0.4,0
EndIf
Next
End Function
Function hud(entity)
sight(entity,cam,visibility)
radar(entity)
End Function
Function sight(entity,camera,range#=3000)
TFormVector 0,0,range,entity,0
x#=EntityX(entity)
y#=EntityY(entity)
z#=EntityZ(entity)
pkd=LinePick(x,y,z,TFormedX#(),TFormedY#(),TFormedZ#())
dist=Sqr((PickedX#()-x)^2+(PickedY#()-y)^2+(PickedZ#()-z)^2)
CameraProject camera,PickedX#(),PickedY#(),PickedZ#()
sightx#=ProjectedX()
sighty#=ProjectedY()
If (sightx<>0 Or sighty<>0) And pkd<>0
DrawImage sight,sightx-ImageWidth(sight)/2,sighty-ImageHeight(sight)/2
SetFont def_font
Color 0,255,0
Text sightx,sighty+12,dist,True,True
EndIf
Color 0,0,0
End Function
Function radar(entity)
DrawImage radar,0,768-ImageHeight(radar)
xb#=ImageWidth(radar)/2-2.5
yb#=768-ImageHeight(radar)/2-2.5
Color 0,255,0
For v.vehicle=Each vehicle
If EntityDistance(entity,v\model)<=100 And v\active=False
TFormPoint EntityX(v\model),EntityY(v\model),EntityZ(v\model),0,entity
Oval xb+TFormedX(),yb-TFormedZ(),5,5
EndIf
Next
Color 0,0,0
End Function
Some code I ripped out fixed the sight, but the radar has an unusual problem: when you "walk" around, it workes fine. In the sphere shaped aircraft, distances are shown as half of what they're supposed to be, while in the cube they are 1/3 their correct distance. This looks a lot like the "mode" is somehow interfering.