Hello everyone.
I wanted to try something like minecraft. I wanted to look at a cube and raycast the cube with ObjectRayCast(0...) from my Head (camera on an object) to faraway.
This works all good. I knew with
If GetObjectRayCastNumHits()>0
HitID = GetObjectRayCastNumHits()-1
hitObjID = GetObjectRayCastHitID(HitID)
EndIf
I will get the right object I am looking at. But I want to draw a transparent cube where the new one can be placed. And there is the problem. I don't want to cast the Object I want to place or other helper-objects and Brush-Visuals for the Objects I want to place or 3D Arrows to show where I can do something.
How can I cast all, but only special objects not or how can I cast groups of objects? Or is there an easy way arround?
Something with the HitID, but I can't figure out, how I cast through the object that is nearest to me, but not the object I want to cast, because that's the object I want to place.
Thanks for help.
I changed the tutorial to ObjectRaycast and GetObjectRayCastX, but in the last example there is only one Object.
I don't know how to do this red ball the easy way.
[youtube]https://www.youtube.com/watch?v=sM4T_sZO6KI[/youtube]
My Code so far
// this example should work in AGK V1 and AGK V2
// use the w and s key or the push up and down on the on screen joystick to move the two spheres back and forth
// the cube will turn red if the raycast - from the green sphere to the red sphere - intersects it
SetAntialiasMode( 1) // 4X-MSAA
SetScreenResolution(1280,720)
SetDisplayAspect( 1.75 ) // ca. 16:9
//SetWindowSize(1280,720,0,1)
SetWindowTitle(":: FL Cubes ::")
//AddVirtualJoystick( 1, 1, 1, 10 )
// create a box (it will automaticall be located at point 0,0,0)
CreateObjectBox(2,1,8,8)
SetObjectColor(2,255,128,128,255)
For i = 5 to 8
//objID = CreateObjectBox(8*i+8,8*i+8,8*i+8)
CreateObjectBox(1+i,20,20,20)
objID=1+i
SetObjectPosition(objID,0,0,-60+40*i-150)
SetObjectColor(objID,25,100,255,255)
Next
// create a green sphere and position it
CreateObjectSphere(30,10,12,12)
SetObjectColor(30,255,0,0,255)
SetObjectPosition(30,-50,0,50)
// create a red sphere and position it
CreateObjectSphere(20,10,12,12)
SetObjectColor(20,0,255,0,255)
SetObjectPosition(20,50,0,20)
// create a direction to make the whole scene look better (go on, delete this line and you'll find out why I included it)
//CreatePointLight(1,-1,-1,-1,255,255,255)
//CreateListOfElements()
// position and orientate the camera
SetCameraPosition(1,100,150,-60)
SetCameraLookAt(1,0,0,0,0)
// main loop
hitID=-2
do
// get player input (multiply by -1 so sphere move in direction the joystick is pushed, once again delete it and see what happens)
joystick_y# = GetJoystickY() * -1
// move the spheres based on input
MoveObjectLocalZ(20, joystick_y#)
MoveObjectLocalZ(30, joystick_y#)
// ray cast from the green sphere to the blue sphere
old_x# = GetObjectX(20)-11
old_y# = GetObjectY(20)
old_z# = GetObjectZ(20)
new_x# = GetObjectX(30)+11
new_y# = GetObjectY(30)
new_z# = GetObjectZ(30)
lasthitID = hitID
hitID=-2
//SetObjectVisible(2,0)
if ObjectRayCast(0,old_x#,old_y#,old_z# ,new_x#,new_y#,new_z#) = 1
EndIf
//SetObjectVisible(2,1)
If GetObjectRayCastNumHits()>0
Hit = GetObjectRayCastNumHits()-1
hitID = GetObjectRayCastHitID(Hit)
/* if hitID =2
Hit = GetObjectRayCastNumHits()
hitID = GetObjectRayCastHitID(Hit)
EndIf
*/
EndIf
If hitID<>-2
SetObjectColor(hitID,255,255,0,255)
/*
// get the ray cast intersect point
intersect_x# = GetObjectRayCastX(Hit)
intersect_y# = GetObjectRayCastY(Hit)
intersect_z# = GetObjectRayCastZ(Hit)
// set the position of the blue sphere to the intersect point
SetObjectPosition(2,intersect_x#,intersect_y#,intersect_z#)
// make the blue sphere visible
SetObjectVisible(2,1)
*/
EndIf
If lasthitID<>hitID
SetObjectVisible(2,0)
SetObjectColor(lasthitID,25,100,255,255)
Endif
Print (Str(GetObjectRayCastDistance( 0 ),2))
print(Str(GetObjectRayCastNumHits()))
Print (Str(hitID))
sync()
loop