if you want to be able to move an object around
here is an example
#constant width=1024
#constant height=768
SetWindowSize(width,height,0)
Setvirtualresolution(width,height)
CreateObjectBox(1,16,16,16)
SetObjectPosition(1,50,1,50)
CreateObjectBox(2,10*16,1*16,10*16)
SetObjectColor(2,0,255,0,255)
SetObjectPosition(2,50,-15,50)
//SetCameraPosition(1,50,50,-10)
global angx#=30
global angy#=0
global camerax#=50
global cameray#=50
global cameraz#=-10
global startx#=512
global starty#=384
#constant Depth#=48
do
moveblockwithmouse()
//movecamerawithmouse()
sync()
loop
function movecamerawithmouse()
fDiffX# = (GetPointerX() - startx#)/6.0
fDiffY# = (GetPointerY() - starty#)/6.0
newX# = angx# + fDiffY#
if ( newX# > 89 ) then newX# = 89
if ( newX# < -89 ) then newX# = -89
SetCameraRotation( 1, newX#, angy# + fDiffX#, 0 )
SetCameraPosition(1,camerax#,cameray#,cameraz#)
endfunction
function moveblockwithmouse()
VectorX# = Get3DVectorXFromScreen( GetPointerX(), GetPointerY() )
VectorY# = Get3DVectorYFromScreen( GetPointerX(), GetPointerY() )
VectorZ# = Get3DVectorZFromScreen( GetPointerX(), GetPointerY()*2 )
// get a vector that points in the direction the camera is looking
VectorX2# = Get3DVectorXFromScreen( GetVirtualWidth()/2.0, GetVirtualHeight()/2.0 )
VectorY2# = Get3DVectorYFromScreen( GetVirtualWidth()/2.0, GetVirtualHeight()/2.0 )
VectorZ2# = Get3DVectorZFromScreen( GetVirtualWidth()/2.0, GetVirtualHeight()/2.0 )
// normalise Vector in the direction of Vector2
Dot# = VectorX#*VectorX2# + VectorY#*VectorY2# + VectorZ#*VectorZ2#
if ( Dot# > 0 )
VectorX# = VectorX# / Dot#
VectorY# = VectorY# / Dot#
VectorZ# = VectorZ# / Dot#
endif
WorldX# = VectorX# * Depth# + GetCameraX(1)
WorldY# = VectorY# * Depth# + GetCameraY(1)
WorldZ# = VectorZ# * Depth# + GetCameraZ(1)
SetObjectPosition(1,WorldX#,GetObjectY( 1) ,WorldZ#)
endfunction
raycasting is great for checking collision between two objects ie
local height# as float
local depth# as float
local x# as float
local y# as float
local z# as float
local start_x# as float
local start_y# as float
local start_z# as float
local end_x# as float
local end_y# as float
local end_z# as float
local object_Hit as integer
//this checks for colliion between an object with any other object and returns its id
width#=(GetObjectMeshSizeMaxX(objID,1)-GetObjectMeshSizeMinX(objID,1))/2
height#=(GetObjectMeshSizeMaxX(objID,1)-GetObjectMeshSizeMinX(objID,1))/2
depth#=(GetObjectMeshSizeMaxX(objID,1)-GetObjectMeshSizeMinX(objID,1))/2
x#=getObjectWorldX(objID)
y#=GetObjectWorldY(objID)
z#=getObjectWorldZ(objID)
// calculate the start of the ray cast
start_x# = x#-width#
start_y# = y#+height#
start_z# = z#-depth#
// calculate the end of the ray cast
end_x# = x#+width#
end_y# = y#-height#
end_z# = z#+depth#
// determine which object has been hit
object_Hit = ObjectRayCast(0,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
If you just want to be able to select an object with the mouse
// get the position of the pointer (mouse)
pointer_x = GetPointerX()
pointer_y = GetPointerY()
// get the x, y and z unit vectors based on the pointer position
unit_x# = Get3DVectorXFromScreen(pointer_x,pointer_y)
unit_y# = Get3DVectorYFromScreen(pointer_x,pointer_y)
unit_z# = Get3DVectorZFromScreen(pointer_x,pointer_y)
// calculate the start of the ray cast, which is the unit vector + the camera position
start_x# = unit_x# + 0
start_y# = unit_y# + 10
start_z# = unit_z# - 20
// calculate the end of the vector, which is the unit vector multiplied by the length of the ray cast and then add the camera position to it
end_x# = 800*unit_x# + 0
end_y# = 800*unit_y# + 10
end_z# = 800*unit_z# - 20
// determine which object has been hit
object_hit = ObjectRayCast(0,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
fubar