I would project the 3d coordinate of the objects into 2d screen coordinates, like this:
// Project: 3D_Rts_Selection
// Created: 2016-06-23
// set window properties
SetWindowTitle( "3D_Rts_Selection" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
type _box_type
obj as integer
spr as integer
xp_screen# as integer
yp_screen# as integer
xp_world# as float
yp_world# as float
zp_world# as float
endtype
dim box[10] as _box_type
For i= 0 to 10
r = 5
box[i].obj = CreateObjectBox(r,r,r)
r = 40
box[i].xp_world# = random(-r,r)
box[i].yp_world# = random(-r,r)
box[i].zp_world# = random(-r,r)
SetObjectPosition(box[i].obj,box[i].xp_world#,box[i].yp_world#,box[i].zp_world#)
box[i].spr = createSprite(0)
SetSpriteSize(box[i].spr,10,10)
SetSpriteColor(box[i].spr,0,0,255,150)
next
SetCameraPosition(1,0,-50,-50)
SetCameraLookAt(1,0,0,0,0)
do
// hold shift to move slower
speed# = 4
// control the camera with WASD
if ( GetRawKeyState( 38 ) ) then MoveCameralocalZ( 1, speed# )
if ( GetRawKeyState( 40 ) ) then MoveCameraLocalZ( 1, -speed# )
if ( GetRawKeyState( 37 ) ) then MoveCameraLocalX( 1, -speed# )
if ( GetRawKeyState( 39 ) ) then MoveCameraLocalX( 1, speed# )
if ( GetRawKeyState( 27 ) ) then end
// rotate the camera
if GetRawMouseLeftPressed()
oldMx#= GetPointerX()
oldMy#= GetPointerY()
angx# = GetCameraAngleX(1)
angy# = GetCameraAngleY(1)
endif
if GetRawMouseLeftState()
fDiffX# = (GetPointerX() -oldMx#)/4.0
fDiffY# = (GetPointerY() -oldMy#)/4.0
newX# = angx# + fDiffY#
if ( newX# > 89 ) then newX# = 89
if ( newX# < -89 ) then newX# = -89
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
endif
// project 3d coordinate to 2d screen coordinates
for i = 0 to 10
box[i].xp_screen# = GetScreenXFrom3D(box[i].xp_world#,box[i].yp_world#,box[i].zp_world#)
box[i].yp_screen# = GetScreenYFrom3D(box[i].xp_world#,box[i].yp_world#,box[i].zp_world#)
SetSpritePositionByOffset(box[i].spr,box[i].xp_screen#,box[i].yp_screen#)
next i
if GetRawMouseRightPressed()
StartX# = GetpointerX()
StartY# = GetpointerY()
For i= 0 to 10
SetObjectColor(box[i].obj,0,255,0,255)
next
endif
if GetRawMouseRightState()
EndX# = GetpointerX()
EndY# = GetpointerY()
DrawBox(StartX#,StartY#,EndX#,EndY#,255,255,255,255,0)
endif
if GetRawMouseRightReleased()
EndX# = GetpointerX()
EndY# = GetpointerY()
// determine the left, right, top and bottom of the selection box
if StartX# < EndX#
left# = StartX#
right# = EndX#
else
left# = EndX#
right# = StartX#
endif
if StartY# < EndY#
top# = StartY#
bottom# = EndY#
else
top# = EndY#
bottom# = StartY#
endif
// determine if the centre of the object if within the selection box
// based on the object's screen x and y position
For i=0 to 10
if box[i].xp_screen# > left# and box[i].xp_screen# < right#
if box[i].yp_screen# > top# and box[i].yp_screen# < bottom#
SetObjectColor(box[i].obj,255,0,0,255)
endif
endif
next
Endif
Sync()
loop
I've also added in code that means it doesn't matter how the player draws the box (from corner to corner).