The problem that I can see is that you need to take into account the scale factor for the physics world.
Your table is very small size for the default scale factor(40) of the physics world. Tunneling and other
strange behaviors will occur with and incorrect scale factor. I changed the scale factor to Create3DPhysicsWorld(15)
and it has better results. You will have to experiment to find the best scale factor.
I tweaked the gravity to Set3DPhysicsGravity(0, -2.0, 0.1) but you will have to experiment with these values as well.
I remarked adding restitution to the plunger it seemed to do fine with out it.
Also some times kinematic bodies can impart to much force to a dynamic object so be careful how much movement
you give to them.
I just posted the main file below since its the only changes I made.
// Project: pinball
// Created: 2017-12-12
#insert "types.agc"
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "pinball" )
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
SetWindowTitle( "3D Physics" )
SetWindowSize( 1024, 768, 0 )
SetPrintColor(0, 0, 0)
SetPrintSize(20)
Create3DPhysicsWorld(15)
gravity as point
gravity.y=-cos(0)*cos(-10)
gravity.x=sin(0)*cos(-10)
gravity.z=sin(10)
Set3DPhysicsGravity(0, -2.0, 0.1)
SetCameraPosition( 1, 0, 8, 11 )
SetCameraLookAt( 1, 0, 0, 0, 0 )
SetSkyBoxVisible(1)
SetSunDirection(0, -1, 0)
SetAmbientColor(0x40, 0x40, 0x40)
names as objinfo[]
names = LoadTable()
loadObjects(names)
global ball as integer
global rflipper as integer
global lflipper as integer
global plunger as integer
u as float
name as string
b as point
p as point
b.x = getobjectx(ball)
b.y = getobjecty(ball)
b.z = getobjectz(ball)
p.x = getobjectx(plunger)
p.y = getobjecty(plunger)
p.z = getobjectz(plunger)
shoot as integer = 0
do
u = (GetRawKeyState(KEY_UP) - GetRawKeyState(KEY_DOWN)) * .1
if GetRawKeyState(KEY_SHIFT) = 1
SetCameraPosition(1, GetCameraX(1), GetCameraY(1), getcameraZ(1)-u)
else
SetCameraPosition(1, GetCameraX(1), GetCameraY(1)+u, getcameraZ(1))
endif
u = (GetRawKeyState(KEY_LEFT) - GetRawKeyState(KEY_RIGHT)) * .1
SetCameraPosition(1, GetCameraX(1) + u, GetCameraY(1), getcameraZ(1))
SetCameraLookAt( 1, 0, 0, 0, 0 )
if GetRawKeyReleased(KEY_F) = 1
SetObjectRotation(lflipper, 0, 30, 0)
SetObjectRotation(rflipper, 0, -30, 0)
endif
if GetRawKeyPressed(KEY_F) = 1
RotateObjectLocalY(rflipper, 30)
RotateObjectLocalY(lflipper, -30)
endif
if GetRawKeyReleased(KEY_P) = 1
SetObject3DPhysicsLinearVelocity( ball, 0, 0, -1, 20)
endif
if GetRawKeyPressed(KEY_S) = 1
//SetObject3DPhysicsRestitution(plunger, 8.0)
SetObjectPosition(plunger, getobjectx(plunger), getobjecty(plunger), getobjectz(plunger) - .4)
endif
if GetRawKeyReleased(KEY_S) = 1
//SetObject3DPhysicsRestitution(plunger, 0.0)
SetObjectPosition(plunger, p.x, p.y, p.z)
endif
if GetRawKeyReleased(KEY_SPACE) = 1
if b.y <> getobjecty(ball)
Delete3DPhysicsBody(ball)
SetObjectPosition(ball, b.x, b.y, b.z)
else
Create3DPhysicsDynamicBody(ball)
SetObjectShapeSphere(ball)
SetObject3DPhysicsRestitution(ball, 1)
endif
endif
print(str(ScreenFPS())+" SPACE-Make ball a dynamic object, S-Plunger, F-Flippers, P-Angular velocity")
Sync()
Step3DPhysicsWorld()
loop
function DeleteObjects(names as objinfo[])
i as integer
n as string
for i=0 to names.length
n = getFileName(names[i].name)
if mid(n,1,5) <> "floor" and mid(n,1,4) <> "ball"
DeleteObject(names[i].o)
endif
next
endfunction
function LoadObjects(names ref as objinfo[])
i as integer
n as string
o as integer
g as integer
lf as integer
rf as integer
p as point
a as float
for i=0 to names.length
if names[i].group = TABLE_GROUP_FLOOR
g = LoadObject(names[i].name)
names[i].o = g
SetObjectColor(g, 0x30, 0x30, 0x30, 0xff)
endif
next
for i=0 to names.length
n = getFileName(names[i].name)
if names[i].group = TABLE_GROUP_FLOOR
else
o = LoadObject(names[i].name)
names[i].o = o
select names[i].group
case TABLE_GROUP_BALL
SetObjectColor(o, 0, 0xf0, 0x00, 0xff)
ball = o
endcase
case TABLE_GROUP_PIVOT
if mid(n,1,7) = "fpivot2"
rflipper = names[i].o
else
lflipper = names[i].o
endif
SetObjectColor(o, 0x0, 0xff, 0, 0xff)
endcase
case TABLE_GROUP_TABLE
SetObjectColor(o, 0x70, 0x70, 0x70, 0xff)
endcase
case TABLE_GROUP_BUMPER
SetObjectColor(o, 0xff, 0x40, 0x40, 0xff)
endcase
case TABLE_GROUP_FLIPPER
SetObjectColor(o, 0xff, 0x40, 0x40, 0xff)
if mid(n,1,8) = "flipper2"
rf = names[i].o
else
lf = names[i].o
endif
endcase
case TABLE_GROUP_TARGET
SetObjectColor(o, 0x40, 0x40, 0xff, 0xff)
endcase
case TABLE_GROUP_PLUNGER
SetObjectColor(o, 0x40, 0xff, 0x40, 0xff)
endcase
endselect
ResetOrigin(names[i])
endif
next
rflipper = SetPivot(rf, rflipper)
lflipper = SetPivot(lf, lflipper)
for i=0 to names.length
n = getFileName(names[i].name)
if mid(n,1,4) <> "ball"
if mid(n,1,7) = "plunger"
plunger = names[i].o
Create3DPhysicsKinematicBody(plunger)
SetObjectShapeCylinder(plunger, 2)
elseif mid(n,1,7) = "flipper"
Create3DPhysicsKinematicBody(names[i].o)
SetObjectShapeConvexHull(names[i].o)
SetObject3DPhysicsRestitution(names[i].o, 4.0)
elseif mid(n,1,6) = "fpivot"
else
Create3DPhysicsStaticBody(names[i].o)
endif
endif
next
RotateObjectLocalY(rflipper, -20)
RotateObjectLocalY(lflipper, 20)
endfunction
function SetPivot(obj as integer, toobj as integer)
t as point
f as point
f.x = GetObjectX(obj)
f.y = GetObjectY(obj)
f.z = GetObjectZ(obj)
t.x = GetObjectX(toobj)
t.y = GetObjectY(toobj)
t.z = GetObjectZ(toobj)
SetObjectPosition(obj, f.x - t.x, f.y - t.y, f.z - t.z)
FixObjectPivot(obj)
SetObjectPosition(obj, t.x, t.y, t.z)
DeleteObject(toobj)
endfunction obj
function ResetOrigin(obj as objinfo)
o as integer
o = obj.o
SetObjectPosition(o, getobjectX(o) - obj.b.center.x, getobjectY(o) - obj.b.center.y, getobjectZ(o) - obj.b.center.z)
FixObjectPivot(o)
SetObjectPosition(o, obj.b.center.x, obj.b.center.y, obj.b.center.z)
endfunction
function getFileName(name as string)
i as integer
n as string
i = CountStringTokens2(name, "/")
n = GetStringToken(name, "/", i)
endfunction n
function Unfix(o as objinfo, g as integer)
p as point
a as point
p.x = GetObjectWorldX(o.o)
p.y = GetObjectWorldY(o.o)
p.z = GetObjectWorldZ(o.o)
a.x = GetObjectAngleX(g)
a.y = GetObjectAngleY(g)
a.z = GetObjectAngleZ(g)
FixObjectToObject(o.o, 0)
SetObjectPosition(o.o, p.x, p.y, p.z)
SetObjectRotation(o.o, a.x, a.y, a.z)
endfunction
#insert "loadtable.agc"
The coffee is lovely dark and deep,and I have code to write before I sleep.