TGC Codebase Backup



Get Screen Coordinates From Sprite Coordinates by Grumpy Jedi

10th Oct 2011 13:08
Summary

These two functions allow you to return the screen x and y coordinates underneath a particular spot on a sprite.



Description

These two functions allow you to return the screen x and y coordinates underneath a particular spot on a sprite.

You can use the return values to place sprites at exact locations on other sprites. If you then return the values and update the sprite every loop, you can keep a sprite perfectly placed on another sprite even when it is rotated or resized.

The functions also take aspect ratio into account so that is not a problem.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    function GetScreenXFromSpriteXY(iSpriteIndex, fSpriteX#, fSpriteY#)

`fSpriteX# and fSpriteY# should be passed as a percentage of the distance to the edge of the sprite from the offset point.
`In otherwords if your sprite is rotating around its center (50,50) and you wanted to return the screen location under 0,0
`on the sprite, you would pass (-100, -100) into the function because 0,0 is negative 100 percent of the distance from the
`offset point to the sprite edge in both x and y.

`Note that if your offset point is 0,0 - Passing (100, 100) into the function returns the bottom right corner of the sprite.
`The distance to the edge is relative to the offset point and always totals 100 percent.

`The return value is a float

fSpriteY# = fSpriteY# / GetDisplayAspect()

SpriteX# = GetSpriteXByOffset(iSpriteIndex)
SpriteY# = GetSpriteYByOffset(iSpriteIndex)

SpriteAngle# = GetSpriteAngle(iSpriteIndex)


OffsetAmountX = (GetSpriteXByOffset(iSpriteIndex) - GetSpriteX(iSpriteIndex))
OffsetAmountY = (GetSpriteYByOffset(iSpriteIndex) - GetSpriteY(iSpriteIndex))


DistanceX# = (GetSpriteWidth(iSpriteIndex) - OffsetAmountX) * (fSpriteX# / 100)
DistanceY# = (GetSpriteHeight(iSpriteIndex) - OffsetAmountY) * (fSpriteY# / 100)

angle#= (SpriteAngle# + 90) + ATANFULL(DistanceX#,DistanceY#)

`These are not the droids you're looking for (err ANGLES! I mean ANGLES!)
if angle# + 180 >= 360
angle# = ABS(360 - (angle# + 180))
else
angle# = ABS(angle# + 180)
endif

TheDistance# = sqrt ((DistanceX# * DistanceX#) + (DistanceY# * DistanceY#))

GetCosine# = COS(angle#) * TheDistance#
GetSine# = SIN(angle#) * TheDistance# * GetDisplayAspect()

ScreenX# = SpriteX# + GetCosine#
ScreenY# = SpriteY# + GetSine#

endfunction ScreenX#



function GetScreenYFromSpriteXY(iSpriteIndex, fSpriteX#, fSpriteY#)

`fSpriteX# and fSpriteY# should be passed as a percentage of the distance to the edge of the sprite from the offset point.
`In otherwords if your sprite is rotating around its center (50,50) and you wanted to return the screen location under 0,0
`on the sprite, you would pass (-100, -100) into the function because 0,0 is negative 100 percent of the distance from the
`offset point to the sprite edge in both x and y.

`Note that if your offset point is 0,0 - Passing (100, 100) into the function returns the bottom right corner of the sprite.
`The distance to the edge is relative to the offset point and always totals 100 percent.

`The return value is a float


fSpriteY# = fSpriteY# / GetDisplayAspect()

SpriteX# = GetSpriteXByOffset(iSpriteIndex)
SpriteY# = GetSpriteYByOffset(iSpriteIndex)

SpriteAngle# = GetSpriteAngle(iSpriteIndex)

OffsetAmountX = GetSpriteXByOffset(iSpriteIndex) - GetSpriteX(iSpriteIndex)
OffsetAmountY = GetSpriteYByOffset(iSpriteIndex) - GetSpriteY(iSpriteIndex)

DistanceX# = (GetSpriteWidth(iSpriteIndex) - OffsetAmountX)  * (fSpriteX# / 100)
DistanceY# = (GetSpriteHeight(iSpriteIndex) - OffsetAmountY) * (fSpriteY# / 100)

angle#= (SpriteAngle# + 90) + ATANFULL(DistanceX#,DistanceY#)

`These are not the droids you're looking for (err ANGLES! I mean ANGLES!)
if angle# + 180 >= 360
angle# = ABS(360 - (angle# + 180))
else
angle# = ABS(angle# + 180)
endif

TheDistance# = sqrt ((DistanceX# * DistanceX#) + (DistanceY# * DistanceY# ))

GetCosine# = COS(angle#) * TheDistance#
GetSine# = SIN(angle#) * TheDistance# * GetDisplayAspect()

ScreenX# = SpriteX# + GetCosine#
ScreenY# = SpriteY# + GetSine#

endfunction ScreenY#