Compound shape size values are half the actual size of the shape.
Run the following code. Click to shoot the ball. You will see there's an invisible halo around the shapes. This is because the hit shape sizes match the size of the 3D shape.
If you change the line;
scale=SCALE_1
to
scale=SCALE_HALF
This will half the size values and it will work as expected.
Note that this applies ONLY to the size values. The position and rotation values are correct
I can see that fixing this would create problems for those apps that already take this into account but i think that a note in the documentation would help out a lot
// Project: test20
// Created: 2019-01-06
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "test20" )
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 ) // since version 2.0.22 we can use nicer default fonts
create3DPhysicsWorld(2)
Set3DPhysicsGravity(0, -1, 0)
SetCameraRange( 1, 1, 50000 )
type point
x as float
y as float
z as float
endtype
#constant BOX__WIDTH=5
#constant BOX_HEIGHT=5
#constant BOX_DEPTH=1
#constant CYL_DIAMETER=5
#constant CYL_HEIGHT=1
#constant SCALE_1=1
#constant SCALE_HALF=0.5
scale as float = SCALE_1
box = CreateObjectBox(BOX__WIDTH, BOX_HEIGHT, BOX_DEPTH)
SetObjectColor(box, 0, 0xff, 0, 0xff)
SetObjectPosition(box, -5, 0, 0)
Create3DPhysicsKinematicBody(box)
SetObjectShapeCompound(box)
pos = CreateVector3(0, 0, 0)
rot = CreateVector3(0, 0, 0)
size = CreateVector3(BOX__WIDTH * scale, BOX_HEIGHT * scale, BOX_DEPTH * scale)
AddObjectShapeBox(box, pos, rot, size)
cyl = CreateObjectCylinder(1, CYL_DIAMETER, 16)
RotateObjectLocalX(cyl, 90)
FixObjectPivot(cyl)
SetObjectColor(cyl, 0, 0xff, 0xff, 0xff)
Create3DPhysicsKinematicBody(cyl)
SetObjectShapeCompound(cyl)
pos = CreateVector3(0, 0, 0)
rot = CreateVector3(90, 0, 0)
size = CreateVector3(CYL_DIAMETER * scale, CYL_HEIGHT * scale, CYL_DIAMETER * scale)
AddObjectShapeCylinder(cyl, pos, rot, size, 1)
SetObjectPosition(cyl, 5, 0, 0)
ball = CreateObjectSphere(1, 8, 8)
SetObjectColor(ball, 0xff, 0, 0, 0xff)
SetObjectVisible(ball, 0)
SetCameraPosition(1, 0, 0, 15)
SetCameraLookAt(1, 0, 0, 0, 0)
do
if GetPointerPressed() = 1
Shoot(ball)
endif
Step3DPhysicsWorld()
Print( str(ScreenFPS())+" Scale is "+str(scale))
Sync()
loop
function Shoot(ball as integer)
f as point
t as point
v as point
b as integer
x as float
y as float
m as float
x = GetPointerX()
y = GetPointerY()
f.x = Get3DVectorXFromScreen( x, y ) + GetCameraX(1)
f.y = Get3DVectorYFromScreen( x, y ) + GetCameraY(1)
f.z = Get3DVectorZFromScreen( x, y ) + GetCameraZ(1)
t.x = Get3DVectorXFromScreen( x, y ) * 2 + GetCameraX(1)
t.y = Get3DVectorYFromScreen( x, y ) * 2 + GetCameraY(1)
t.z = Get3DVectorZFromScreen( x, y ) * 2 + GetCameraZ(1)
v.x = t.x - f.x : v.y = t.y - f.y : v.z = t.z - f.z
b = CloneObject(ball)
SetObjectVisible(b, 1)
SetObjectPosition(b, f.x, f.y, f.z)
SetObjectLookAt(b, t.x, t.y, t.z, 0)
Create3DPhysicsDynamicBody(b)
SetObjectShapeSphere(b, 1)
SetObject3DPhysicsCanSleep( b, 0 )
SetObject3DPhysicsLinearVelocity(b, v.x, v.y, v.z, 20)
endfunction