This code works....if you new rotation point is x,y
xn# = GetSpriteXFromWorld(spr,x,y)
yn# = GetSpriteYFromWorld(spr,x,y)
// Set the new offset AND the POSITION
SetSpriteOffset(spr,xn#+GetSpriteOffsetX(spr),yn#+GetSpriteOffsetY(spr))
SetSpritePositionByOffset(spr,x,y)
I think the only thing you were missing was setting the sprites new position to be the new rotation point.
A Full example....
// Project: SpriteRotate
// Created: 2019-03-12
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "SpriteRotate" )
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 ) // since version 2.0.22 we can use nicer default fonts
// Set the rotation point to be centre of the screen
xrot# = GetDeviceWidth()/2
yrot# = GetDeviceHeight()/2
// Create centre point sprint
cps = CreateSprite(0)
SetSpriteColor(cps,255,0,0,255)
SetSpriteSize(cps,20,20)
SetSpritePositionByOffset(cps,xrot#,yrot#)
// Create a sprite that will rotate
spr = CreateSprite(0)
SetSpriteSize(spr,80,80)
// Position it randomly on the screen with a random angle
xr# = random2(200,824)
yr# = random2(200,568)
SetSpritePosition(spr,xr#,yr#)
SetSpriteAngle(spr,random2(1,360))
// now set the sprite offset to make it rotate about the middle of the screen to start with
SetSpriteRotationPoint(spr,xrot#,yrot#)
do
if Timer()<1
// Dont rotate for 1 second - so we can see where the sprite started from
else
// Now just rotate the sprite
SetSpriteAngle(spr,GetSpriteAngle(spr) + 40*GetFrameTime())
endif
// if the mouse is clicked - the change the rotation point
if GetPointerPressed()
xrot# = GetPointerX()
yrot# = GetPointerY()
SetSpriteRotationPoint(spr,xrot#,yrot#)
SetSpritePositionByOffset(cps,xrot#,yrot#)
endif
//Print( ScreenFPS() )
Print("Click to define a new point to rotate about")
Print("Currently rotating around (x,y): " + str(xrot#,0) +","+ str(yrot#,0))
Sync()
loop
function SetSpriteRotationPoint(spr as integer, x as float, y as float)
// Now get the position on the sprite that is the centre of the screen
xn# = GetSpriteXFromWorld(spr,x,y)
yn# = GetSpriteYFromWorld(spr,x,y)
// Set the new offset
SetSpriteOffset(spr,xn#+GetSpriteOffsetX(spr),yn#+GetSpriteOffsetY(spr))
SetSpritePositionByOffset(spr,x,y)
endfunction
The example allows you to set any point on the screen for the sprite to rotate about by clicking the mouse over and over.