I think what the engine does is registers a collision and then the physics responds
so getfirstcontact probably should be named something like getfirstcollision because
it is detecting the first collision and not the contact
this may not be the solution you want but works well for me
// Project: objectCollisionTest
// Created: 20-05-03
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "objectCollisionTest" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )
type myColor
r as integer
g as integer
b as integer
endtype
global hangerSwing_colorOff as myColor
hangerSwing_colorOff.r = 89
hangerSwing_colorOff.g = 100
hangerSwing_colorOff.b = 255
global hangerSwing_colorOn as myColor
hangerSwing_colorOn.r = 89
hangerSwing_colorOn.g = 227
hangerSwing_colorOn.b = 255
setUpWorld()
do
if GetRawKeyState(39)
moveDoor(5)
endif
if GetRawKeyState(37)
moveDoor(-5)
endif
if GetRawKeyState(32)
moveDoor(0)
endif
print("Left Arrow = Slide Door Left")
print("Right Arrow = Slide Door Right")
print("Spacebar = Stop Door Moving")
if checkCollision(checkBox,checkBox2)
SetObjectColor(hangerSwing, hangerSwing_colorOn.r, hangerSwing_colorOn.g, hangerSwing_colorOn.b, 255)
else
SetObjectColor(hangerSwing, hangerSwing_colorOff.r, hangerSwing_colorOff.g, hangerSwing_colorOff.b, 255)
endif
Step3DPhysicsWorld()
Sync()
loop
function moveDoor(getSpeed as float)
Set3DPhysicsSliderJointTargetLinearMotorVelocity(sliderJoint, getSpeed)
endfunction
function setUpWorld()
create3DPhysicsWorld(1)
SetGlobal3DDepth(100)
Set3DPhysicsGravity(0, -40, 0)
SetAmbientColor(50, 50, 50)
MoveCameraLocalY(1, 3)
global sliderRail as integer
sliderRail = CreateObjectBox(10, 0.5, 0.5)
SetObjectPosition(sliderRail, 0, 6, 0)
Create3DPhysicsStaticBody(sliderRail)
SetObjectColor(sliderRail, 150, 150, 150, 255)
global sliderDoor as integer
sliderDoor = CreateObjectBox(5, 5, 0.5)
SetObjectPosition(sliderDoor, -2.5, 3, 0)
Create3DPhysicsDynamicBody(sliderDoor)
SetObjectShapeBox(sliderDoor)
SetObjectColor(sliderDoor, 150, 150, 150, 255)
SetObject3DPhysicsCanSleep(sliderDoor, 0)
global checkBox as integer
checkBox = CreateObjectBox(1,2,2)
SetObjectPosition(checkBox,3, 1, 0)
FixObjectToObject(checkBox,sliderDoor)
SetObjectColor(checkBox,255,255,255,255)
SetObjectCollisionMode(checkBox,1)
SetObjectVisible(checkBox,0)
global sliderJoint as integer
sliderJoint = Create3DPhysicsSliderJoint(sliderRail, sliderDoor, CreateVector3(-3, 0, 0), CreateVector3(1, 0, 0))
Set3DPhysicsSliderJointPoweredLinearMotorIsEnabled(sliderJoint, 1)
Set3DPhysicsJointSliderLinearLimits(sliderJoint, 0, 6.5)
Set3DPhysicsSliderJointMaxLinearMotorForce(sliderJoint, 1)
global hangerBlock as integer
hangerBlock = CreateObjectBox(2, 2, 1)
SetObjectPosition(hangerBlock, 6.5, 9, 0)
Create3DPhysicsStaticBody(hangerBlock)
SetObjectColor(hangerBlock, 0, 11, 178, 255)
global hangerSwing as integer
hangerSwing = CreateObjectBox(0.5, 4, 0.5)
SetObjectPosition(hangerSwing, 6.5, 6.5, 0)
Create3DPhysicsDynamicBody(hangerSwing)
SetObjectShapeBox(hangerSwing)
SetObject3DPhysicsCanSleep(hangerSwing, 0)
SetObjectColor(hangerSwing, hangerSwing_colorOff.r, hangerSwing_colorOff.g, hangerSwing_colorOff.b, 255)
global checkBox2 as integer
checkBox2 = CreateObjectBox(2,3,1)
SetObjectPosition(checkBox2,0, -1, 0)
FixObjectToObject(checkBox2,hangerSwing)
SetObjectColor(checkBox2,255,255,255,255)
SetObjectCollisionMode(checkBox2,1)
SetObjectVisible(checkBox2,0)
global hingeJoint as integer
positionVect = CreateVector3()
rotationVect = CreateVector3(0,0,1.0)
posVestStatus = GetObjects3DPhysicsContactPositionVector(hangerSwing, hangerBlock, positionVect)
hingeJoint = Create3DPhysicsHingeJoint(hangerBlock, hangerSwing, positionVect, rotationVect, 1)
Set3DPhysicsJointHingeLimits(hingeJoint, -90, 0)
endfunction
//EDITED as I noticed I was using x when y and z calculations were needed
function checkCollision(objID as integer,objID2 as integer)
local width# as float
local height# as float
local depth# as float
local x# as float
local y# as float
local z# as float
local start_x# as float
local start_y# as float
local start_z# as float
local end_x# as float
local end_y# as float
local end_z# as float
local object_Hit as integer
//this checks for colliion between an object with any other object and returns its id
width#=(GetObjectMeshSizeMaxX(objID,1)-GetObjectMeshSizeMinX(objID,1))/2
height#=(GetObjectMeshSizeMaxY(objID,1)-GetObjectMeshSizeMinY(objID,1))/2
depth#=(GetObjectMeshSizeMaxZ(objID,1)-GetObjectMeshSizeMinZ(objID,1))/2
x#=getObjectWorldX(objID)
y#=GetObjectWorldY(objID)
z#=getObjectWorldZ(objID)
// calculate the start of the ray cast
start_x# = x#-width#
start_y# = y#+height#
start_z# = z#-depth#
// calculate the end of the ray cast
end_x# = x#+width#
end_y# = y#-height#
end_z# = z#+depth#
// determine which object has been hit
object_Hit = ObjectRayCast(objID2,start_x#,start_y#,start_z#,end_x#,end_y#,end_z#)
endfunction object_Hit