OK, I modified the program. Now it works fine, but there's another problem: if I rem lines 33 and 34 instead 35 and 36 to change dice type from D10 to D4, after some dozens of rolls it goes through the bottom (or the cover if you press "Roll" button too frequently) and the dice falls down below the floor. It doesn't happen with D10 or any other dice type. I made a workaround that if y coordinate gets too small it creates another D4 in the right place, but it's "dirty" solution
Can anybody help me making it work right?
// Project: Dice
// Created: 2018-02-03
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Dice roll" )
SetWindowSize( 900, 750, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 900, 750 ) // 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
objTable=CreateObjectPlane(40,40)
SetObjectRotation(objTable,90,0,0)
SetObjectPosition(objTable,0,-30.0,0)
texBoard=loadImage("board.png")
SetObjectImage(objTable,texBoard,0)
objCover=CreateObjectPlane(30,30)
SetObjectRotation(objCover,90,0,0)
SetObjectPosition(objCover,-3,-6,0)
texCover=loadImage("cover.png")
SetObjectImage(objCover,texCover,0)
SetObjectTransparency(objCover,1)
`objDice=LoadObject("kostka2.obj")
`texDice=LoadImage("kostka.png")
objDice=LoadObject("k4.obj")
texDice=LoadImage("k4tex.jpg")
setObjectImage(objDice,texDice,0)
SetObjectScale(objDice,3.0,3.0,3.0)
SetObjectPosition(objDice,0,-8,3)
RotX=Random(1,180)
RotY=Random(1,180)
RotZ=Random(1,180)
SetObjectRotation(objDice,RotX,RotY,RotZ)
WallL=CreateObjectPlane(30,30)
SetObjectImage(WallL,texBoard,0)
SetObjectRotation(WallL,0,0,0)
SetObjectPosition(WallL,-3,-22,-15) `przesunięcia: x=góra/dół, z=lewo/prawo
WallR=CreateObjectPlane(30,30)
SetObjectRotation(WallR,0,0,0)
SetObjectImage(WallR,texBoard,0)
SetObjectPosition(WallR,-3,-22,15)
WallU=CreateObjectPlane(30,30)
SetObjectRotation(WallU,0,90,0)
SetObjectImage(WallU,texBoard,0)
SetObjectPosition(WallU,-18,-22,0)
WallD=CreateObjectPlane(30,30)
SetObjectRotation(WallD,0,90,0)
SetObjectImage(WallD,texBoard,0)
SetObjectPosition(WallD,12,-22,0)
Create3DPhysicsWorld()
Create3DPhysicsDynamicBody(objDice)
Create3DPhysicsStaticBody(objTable)
Create3DPhysicsStaticBody(WallD)
Create3DPhysicsStaticBody(WallU)
Create3DPhysicsStaticBody(WallR)
Create3DPhysicsStaticBody(WallL)
Create3DPhysicsStaticBody(objCover)
SetObjectShapeConvexHull(objDice)
SetObject3DPhysicsCanSleep(objDice,0)
SetSunActive( 1 )
SetAmbientColor( 0,0,0 )
SetCameraPosition (1,0,15,0)
SetCameraLookAt(1,0,0,0,0)
createpointlight (1, 10,-10,10,100,200,200,200)
createpointlight (2, -10,-10,10,100,200,200,200)
btnRoll as integer
btnRoll=1
AddVirtualButton(btnRoll,850,100,100)
SetVirtualButtonText(btnRoll,"Roll")
SetObject3DPhysicsAngularVelocity( objDice, RotX,RotY,RotZ, 720 )
Set3DPhysicsGravity( 0,-5,0)
do
Print( ScreenFPS() )
yDice#=getobjecty(objDice)
print ("YDice="+str(yDice#))
if GetVirtualButtonPressed(btnRoll)=1
repeat
RotX=Random(0,2)-1
RotY=Random(0,2)-1
RotZ=Random(0,2)-1
until RotX<>0 or RotY<>0 or RotZ<>0
SetObject3DPhysicsAngularVelocity( objDice, RotX,RotY,RotZ, 720 )
SetObject3DPhysicsLinearVelocity(objDice, 0,1,0,100)
endif
if GetVirtualButtonReleased( btnRoll ) =1 then Set3DPhysicsGravity( 0,-5,0)
Step3DPhysicsWorld()
Sync()
loop