I seem to be missing something fundamental about how gravity physics work? The following code drops rows of rectangular sprites from the top of the screen while drawing a series of horizontal bars up the screen indicating where the top of each row of sprites should be:
// set window properties
SetWindowTitle( "Gravity Test" )
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 )
#Include "Game.scene"
Game_SetUp()
CreateImageColor(1, 255, 0, 0, 255)
CreateImageColor(2, 0, 255, 0, 255)
CreateImageColor(3, 0, 0, 255, 255)
SetPhysicsDebugOn()
do
intRow as Integer
intCol as Integer
If intRow < 16
For intRow = 0 to 15 // 9 really bounces
For intCol = 0 to 16
intSpriteID = intSpriteID + 1
CreateSprite(intSpriteID + 1000, 3) // creating blue, horizontal bars to show where the top of each sprite should be
SetSpriteDepth(intSpriteID + 1000, 0)
SetSpriteSize(intSpriteID + 1000, 800, 1)
SetSpritePosition(intSpriteID + 1000, 1, 768 - (intRow * 41))
If intRow = 0 or intRow = 2 or intRow = 4 or intRow = 6 or intRow = 8 or intRow = 10 or intRow = 12 or intRow = 14 or intRow = 16
CreateSprite(intSpriteID, 1)
Else
CreateSprite(intSpriteID, 2)
EndIf
SetSpriteSize(intSpriteID, 40, 41)
SetSpriteDepth(intSpriteID, 10000)
SetSpritePhysicsOn(intSpriteID, 2)
SetSpritePhysicsCanRotate(intSpriteID, 0)
SetSpritePhysicsRestitution(intSpriteID, 0)
SetSpritePhysicsMass(intSpriteID, 0)
SetSpritePosition(intSpriteID, (intCol * 44) + 25, 1) // Random(1,5))
Next
intFall as integer
For intFall = 1 to 35 // allow the falling sprites to clear staging
Sync()
Next
Next
For intFall = 1 to 80 // allow remaining falling sprites to finish falling
Sync()
Next
EndIf
intSleep = intSleep + 1
SetWindowTitle("Sleep: " + Str(intSleep)) // <<< - watch for the title bar to change - sprites start moving around the 12th iteration
Sync() // debugging
Sleep(500) // comment out this line and the sprites no longer overlap - Sleep(1) does not appear to cause the issue
loop
But this code is producing unexpected results. As illustrated below, the blue horizontal lines indicate where the tops of each row of sprites should be when PhysicsHorizontalY~0, and everything looks pretty good once all of the stacks have stopped bouncing (despite restitution=0).
But then around the 13th time it runs the Sleep(200) statement the pile of sprites starts to sag, with varying degrees of vertical compression per row but all of the sprites in each row behaving the same way:
By iteration 23 the stacks have sagged by more than two rows - of sprites, not pixels:
They bounce up and down a few more times and still have not recovered their original positions by the 100th iteration:
BUT...simply comment out Sleep(500) and the sprites remain rooted in place to at least a count of 1000. Sleep thus appears to be impacting physics calculations in some way that causes the sprites to suddenly begin moving when no force has been applied after several seconds. It is unclear why this should be the case but it would be helpful to use something like Sleep without impacting physics like this.
Has anybody else noticed this? Any suggestions?