I'm trying to drag stuff around in VR space, using
FixObjectToObject()
The command lets an objects position and rotation be relative to another object, instead of being relative to the world. So if the object is positioned slightly above ground level, the object will suddenly be placed slightly above the parent object, with "above" being a direction determined by the parent objects rotation.
Getting the fixed object to keep its original world position is a bit tricky. I tried this:
old_pos_X = GetObjectX (vBox)
old_pos_Y = GetObjectY (vBox)
old_pos_Z = GetObjectZ (vBox)
FixObjectToObject( vBox, leftControllerObject )
SetObjectPosition( vBox, old_pos_X - GetObjectX (leftControllerObject), old_pos_Y - GetObjectY (leftControllerObject), old_pos_Z - GetObjectZ (leftControllerObject))
And this works as long as leftControllerObject is not rotated at all. But as soon as it is rotated, things gets weird.
I tried my best, but I'm truly lost. Any clues to where to go from here?
//******************************************************
// MOVE_A_BOX
//******************************************************
//Load the AGKVR plugin
#import_plugin AGKVR
//Init App
SetWindowTitle( "move_a_box" )
SetSyncRate(0, 0)
SetWindowSize( 1024, 768, 0 )
SetScissor(0, 0, 0, 0)
UseNewDefaultFonts( 1 )
/*===============================================================================
VR TECH STUFFIE
===============================================================================*/
//Set the Camera Range in AGKVR
//It is necessary to use this command for setting the camera's range instead of the standard AGK SetCameraRange command
AGKVR.SetCameraRange( 0.01, 1000.0 )
//Initialiaze AGKVR
// The parameters are the Right and Left Eye image ID's that will be used to render to the HMD
InitError As Integer
RightEyeImg As Integer = 500
LeftEyeImg As Integer = 501
InitError = AGKVR.Init( RightEyeImg, LeftEyeImg )
// InitError = 0: SUCCESS!
// InitError = 1: Unable to init VR runtime
// InitError = 2: Compositor initialization failed.
//This command will lock the player's heading direction
//to follow the turn angle of the HMD. This would be common in FPS games, where
//you want the forward moving direction's turn angle to change based on where the
//player is looking. LockPlayerTurn is ON(1) by default
AGKVR.LockPlayerTurn( 1 )
//This command will lock the player's heading direction (forward on the Z axis)
//to follow the pitch angle of the HMD. This would be useful in a freeflight style game
//where you want the forward moving direction's pitch angle to change based on where the
//player is looking. LockPlayerPitch is OFF(0) by default
AGKVR.LockPlayerPitch( 0 )
/*===============================================================================
ADD STUFF
===============================================================================*/
SetClearColor(64, 64, 128)
// This is the box I move around
vBox = CreateObjectBox (0.08, 0.11, 0.17)
SetObjectColor(vBox, 128, 128, 128, 255)
SetObjectPosition( vBox, 0.0, 1.4, 0.4)
// This is simply an object that sticks to the position and rotation of the left Touch controller
leftControllerObject = CreateObjectBox (0.03, 0.03, 0.03)
SetObjectColor(leftControllerObject, 0, 255, 255, 255)
old_pos_X as float = 0.0
old_pos_Y as float = 0.0
old_pos_Z as float = 0.0
old_angle_X as float = 0.0
old_angle_Y as float = 0.0
old_angle_Z as float = 0.0
#constant FALSE = 0
#constant TRUE = 1
A_down as integer = FALSE
B_down as integer = FALSE
do
if GetRawKeyPressed(27) //Esc
End //Close App
endif
// Let the object follow the left controller
SetObjectRotation( leftControllerObject, AGKVR.GetLeftHandAngleX(), AGKVR.GetLeftHandAngleY(), AGKVR.GetLeftHandAngleZ() )
SetObjectPosition( leftControllerObject, AGKVR.GetLeftHandX(), AGKVR.GetLeftHandY(), AGKVR.GetLeftHandZ())
// Press A to fix the position to the left hand
if AGKVR.RightController_Button2() = 1 and A_down = FALSE // A BUTTON
old_pos_X = GetObjectX (vBox)
old_pos_Y = GetObjectY (vBox)
old_pos_Z = GetObjectZ (vBox)
old_angle_X = GetObjectAngleX (vBox)
old_angle_Y = GetObjectAngleY (vBox)
old_Angle_Z = GetObjectAngleZ (vBox)
FixObjectToObject( vBox, leftControllerObject )
SetObjectPosition( vBox, old_pos_X - GetObjectX (leftControllerObject), old_pos_Y - GetObjectY (leftControllerObject), old_pos_Z - GetObjectZ (leftControllerObject))
// How do I keep the rotation and position here???
A_down = TRUE
else
if AGKVR.RightController_Button2() = 0 then A_down = FALSE
endif
// Press B to unfix the position and return to its initial position
if AGKVR.RightController_Button1() = 1 and B_down = FALSE // B BUTTON
FixObjectToObject( vBox, 0 )
SetObjectPosition( vBox, 0.0, 1.4, 0.4)
SetobjectRotation( vBox, 0.0, 0.0, 0.0)
// How do I keep the rotation and position here???
B_down = TRUE
else
if AGKVR.RightController_Button1() = 0 then B_down = FALSE
endif
AGKVR.UpdatePlayer( )
//This command renders to the HMD.
AGKVR.Render( )
//Sync to update the monitor display (the HMD display is updated through AGKVR's Render Command
//so this Sync is not necessary to for rendering to the HMD)
Sync()
loop