Being a newbie myself, I thought I would post these functions in case anyone found them useful. They are used to calculate and display the rotating of a sprite that has been scaled with the SCALE SPRITE() function.
There are also some functions for returning the X, Y positions of the rotated sprite from an offset and likewise the degrees of the rotation given and X and Y.
I hope someone might find them useful.
//
// Rotate a scaled sprite around its centre; there is no radius offset.
//
function spriteRotateScale( spr, deg, scale )
// Just to ensure a sensible value - 0 would entail no sprite to be shown
if ( scale = 0 )
scale = 100
endif
// Centre the sprite (including scaling value) then rotate it around the centre
offset sprite spr, ( SPRITE WIDTH( spr ) / 2 ) * ( 100 / scale ), ( ( SPRITE HEIGHT( spr ) / 2 ) * ( 100 / scale ) )
rotate sprite spr, deg
endfunction
//
// Rotate a scaled sprite with an offset value (from its current location).
// Scaling is a percentage value with 100 being 1 to 1.
//
function spriteRotateScaleOffset( spr, deg, radius, scale )
// Just to ensure a sensible value - 0 would entail no sprite to be shown
if ( scale = 0 )
scale = 100
endif
// Offset by the radius, but also take into account the width and height of the sprite (positioning is from 0,0 of the sprite, not its centre)
// The offset works by +ve Y moving up the screen and +ve X to the left
offset sprite spr, ( SPRITE WIDTH( spr ) / 2 ) * ( 100 / scale ), ( ( SPRITE HEIGHT( spr ) / 2 ) * ( 100 / scale ) ) + ( radius * ( 100 / scale ) )
rotate sprite spr, deg
endfunction
//
// Rotate a scaled sprite around its centre then display at its X and Y positions.
// This function requires the object number for the SPRITE display command.
// Scaling is a percentage value with 100 being 1 to 1.
//
function spriteRotateScalePos( spr, obj, x, y, deg, scale )
// Just to ensure a sensible value - 0 would entail no sprite to be shown
if ( scale = 0 )
scale = 100
endif
// Centre the sprite (including scaling value) then rotate it around the centre
offset sprite spr, ( SPRITE WIDTH( spr ) / 2 ) * ( 100 / scale ), ( ( SPRITE HEIGHT( spr ) / 2 ) * ( 100 / scale ) )
rotate sprite spr, deg
sprite spr, x, y, obj
endfunction
//
// Retrieve the X value of a rotation from its current position.
//
function getRotationX( x, deg, radius )
calcDeg# = deg - 90.0 ` For calculated zero degrees
calcRadius# = radius
// Calculate the X co-ordinate for the sprite given the current radius and degrees
nX = x + ( cos( calcDeg# ) * calcRadius# )
endfunction nX
//
// Retrieve the Y value of a rotation from its current position.
//
function getRotationY( y, deg, radius )
calcDeg# = deg - 90.0 ` For calculated zero degrees
calcRadius# = radius
// Calculate the Y co-ordinate for the sprite given the current radius and degrees
nY = y + ( sin( calcDeg# ) * calcRadius# )
endfunction nY
//
// Retrieve the distance between two points.
//
function getDistanceFromXY( sX, sY, eX, eY )
// Simply use pythagorus theorem to get the length
dX# = abs( eX - sX )
dY# = abs( eY - sY )
deg# = sqrt( ( dX# * dX# ) + ( dY# * dY# ) )
endfunction deg#
//
// Retrieve the degrees of two points, presumably from the screen origin of (0, 0). The output
// from atanfull() runs from -180 -> 180; subtract the value from 180 to give a 0 -> 360 value.
//
function getDegreesFromXY( sX, sY, eX, eY )
// Utilise the atanfull() to get the angle between the two points
dX# = eX - sX
dY# = eY - sY
deg# = 180.0 - ( atanfull( dX#, dY# ) )
endfunction deg#
//
// Retrieve the degrees of the sprite given its current X position against an origin. This uses
// acos() so it is not accurate around 90 degrees and can only give a range of -90 (270) -> 90.
//
function getDegreesFromX( x, originX, radius )
// Calculate the degrees from the X values and the given radius
offsetX# = x - originX
radius# = radius
deg# = acos( offsetX# / radius# )
deg# = deg# - 90.0
// For reasons I am not so sure of yet, the degrees need to be reversed
deg# = checkDegs( CIRCLE_DEGREES - deg# )
endfunction deg#
//
// Quick degree check function to ensure the value is always between 0 and 359. This is used
// as opposed to the inbuilt WRAPVALUE() as the documentation states 0 to 360, which essentially
// gives two points at 0 degrees.
//
function checkDegs( deg )
deg = deg %% CIRCLE_DEGREES
if ( deg >= CIRCLE_DEGREES )
retDeg = deg - CIRCLE_DEGREES
else
if ( deg < 0 )
retDeg = CIRCLE_DEGREES + deg
else
retDeg = deg
endif
endif
endfunction retDeg
Notes:
spr parameter is the Sprite Number (I used eXtends for loading in sprites)
deg parameter is degrees with 0 at 12 o' clock
scale is a percentage value of the value used to scale the sprite