have a example, maybe u can use it.
its controlled by cursor keys.
it align the cube by the surface normal vector.
rem
rem AGK Application 1.08 Beta 9
rem MR
rem Landscape App
SetDisplayAspect( 4.0/3.0 )
setdefaultwrapu(1)
setdefaultwrapv(1)
//------------------------------------------ Camera
cameraID=1
SetCameraRange( cameraID, 1, 1000 )
SetCameraPosition( cameraID, 0,1.68,-5 )
SetCameraLookAt( cameraID, 0, 0, 0, 0 )
//------------------------------------------ Licht
lightID=1
ClearLightDirectionals()
CreateLightDirectional( lightID, 0, -1, 1, 255, 255, 255 )
//------------------------------------------ Boden
iGround=CreateObjectBox( 100, 0.2, 100 )
SetObjectPosition(iGround,0,-0.1,0)
SetObjectCollisionMode( iGround, 1 )
SetObjectColor( iGround, 255,255,255, 255 )
SetObjectLightMode(iGround,1)
//RotateObjectLocalz(iGround,45.0/2.0)
//------------------------------------------ Box
iBox=CreateObjectBox( 1, 1, 1 )
SetObjectPosition( iBox, 0, 0.5, 0 )
SetObjectCollisionMode( iBox, 0 )
SetObjectColor( iBox, 255, 0, 0, 192 )
SetObjectLightMode(iBox,1)
//------------------------------------------
do
//print(GetRawLastKey( ) )
k37=GetRawKeystate( 37 )
k39=GetRawKeystate( 39 )
k38=GetRawKeystate( 38 )
k40=GetRawKeystate( 40 )
if k37
amount#=360.0/(60.0*5.0)
RotateObjectLocalz( iGround, amount# )
endif
if k39
amount#=360.0/(60.0*5.0)
RotateObjectLocalz( iGround, -amount# )
endif
if k38
amount#=360.0/(60.0*5.0)
RotateObjectLocalx( iGround, amount# )
endif
if k40
amount#=360.0/(60.0*5.0)
RotateObjectLocalx( iGround, -amount# )
endif
Print("align object to surface")
//Cube->Down
ObjectRayCast(0,getobjectx(iBox),getobjecty(iBox),getobjectz(iBox), getobjectx(iBox),getobjecty(iBox)-100,getobjectz(iBox))
n=GetObjectRayCastNumHits()
print(n)
for i=0 to n-1
obj = GetObjectRayCastHitID(i)
print("Obj " +str(obj))
nx#=GetObjectRayCastNormalX(i)
ny#=GetObjectRayCastNormalY(i)
nz#=GetObjectRayCastNormalZ(i)
printv("nx",nx#)
printv("ny",ny#)
printv("nz",nz#)
StableZ(iBox,nx#,ny#,nz#)
StableX(iBox,nx#,ny#,nz#)
x#=GetObjectRayCastX(i)
y#=GetObjectRayCastY(i)
z#=GetObjectRayCastZ(i)
h#=0.5 //height
px#=x#+nx#*h#
py#=y#+ny#*h#
pz#=z#+nz#*h#
setobjectposition(iBox,px#,py#,pz#)
next
Sync()
loop
end
Function StableZ(obj,nx#,ny#,nz#)
//stabilisation at Z Axis
Winkel#=ATan2(ny#,nx#)-90.0
print("z ground degree "+str(Winkel#))
If Abs(Winkel#)>1.0
SetObjectRotation( obj, GetObjectAngleX( obj ) , GetObjectAngley( obj ), Winkel# )
EndIf
EndFunction
Function StableX(obj,nx#,ny#,nz#)
//stabilisation at X Axis
Winkel#=ATan2(ny#,nz#)-90.0
print("x ground degree "+str(Winkel#))
If Abs(Winkel#)>1.0
SetObjectRotation( obj, -Winkel# , GetObjectAngley( obj ), GetObjectAnglez( obj ) )
EndIf
EndFunction
function printv(t$,v#)
print(t$ + " "+str(v#))
EndFunction
AGK 108 (B)19 + AppGameKit V2 Alpha .. : Windows 8.1 Pro 64 Bit : AMD Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)