Hey guys,
I'm trying to find the center point between 2 (or more) 3d objects, but I'm missing something super basic.
I want to find the object that is furthest along the x axis, the object that is furthest along -x, and place another object equidistant between the two.
If you compile the attached code, you'll see by dragging the boxes around it sorta sometimes works - the sphere positions correctly if both boxes are -x and object 1 is 'above' object 0.
Basically I'm doing something stupid in the loop to find the bounds.
Can anyone help?
//find upper and lower bounds of 3d objects
SetWindowSize( GetDeviceWidth(), GetDeviceHeight(), 0 )
SetVirtualResolution ( GetDeviceWidth(), GetDeviceHeight() )
SetPrintSize(28.0)
SetPrintColor(255, 255, 0)
SetErrorMode(2)
UseNewDefaultFonts(1)
SetCameraFOV(1,20)
SetCameraRange( 1, 1, 50000 )
SetCameraRotation(1, 44, 48, 0)
SetCameraPosition(1, -680, 882, -600)
Create3DPhysicsWorld()
Set3DPhysicsGravity( 0, -30, 0 )
global ObjectID as objecttype[]
global centroid as integer
createArena()
createObject(30) //object 1
createObject(30) //object 2
//createObject(30) //object 3
PickJointSetup()
do
info()
pickJointCreateAndUpdate() //This function casts a ray, then creates a pick joint at that the contact point.
updateObjects()
findXCenterOfGroup()
Step3DPhysicsWorld() //required for updating physics.
sync()
loop
function info()
Print( "Click or tap on screen to cast ray and pick object" )
Print( "The goal is to have the centroid objects always be between the two boxs" )
Print( "and be equidistant." )
endfunction
function findXCenterOfGroup()
upperboundX# = 0.0
lowerboundX# = 0.0
for i = 0 to ObjectID.length
xPos# = getObjectWorldX(ObjectID[i].id)
if xPos# < -0.0 //negative X
if xPos# < lowerboundX# then lowerboundX# = xPos#
if xPos# < (upperboundX# *-1) then upperboundX# = xPos#
endif
//positive X number
if xPos# > 0.0 and xPos# > upperboundX# then upperboundX# = xPos#
if xPos# > 0.0 and xPos# > (lowerboundX# *-1) then lowerboundX# = xPos#
print(str(i) + " lowerboundX#: " + str(lowerboundX#,2))
print(str(i) + " upperboundX#: " + str(upperboundX#,2))
next i
centroidX# = (lowerboundX# + upperboundX#) /2
SetObjectPosition(centroid, CentroidX#, 0, 0)
print("centroidX#: " + str(CentroidX#,2))
endfunction
type objectType
id as integer
iText1 as integer
endtype
function updateObjects()
for i = 0 to ObjectID.length
x# = GetObjectWorldX( objectID[i].id )
z# = GetObjectWorldZ( objectID[i].id )
fScreenX# = GetScreenXFrom3D( x#, 0, z# )
fScreenY# = GetScreenYFrom3D( x#, 0, z# )
SetTextString(ObjectID[i].iText1, "obj[" + str(i) + "].X: " + str(x#,2))
SetTextPosition(ObjectID[i].iText1, fScreenX#, fScreenY# -28)
next i
endfunction
function createObject(boxSize as integer)
ObjectID.length = ObjectID.length + 1
iObject = ObjectID.length
ObjectID[iObject].id = CreateObjectBox(boxSize,boxSize,boxSize)
ObjectID[iObject].iText1 = CreateText("")
setTextSize(ObjectID[iObject].iText1, 38)
setTextColor(ObjectID[iObject].iText1, 255, 255,255, 255)
SetObjectPosition( ObjectID[iObject].id, random(-30,30), 200, random(-30,30) )
SetObjectColor(ObjectID[iObject].id, 32, 32, 200, 255)
Create3DPhysicsDynamicBody( ObjectID[iObject].id ) //set the physics body position (deletes and creates again)
SetObject3DPhysicsMass( ObjectID[iObject].id, 10.0 )
SetObject3DPhysicsFriction( ObjectID[iObject].id, 0.3 )
endfunction ObjectID[iObject].id
function createArena()
floorBoxID = CreateObjectBox( 1500.0, 12.0, 1500.0 )
SetObjectPosition(floorBoxID, 0.0, -6.0, 0.0 )
setObjectColor(floorBoxID, 128,32,32,255)
Create3DPhysicsStaticBody(floorBoxID) //A static physics body has no mass and does not get moved.
SetObjectShapeBox(floorBoxID)
//this should denote the center point between the two objects
centroid = CreateObjectSphere(20, 10, 10)
x_axis_Box = createObjectBox( 2, 2, 1500)
setObjectColor(x_axis_Box, 32, 32, 32, 16)
z_axis_Box = createObjectBox( 1500, 2, 2)
setObjectColor(z_axis_Box, 32, 32, 32, 16)
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
function PickJointCreateAndUpdate()
if GetPointerPressed() //Left mouse btn pressed creates pick joint
SetVector3( toVecID, Get3DVectorXFromScreen( GetPointerX(), GetPointerY() ),
Get3DVectorYFromScreen( GetPointerX(), GetPointerY() ),
Get3DVectorZFromScreen( GetPointerX(), GetPointerY() ) )
GetVector3Multiply( toVecID, 5500.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
if GetPointerState() = 1 and pickJointID > 0 //Left Mouse btn held down holds pick joint
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() ) )
pickJointID = 0
endif
endif
endfunction