A very easy function which will align a pixel on the world plane with a pixel on the display plane at a given zoom level.
function AlignWorld( worldX# , worldY# , displayX# , displayY#, worldZ# )
setViewZoom( worldZ# )
setViewOffset( worldX# - ( displayX# / worldZ# ) , worldY# - ( displayY# / worldZ# ) )
endfunction
Intended to enhance setviewoffset()
For example, if you have a sprite on the world plane and you'd like to center the display on it, use;
AlignWorld( getSpriteXByOffset( sprite ) ,getSpriteYByOffset( sprite ) , getVirtualWidth() * 0.5 , getVirtualHeight() * 0.5 , 1.0 ).
For a better example, here a little app which moves four sprites around the screen. At the bottom of the screen are 4 smaller windows - each of which will stay locked on one of the sprites at varying zoom levels
// Set up screen
screenWidth = getDeviceWidth()
screenHeight = getDeviceHeight()
setVirtualResolution( screenWidth , screenHeight )
sync()
// Set up Test Sprites
SpriteSize = screenWidth * 0.125
dim sprite[ 4 ]
thisSprite = createSprite( 0 )
setSpriteSize( thisSprite , screenWidth , screenHeight )
setSpriteColor( thisSprite , 0 , 0 , 127 , 255 )
fixSpriteToScreen( thisSprite , 1 )
setSpriteDepth( thisSprite , 100 )
sprite[ 0 ] = cloneSprite( thisSprite )
setSpriteSize( thisSprite , SpriteSize , SpriteSize )
fixSpriteToScreen( thisSprite , 0 )
setSpriteDepth( thisSprite , 50 )
setSpriteColor( thisSprite , random( 127,255) , random( 127,255) , random( 127,255) , 255 )
sprite[ 1 ] = cloneSprite( thisSprite )
setSpriteColor( thisSprite , random( 127,255) , random( 127,255) , random( 127,255) , 255 )
sprite[ 2 ] = cloneSprite( thisSprite )
setSpriteColor( thisSprite , random( 127,255) , random( 127,255) , random( 127,255) , 255 )
sprite[ 3 ] = cloneSprite( thisSprite )
setSpriteColor( thisSprite , random( 127,255) , random( 127,255) , random( 127,255) , 255 )
sprite[ 4 ] = thisSprite
//
halfWid = screenWidth * 0.5
quartWid = halfWid * 0.5
eightWid = quartWid * 0.5
thirdWid = screenWidth * 0.333
halfWindow = eightWid - 2
do
// Some Code to move four sprites around
thisAngle# = timer() * 100
for thisInt = 1 to 4
select thisInt
case 1
setSpritePositionbyOffset( sprite[ thisInt ] , thirdWid + cos( thisAngle# ) * halfWid , thirdWid + sin( thisAngle# ) * thirdWid )
endcase
case 2
setSpritePositionbyOffset( sprite[ thisInt ] , thirdWid + thirdWid - cos( thisAngle# ) * thirdWid , thirdWid + sin( thisAngle# ) * HalfWid )
endcase
case 3
setSpritePositionbyOffset( sprite[ thisInt ] , thirdWid + thirdWid + cos( thisAngle# ) * thirdWid , thirdWid + thirdWid - sin( thisAngle# ) * HalfWid )
endcase
case 4
setSpritePositionbyOffset( sprite[ thisInt ] , thirdWid - cos( thisAngle# ) * thirdWid , thirdWid + thirdWid - sin( thisAngle# ) * HalfWid )
endcase
endselect
next thisInt
// Display Main Screen
AlignWorld( 0 , 0 , 0 , 0 , 1.0 )
setScissor( 0 , 0 , screenWidth , screenHeight )
// Background Sprite Blue
setSpriteColor( sprite[ 0 ] , 0 , 0 , 127 , 255 )
update(0)
render()
// Render Scissor Regions
// Background Sprite Red
setSpriteColor( sprite[ 0 ] , 127 , 0 , 0 , 255 )
for thisInt = 1 to 4
if thisInt = 1 and getPointerState()
midX = getPointerX()
midY = getPointerY()
else
midX = thisInt * quartWid - eightWid
midY = screenHeight - eightWid
endif
thisZoom# = ( thisInt * 0.25 ) + 0.25
// This is the Call that aligns the two planes based on the sprite position
AlignWorld( getSpriteXByOffset( sprite[ thisInt ] ) , getSpriteYByOffset( sprite[ thisInt ] ) , midX , midY , thisZoom# )
// Limit Render to region around sprite
setScissor( midX - halfWindow , midY - halfWindow , midX + halfWindow , midY + halfWindow )
render()
next thisInt
//
swap()
loop
end
function AlignWorld( worldX# , worldY# , displayX# , displayY#, worldZ# )
setViewZoom( worldZ# )
setViewOffset( worldX# - ( displayX# / worldZ# ) , worldY# - ( displayY# / worldZ# ) )
endfunction
Edit:
Also if you touch the screen, the first box will follow the pointer - while its contents will stay centered on it's sprite