I'm having a major lapse in mathematic ability.
Hopefully someone can help me out.
I'm trying to determine the minimum and maximum Y offset allowed for the following example. The goal is to make it so that the board can move half of its height plus the height of one tile. I seem to have the calculation for max and min for the x offset OK, but y is not cooperating. I can fudge it, but I'd like to understand the calculation so that I can use different zoom levels.
Here's the code (offending lines are 150 and 153):
global gTapTime = 0
global gOldY# = 0.0
base = createSprite(0)
SetSpritePosition(base , -200,-200)
w# = 100.0 / 15.0
SetSpriteSize(base , w# , -1)
h# = GetSpriteHeight(base)
startX# = 0
startY# = 50.0 - 0.5 * 15.0 * h#
boardEnd# = 15.0 * h# + startY#
gBoardH# = h# * 15.0
dim boardSprites[225]
for c = 1 to 15
for r = 1 to 15
inc counter
x# = (c-1) * w#
y# = (r-1) * h# + startY#
thisSprite = CloneSprite(base)
SetSpriteSize(thisSprite , w# , h#)
SetSpriteColor(thisSprite , random(0,255) , random(0,255) , random(0,255) , 255)
SetSpritePosition(thisSprite , x# , y#)
SetSpriteScissor(thisSprite , 0.0 , startY# , 100.0 , boardEnd#)
boardSprites[counter] = thisSprite
next r
next c
dim playerHand[7]
phY# = boardEnd# + 0.5 * h#
for i = 1 to 7
thisSprite = CreateSprite(0)
SetSpriteSize(thisSprite , (100.0 / 7.7) , -1)
phX# = (i-1) * (100.0 / 7.0 + 0.1) + 0.5 * 0.7
SetSpritePosition(thisSprite , phX# , phY#)
SetSpriteColor(thisSprite , 255 , 255 , 255 , 255)
FixSpriteToScreen(thisSprite , 1)
playerHand[i] = thisSprite
next i
SetViewZoomMode(1)
cameraX# = 0.0
cameraY# = 0.0
oldX# = 0.0
oldY# = 0.0
x# = 0.0
y# = 0.0
do
Print(GetPointerY())
Print(cameraX#)
Print(cameraY#)
Print(minCamY#)
ptrY# = GetPointerY()
ptrX# = GetPointerX()
worldPtrY# = ScreenToWorldY(ptrY#)
worldPtrX# = ScreenToWorldX(ptrX#)
if GetPointerPressed() = 1
z# = GetViewZoom()
phHit = 0
for i = 1 to 7
phSprite = playerHand[i]
if GetSpriteHitTest(phSprite , worldPtrX# , worldPtrY#) = 1
repeat
SetSpritePosition(phSprite , GetPointerX() , GetPointerY())
Sync()
until GetPointerReleased() = 1
phHit = 1
EXIT
endif
next i
if ptrY# < boardEnd# and ptrY# > startY# and phHit = 0
timeNow = GetMilliseconds()
if timeNow - gTapTime > 0 and timeNow - gTapTime <= 500
if z# > 1.0
zoom# = 1.0
//needs tweening
SetViewOffset(0.0 , 0.0)
cameraX# = 0.0
cameraY# = 0.0
oldX# = 0.0
oldY# = 0.0
else
zoom# = 2.0
endif
zTime# = 0.25
dZ# = zoom# - z#
done = 0
repeat
ft# = GetFrameTime()
z# = z# + dZ# / zTime# * ft#
if (z# >= zoom# and dZ# > 0) or (z# <= zoom# and dZ# < 0)
z# = zoom#
done = 1
endif
top# = 50.0 - 0.5 * 15.0 * h# / z#
bottom# = top# + 15.0 * h# / z#
for i = 1 to 225
thisSprite = boardSprites[i]
SetSpriteScissor(thisSprite , 0.0 , top# , 100.0 , bottom# )
next i
SetViewZoom(z#)
Sync()
until done = 1
timeNow = 0
endif
gTapTime = timeNow
endif
endif
if zoom# > 1.0 and ptrY# < boardEnd# and ptrY# > startY#
if GetPointerState() = 1
x# = oldX# - ptrX# / z#
y# = oldY# - ptrY# / z#
else
x# = 0.0
y# = 0.0
endif
oldX# = ptrX# / z#
oldY# = ptrY# / z#
cameraX# = cameraX# + x#
cameraY# = cameraY# + y#
minCamX# = -50.0 + 0.5 * 15.0 * w# / z# - w#
if cameraX# < minCamX# then cameraX# = minCamX#
maxCamX# = 50.0 - 0.5 * 15.0 * w# / z# + w#
if cameraX# > maxCamX# then cameraX# = maxCamX#
minCamY# = -0.5 * 15.0 * h# / z# + h# * z#
if cameraY# < minCamY# then cameraY# = minCamY#
maxCamY# = 0.5 * 15.0 * h# / z# - h# * z#
if cameraY# > maxCamY# then cameraY# = maxCamY#
SetViewOffset(cameraX# , cameraY#)
top# = 50.0 - 0.5 * 15.0 * h# / z# + cameraY#
bottom# = top# + 15.0 * h# / z#
for i = 1 to 225
thisSprite = boardSprites[i]
SetSpriteScissor(thisSprite , 0.0 , top# , 100.0 , bottom# )
next i
endif
Sync()
loop
My current (incorrect) calculation for max and min camera Y offsets are:
minCamY# = -0.5 * 15.0 * h# / z# + h# * z#
and
maxCamY# = 0.5 * 15.0 * h# / z# - h# * z#
Where h# is the height of one of the game board tiles.
And z# is the zoom level.
All help is greatly appreciated!