back to working on
this breakout clone and experiencing some anomalies with 2d physics.
stripped-down code that:
> deactivates the 4 auto-generated walls and recreates them for better control (are the 4 automatic walls
always the same sprite IDs so we can tweak properties like normal ones?).
> creates and sets 50 bricks.
> sets the "ball" in motion
> (attempts to) count hits vs bricks only and marks them with small red boxes (while DEC the
total variable) & deletes the brick(s).
// Project: del sprite
// Created: 2019-09-15
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "del sprite" )
SetWindowSize( 1080, 720, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1080, 720 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 60, 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
SetVSync(1)
SetPhysicsThreading(-1)
SetPhysicsGravity(0,0)
SetPhysicsScale( 0.01 )
SetPhysicsDebugOn()
SetPhysicsWallBottom(0)
SetPhysicsWallTop(0)
SetPhysicsWallLeft(0)
SetPhysicsWallRight(0)
LWall = CreateSprite(0)
SetSpriteSize(LWall,1,720)
SetSpritePosition(LWall,-1,0)
SetSpritePhysicsOn(LWall,1)
SetSpritePhysicsFriction(LWall,0)
SetSpritePhysicsRestitution(LWall,1.0)
SetSpriteVisible(LWall,0)
RWall = CloneSprite(Lwall)
SetSpritePosition(RWall,1079,0)
SetSpritePhysicsOn(RWall,1)
SetSpritePhysicsFriction(RWall,0)
SetSpritePhysicsRestitution(RWall,1.0)
SetSpriteVisible(RWall,0)
TWall = CloneSprite(LWall)
SetSpriteSize(TWall,1080,1)
SetSpritePhysicsOn(TWall,1)
SetSpritePhysicsFriction(TWall,0)
SetSpritePhysicsRestitution(TWall,1.0)
SetSpriteVisible(TWall,0)
BWall = CloneSprite(TWall)
SetSpritePosition(BWall,0,720)
SetSpritePhysicsOn(BWall,1)
SetSpritePhysicsFriction(BWall,0)
SetSpritePhysicsRestitution(BWall,1.0)
SetSpriteVisible(BWall,0)
cols = 10
rows = 5
width = GetVirtualWidth()/cols
height = 50
total = 0
for x = 0 to cols-1
for y = 0 to rows-1
this = CreateSprite(0)
SetSpriteSize(this,width,height)
SetSpritePosition(this,x*width,y*height+height)
SetSpriteColor(this,random(0,255),random(0,255),random(0,255),255)
SetSpritePhysicsOn(this,1)
SetSpritePhysicsFriction(this,0)
SetSpritePhysicsRestitution(this,1.0)
total = total + 1
next y
next x
ball = CreateSprite(0)
SetSpriteSize(ball,24,24)
SetSpritePosition(ball,600,600)
SetSpritePhysicsOn(ball,2)
SetSpritePhysicsCanRotate(ball,0)
SetSpriteShapeCircle(ball,0,0,12)
SetSpritePhysicsVelocity(ball,600,600)
do
hit = GetSpriteFirstContact(ball)
if hit > 0
thishit = GetSpriteContactSpriteID2()
repeat
thisx = GetspriteContactWorldX() : thisy = GetspriteContactWorldY()
if thishit <> LWall and thishit <> RWall and thishit <> TWall and thishit <> BWall
DeleteSprite(thishit)
this = CreateSprite(0)
SetSpriteSize(this,9,9)
SetSpritePositionByOffset(this,thisx,thisy)
SetSpriteColor(this,255,0,0,255)
SetSpriteVisible(this,1)
total = total -1
endif
thishit = GetSpriteNextContact()
until thishit = 0
endif
Print( ScreenFPS() )
print(STR(this) + " @ " + STR(thisx) + " / " + STR(thisy))
print(total)
Sync()
loop
from this image:
1) not all contact with bricks are deleting the sprites (top-middle blue brick should have been deleted); red mark registered but brick is still there.
2) the hit that took out brick 8,0 bounced off the brick that is 1 down and 1 to the right but
did not register a hit, there.
3) ball contact with
other-than brick (non-walls) is registering (is it hitting the auto-generated wall that was de-activated?) - lower-right corner.
4)
total bricks remaining doesn't add up. total = 7 but 9 sprites visible. i've counted the red boxes = 43; the original 50-43 does = 7
this pic shows the 0,3 brick being hit twice (i watched it):
5) i'm assuming
DeleteSprite doesn't
fully delete it until sync() ?
as i'm writing this, i thought to go in and turn physics off on the sprite that's hit before deleting it by adding
SetSpritePhysicsOff(thishit) before
DeleteSprite(thishit) which, now, produces:
6) i'm assuming sprite 1 is the (deactivated) right wall? why was contact registered?
7) could SetPhysicsScale( 0.01 ) be the culprit in any/all of this?
i can build arrays and handle this stuff manually but things are not going as they should.
any help/thoughts is appreciated.
add last night when i started looking at counting brick hits i used
GetManagedSpriteCount() which seemed to always be accurate.
in my main version, there's a sound that plays on a brick hit. while i watched the ball, i would occasionally hear the sound when no brick was hit (i don't recall a physics reaction from it, tho).
i'd leave the room with the sound up and, again, occasional "hit" sound. upon return to the screen, my total (continues) into the negative...