Hi folks, I'm making a little 3D space shooter game and I really wanted a 3D radar so after quite a battle I finally got it working.
I'm not a mathematician so I had to rely heavily on the abilities of AGKS (as usual) and it works just how I wanted.
Feel free to use any of this in your own game projects. Amalien made the asteroid models and she gives permission too.
Cheers.
// Project: 3D radar test by ando
// Created: 20-06-18
SetErrorMode(2)
width# = GetMaxDeviceWidth()
height# = GetMaxDeviceHeight()
SetWindowSize(width#,height#,1)
SetVirtualResolution(width#,height#)
SetSyncRate(30,0)
SetScissor(0,0,0,0)
UseNewDefaultFonts(1)
setprintsize(45)
SetSunActive(1)
SetGenerateMipmaps(1)
SetAmbientColor(160,160,160)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx arrays
DIM object[9] // total number of objects that can appear on radar
DIM objectType[9] // target types will have a different color radar dot
for t = 1 to 9
if t<6 then objectType[t]=1 // asteroid - neutral
if t=6 then objectType[t]=2 // shuttle - friend
if t>6 then objectType[t]=3 // ship - enemy
next t
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx asteroids
for a = 1 to 5
object[a]=LoadObject("asteroid"+str(a)+"/asteroid"+str(a)+".obj",0)
SetObjectImage(object[a],LoadImage("asteroid"+str(a)+"/asteroid"+str(a)+"_d.png"),0)
SetObjectposition(object[a],random(1,4000)-2000,random(1,4000)-2000,random(1,4000)-2000)
SetObjectRotation(object[a],random(1,360),random(1,360),random(1,360))
SetObjectScalePermanent(object[a],50,50,50)
next a
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx shuttle craft
object[6]=LoadObject("shuttle/shuttle.obj",0)
SetObjectRotation(object[6],0,0,0)
shuttletex1=LoadImage("shuttle/shuttletex1.png")
SetObjectImage(object[6],shuttletex1,0)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx enemy ships
for a = 7 to 9
object[a]=LoadObject("RoboShip/spaceship3.ms3d",0)
SetObjectImage(object[a],LoadImage("Roboship/starpic"+str(a-6)+".png"),0)
SetObjectposition(object[a],random(3000,5000),random(3000,5000),random(1,4000)-2000)
SetObjectRotation(object[a],random(1,360),random(1,360),random(1,360))
SetObjectScalePermanent(object[a],1.4,1.4,1.4)
next a
objectpos#=CreateVector3(0,0,0)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Player sphere and camera
man1=CreateObjectSphere(5,6,12)
SetObjectVisible(man1,0)
SetObjectPosition(man1,4000,4000,-8000)
//SetObjectRotation(man1,0,180,0)
cam1=1
SetCameraRange(cam1,1,5000000 )
SetCameraposition(cam1,0,0,0)
//SetCameraRotation(cam1,0,180,0)
mypos#=CreateVector3(GetObjectX(man1),GetObjectY(man1),GetObjectZ(man1))
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx pivot
pivot=createobjectbox(1,1,1) // parent pivot used for rotating objects relative to player
setobjectvisible(pivot,0) // and is only used at radar render time
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx radar spheres, lines and grid
DIM sphere[9] // spheres for radar dots
DIM line[9] // lines for radar dot heights
radarRange=19800
radarOffset#=1.0
displayRange=3
for s = 1 to 9
sphere[s]=createobjectsphere(1,12,24)
setobjectvisible(sphere[s],0)
line[s]=createobjectbox(1,1,1)
setobjectvisible(line[s],0)
next s
grid=CreateObjectPlane(40000,40000)
gridtex=LoadImage("player/grid4.png")
SetObjectImage(grid,gridtex,0)
RotateObjectLocalX(grid,90)
SetObjectAlphaMask(grid,1)
setobjectvisible(grid,0)
sphereVec=CreateVector3(0,0,0)
cam1Vec=CreateVector3(0,0,0)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx radar display sprite
rendertex=CreateRenderImage(512,512,0,1)
rendersprite=CreateSprite(rendertex)
SetSpriteSize(rendersprite,width#/4,height#/4)
SetSpritePosition(rendersprite,width#/2-GetSpriteWidth(rendersprite)/2,height#-GetSpriteHeight(rendersprite))
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx skybox
starsphere=LoadObject("misc/starsphere1.obj")
SetObjectposition(starsphere,0,0,-1000)
SetObjectScalePermanent(starsphere,2100,2100,2100)
startex=LoadImage("misc/stars.png")
SetObjectImage(starsphere,startex,1)
SetImageWrapU(startex,1)
SetImageWrapV(startex,1)
SetObjectMeshUVScale(starsphere,1,0,5,5)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx player sights
sightstex=LoadImage("player/sights4.png")
sightsprite=CreateSprite(sightstex)
SetSpriteSize(sightsprite,height#/2,height#/2)
SetSpritePosition(sightsprite,width#/2-height#/4,height#/2-height#/4)
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
help=1
setrawmousevisible(0)
SetRawMousePosition(width#/2,height#/2)
do // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx MAIN LOOP
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx prepare scene for render to the radar screen
SetRenderToImage(rendertex,-1)
SetCameraAspect(cam1,0.7)
setspritevisible(sightsprite,0)
setspritevisible(rendersprite,0)
SetObjectPosition(pivot,0,0,0)
SetObjectRotation(pivot,0,0,0)
SetObjectPosition(grid,0,0,0)
SetObjectRotation(grid,90,0,0)
setobjectvisible(grid,1)
setobjectvisible(starsphere,0)
setprintsize(125)
SetVector3(mypos#,GetObjectX(man1),GetObjectY(man1),GetObjectZ(man1))
for o=1 to 9
SetVector3(objectpos#,GetObjectX(object[o]),GetObjectY(object[o]),GetObjectZ(object[o]))
objectDist#=GetVector3Distance(mypos#,objectpos#)
setobjectvisible(object[o],0)
if objectDist# < radarRange
fixobjecttoobject(sphere[o],pivot)
setobjectvisible(sphere[o],1)
if objectType[o] = 1 then setobjectcolor(sphere[o],255,255,0,255) // yellow is neutral
if objectType[o] = 2 then setobjectcolor(sphere[o],50,255,50,255) // green is friend
if objectType[o] = 3 then setobjectcolor(sphere[o],255,50,50,255) // red is enemy
setobjectposition(sphere[o],(getobjectX(object[o])-getobjectX(man1))*radarOffset#,(getobjectY(object[o])-getobjectY(man1))*radarOffset#,(getobjectZ(object[o])-getobjectZ(man1))*radarOffset#)
endif
next o
quatW#=GetObjectQuatW(man1)
quatX#=GetObjectQuatX(man1)
quatY#=GetObjectQuatY(man1)
quatZ#=GetObjectQuatZ(man1)
SetObjectRotationQuat(pivot,-quatW#,quatX#,quatY#,quatZ#)
SetCameraPosition(cam1,GetObjectX(grid),GetObjectY(grid),GetObjectZ(grid))
SetCamerarotation(cam1,45,0,0)
MoveCameraLocalZ(cam1,-32000)
SetVector3(cam1Vec,GetCameraX(cam1),GetCameraY(cam1),GetCameraZ(cam1))
for L=1 to 9
if getobjectvisible(sphere[L])
setobjectvisible(line[L],1)
SetVector3(sphereVec,GetObjectWorldX(sphere[L]),GetObjectWorldY(sphere[L]),GetObjectWorldZ(sphere[L]))
sphereDist=GetVector3Distance(cam1Vec,sphereVec)
scaleAmount=1000
scaleAmount=scaleAmount+(sphereDist-12000)*0.05
setobjectscale(line[L],scaleAmount*0.2,getobjectworldY(sphere[L]),scaleAmount*0.2)
setobjectscale(sphere[L],scaleAmount,scaleAmount,scaleAmount)
setobjectposition(line[L],getobjectworldX(sphere[L]),getobjectworldY(sphere[L])/2,getobjectworldZ(sphere[L]))
if objectType[L] = 1 then setobjectcolor(line[L],255,255,0,255) // yellow is neutral
if objectType[L] = 2 then setobjectcolor(line[L],50,255,50,255) // green is friend
if objectType[L] = 3 then setobjectcolor(line[L],255,50,50,255) // red is enemy
endif
next L
Print( "Range " + str(displayRange))
ClearScreen()
SetSpriteImage(rendersprite,rendertex,0)
Render()
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx set render to main screen
SetRenderToScreen()
SetCameraAspect(cam1,width#/height#)
setspritevisible(sightsprite,1)
setspritevisible(rendersprite,1)
setobjectvisible(grid,0)
setobjectvisible(starsphere,1)
SetObjectRotation(pivot,0,0,0)
setprintsize(45)
RotateCameraLocalX(cam1,getobjectangleX(man1))
RotateCameraLocalY(cam1,getobjectangleY(man1))
RotateCameraLocalZ(cam1,getobjectangleZ(man1))
SetCameraPosition(cam1,GetObjectX(man1),GetObjectY(man1),GetObjectZ(man1))
for c=1 to 9
if getobjectvisible(sphere[c])
setobjectvisible(sphere[c],0)
setobjectvisible(line[c],0)
fixobjecttoobject(sphere[c],0)
endif
setobjectvisible(object[c],1)
next c
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx asteroids
RotateObjectLocalX(object[1],0.35)
RotateObjectLocalY(object[1],0.53)
RotateObjectLocalX(object[2],0.15)
//RotateObjectLocalZ(object[2],0.33)
MoveObjectLocalZ(object[2],200.0)
RotateObjectLocalY(object[3],0.43)
MoveObjectLocalZ(object[3],200.0)
RotateObjectLocalZ(object[4],0.35)
//RotateObjectLocalY(object[4],-0.53)
MoveObjectLocalZ(object[4],200.0)
RotateObjectLocalX(object[5],0.16)
//RotateObjectLocalZ(object[5],-0.22)
MoveObjectLocalZ(object[5],200.0)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx enemy ships
RotateObjectLocalY(object[7],0.53)
MoveObjectLocalZ(object[7],60.0)
RotateObjectLocalX(object[8],0.55)
//RotateObjectLocalY(object[8],-0.53)
MoveObjectLocalZ(object[8],60.0)
RotateObjectLocalX(object[9],0.56)
//RotateObjectLocalZ(object[9],-0.22)
MoveObjectLocalZ(object[9],60.0)
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx player
mouseX# = (GetRawMouseX()-width#/2)*0.003
mouseY# = (GetRawMouseY()-height#/2)*0.003
RotateObjectLocalX(man1,mouseY#)
RotateObjectLocalY(man1,mouseX#)
SetCameraPosition(cam1,GetObjectX(man1),GetObjectY(man1),GetObjectZ(man1))
SetCameraRotation(cam1,GetObjectAngleX(man1),GetObjectAngleY(man1),GetObjectAngleZ(man1))
// player roll
if GetRawKeyState(65)
RotateObjectLocalZ(man1,1)
endif
if GetRawKeyState(68)
RotateObjectLocalZ(man1,-1)
endif
// player speed
man1speed = 0
if GetRawMouseLeftState()
man1speed = 60
endif
if GetRawMouseRightState()
man1speed = -50
endif
MoveObjectLocalZ(man1,man1speed)
// player click MMB to stop rotation and hold in for fine control
if GetRawMouseMiddleState()
SetRawMousePosition(width#/2,height#/2)
endif
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx radar range
if GetRawKeyPressed(82) // R
radarOffset#=radarOffset#-0.5
displayRange=displayRange+1
if radarOffset# < 0.5 then radarOffset#=2
if displayRange > 4 then displayRange=1
if displayRange=1 then radarRange=10000
if displayRange=2 then radarRange=13100
if displayRange=3 then radarRange=19800
if displayRange=4 then radarRange=40000
endif
// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SetObjectPosition(starsphere,GetObjectX(man1),GetObjectY(man1),GetObjectZ(man1))
//framerate = ScreenFPS()
if GetRawKeyPressed(72) then help=1-help // key H for help on / off
if help
//Print( " " + str(framerate))
Print( " H hide/show text")
Print( " R radar range")
Print( " A D roll")
Print( " use mouse ")
endif
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// SetRawMousePosition(width#/2,height#/2)
sync()
if GetrawKeyPressed(27) then end // Escape key
loop
Never play leap frog with a unicorn.