This is what I do for picking an object using ray casting with physics.
The code below is the raycasting demo I posted here.
https://forum.thegamecreators.com/thread/216683
Look at the PickJointCreateAndUpdate() function, I get the screen coordinates which are returned as a directional vector,
then turn it into a point in 3d space.
// Project: Raycasting
// Created: 2016-05-25 by Stab In The Dark Software
// set window properties
SetWindowTitle( "Raycasting" )
SetWindowSize( GetDeviceWidth(), GetDeviceHeight(), 0 )
//Set display properties
SetVirtualResolution ( GetDeviceWidth(), GetDeviceHeight() )
SetVSync( 1 )
//Set Backdrop color
SetClearColor( 0,170,204 ) // light blue
//Set display orientation for mobile device
SetOrientationAllowed( 0, 0, 1, 0 )
SetScissor( 0, 0, 0, 0 )
//Set Lighting
//SetSunDirection( 0, -1, 1 )
CreatePointLight( 1, 0, 200, 0, 1000, 255, 255, 255 )
//SetAmbientColor( 60, 60, 60 )
//Set camera
SetCameraRange( 1, 1, 50000 )
SetCameraPosition( 1, 0, 100.0, -350.0 )
SetCameraLookAt( 1, 0.0, 100.0, 0.0, 0.0 )
//Set Text size and color as displayed to screen.
SetPrintSize( 22.0 )
SetPrintColor( 255, 255, 0 )
SetSkyBoxVisible( 1 )
//Set the error reporting for debugging.
//SetErrorMode( 2 )
DisplayLogo()
Create3DPhysicsWorld()
global FPS as integer
global floorBoxID as integer
global floorTexID as integer
global ballTex as integer
global crateTex as integer
global concreteTex as integer
LoadDemoTextures()
//Make Physics floor
floorBoxID = CreateObjectBox( 1000.0, 12.0, 1000.0 )
SetObjectImage( floorBoxID, floorTexID, 0 )
//SetObjectColor( floorBoxID, 112, 100, 100, 255 )
SetObjectLightMode( floorBoxID, 1 )
SetObjectPosition( floorBoxID, 0.0, 6.0, 0.0 )
//This command creates a static physics body for the floor box with a triangle mesh collision shape.
//A static physics body has no mass and does not get moved.
Create3DPhysicsStaticBody( floorBoxID )
//This 3D Physics command changes the collision shape to a primative box shape since we do not need the accuracy of
// polygon collision for just a box. The physics body is still static.
SetObjectShapeBox( floorBoxID ) //
//Create a stack of dynamic boxes
CreateBoxStack( 17.0, 6, 20, -10.0, 0.0 )
CreateBoxStack( 17.0, 3, 20, -10.0, -100.0 )
PickJointSetup()
do
//This function casts a ray, then creates a pick joint at that the contact point.
PickJointCreateAndUpdate()
FPS = ScreenFPS()
PrintC( "Fame Rate: " ) : Print( FPS )
PrintC( "Total Physics Objects: " ) : Print( Get3DPhysicsTotalObjects() )
PrintC( "Active Physics Objects: " ) : Print( Get3DPhysicsActiveObjects() )
Print( "Click or tap on screen to cast ray and pick object" )
Step3DPhysicsWorld() //required for updating physics.
Sync()
loop
function DisplayLogo()
bulletLogo = LoadImage("/media/agk2_logokit/bullet_logo_small.png")
SetImageWrapU( bulletLogo,1 )
SetImageWrapV( bulletLogo,1 )
bulletSprite = CreateSprite( bulletLogo )
SetSpritePosition( bulletSprite, GetScreenBoundsRight() -310.0, GetScreenBoundsTop() )
agkLogo = LoadImage("/media/agk2_logokit/Made-With-AGK-128px.png")
SetImageWrapU( agkLogo,1 )
SetImageWrapV( agkLogo,1 )
agkSprite = CreateSprite( agkLogo )
SetSpritePosition( agkSprite, GetScreenBoundsRight() -128.0, GetScreenBoundsTop() )
endfunction
function LoadDemoTextures()
floorTexID = LoadImage( "/media/steelPanelTiled.png" )
SetImageWrapU( floorTexID, 0 )
SetImageWrapV( floorTexID, 0 )
ballTex = LoadImage( "/media/soccerball2.jpg" )
SetImageWrapU( ballTex, 1 )
SetImageWrapV( ballTex, 1 )
crateTex = LoadImage( "/media/Crate_color.png" )
SetImageWrapU( crateTex, 1 )
SetImageWrapV( crateTex, 1 )
concreteTex = LoadImage( "/media/Concrete.png" )
SetImageWrapU( concreteTex, 1 )
SetImageWrapV( concreteTex, 1 )
endfunction
Function CreateBoxStack( boxSize as float, rows as integer, columns as integer , yPosition as float, zPosition as float)
objID as integer
for h = 1 to rows
for i = 1 to columns
objID = CreateObjectBox( boxSize, boxSize, boxSize )
SetObjectImage( objID, crateTex, 0 )
SetObjectPosition( objID, ( boxSize * columns / 2.0 - boxSize / 2.0 ) * -1.0 + ( boxSize * i ), boxSize / 2.0 + ( boxSize * h ) + yPosition, zPosition )
Create3DPhysicsDynamicBody( objID ) //This command by default gives the body a box collision shape so no need to change it.
SetObject3DPhysicsMass( objID, 10.0 )
SetObject3DPhysicsFriction( objID, 0.3 )
next i
next h
endfunction
function PickJointCreateAndUpdate()
//Left mouse btn pressed creates pick joint
if GetPointerPressed()
SetVector3( toVecID, Get3DVectorXFromScreen( GetPointerX(), GetPointerY() ),
Get3DVectorYFromScreen( GetPointerX(), GetPointerY() ),
Get3DVectorZFromScreen( GetPointerX(), GetPointerY() ) )
GetVector3Multiply( toVecID, 1500.0 )
SetVector3( origPosCamVecID, GetCameraX(1), GetCameraY(1), GetCameraZ(1) )
GetVector3Add( toVecID, origPosCamVecID )
RayCast3DPhysics( pickObjectRayID, origPosCamVecID, toVecID, 1 )
HitObjID = Get3DPhysicsRayCastObjectHit( pickObjectRayID, 0 )
if HitObjID > 0
Get3DPhysicsRayCastContactPosition( pickObjectRayID, 0.0, PickVecID )
pickJointID = Create3DPhysicsPickJoint( HitObjID, PickVecID )
endif
endif
//Left Mouse btn held down holds pick joint
if GetPointerState() and pickJointID > 0
SetVector3( updatedPickVecID, Get3DVectorXFromScreen( GetPointerX(), GetPointerY() ),
Get3DVectorYFromScreen( GetPointerX(), GetPointerY() ),
Get3DVectorZFromScreen( GetPointerX(), GetPointerY() ) )
origDistance = GetVector3Distance( origPosCamVecID, PickVecID )
GetVector3Multiply( updatedPickVecID, origDistance )
SetVector3( currPosCamVecID, GetCameraX(1), GetCameraY(1), GetCameraZ(1) )
GetVector3Add( updatedPickVecID, currPosCamVecID )
Update3DPhysicsPickJoint( pickJointID, updatedPickVecID )
elseif pickJointID > 0
Delete3DPhysicsPickJoint( pickJointID )
if HitObjID > 0
SetVector3( updatedPickVecID, Get3DVectorXFromScreen( GetPointerX(), GetPointerY() ),
Get3DVectorYFromScreen( GetPointerX(), GetPointerY() ),
Get3DVectorZFromScreen( GetPointerX(), GetPointerY() ) )
SetObject3DPhysicsLinearVelocity( HitObjID, updatedPickVecID, 5000.0)
pickJointID = 0
endif
endif
endfunction
function PickJointSetup()
global pickObjectRayID as integer
global currPosCamVecID as integer
global origPosCamVecID as integer
global toVecID as integer
global updatedPickVecID as integer
global PickVecID as integer
global HitObjID as integer
global pickJointID as integer
pickObjectRayID = Create3DPhysicsRay()
currPosCamVecID = CreateVector3()
origPosCamVecID = CreateVector3()
toVecID = CreateVector3()
PickVecID = CreateVector3()
updatedPickVecID = CreateVector3()
endfunction
The coffee is lovely dark and deep,and I have code to write before I sleep.