Page 1 of 1

Rotate Sprite with Offset

Posted: Mon Sep 19, 2016 6:27 pm
by J. Baker
A couple of years ago someone showed me how to rotate a sprite from an offset or pivot point. I recently came across the code that I had saved and noticed it could use an update. This versions math is a bit simplified for x and y by using Radian() within Cos() and Sin(). Hopefully this should help someone understand how cosine and sine work.

For more information, check out this website, http://www.mathsisfun.com/geometry/unit-circle.html .

Code: Select all

;PureBasic 5.43 LTS

InitSprite()

OpenWindow(0, 0, 0, 800, 600, "Rotate Sprite Offset", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)

CreateSprite(1, 256, 256)
StartDrawing(SpriteOutput(1))
  Box(0,0,128,128,$FF0000)
  Box(128,0,128,128,$00FF00)
  Box(0,128,128,128,$0000FF)
  Box(128,128,128,128,$00FFFF)
StopDrawing()

DisplayX.c = 256
DisplayY.c = 160
Offset.c = 150
Angle.c = 0

Repeat

Event = WindowEvent()

ClearScreen(0)
  
  Angle + 2 ;<--Adjusting the angle will adjust the speed

  If Angle = 360
    Angle = 0
  EndIf
  
  ;RotateSprite(1, Angle, 0)
  DisplaySprite(1, DisplayX + Cos(Radian(Angle)) * Offset, DisplayY + Sin(Radian(Angle)) * Offset)
  
FlipBuffers()

Until Event = #PB_Event_CloseWindow

Re: Rotate Sprite with Offset

Posted: Mon Sep 19, 2016 7:54 pm
by VB6_to_PBx
;PureBasic 4.53 LTS
you meant = 5.43 LTS instead
and it works great in 5.41 LTS as well :)

Re: Rotate Sprite with Offset

Posted: Mon Sep 19, 2016 8:38 pm
by davido
@J. Baker,

Nice example. Thank you for sharing.

MacBook Pro - PureBasic 5.50.

Re: Rotate Sprite with Offset

Posted: Mon Sep 19, 2016 10:21 pm
by J. Baker
VB6_to_PBx wrote:
;PureBasic 4.53 LTS
you meant = 5.43 LTS instead
and it works great in 5.41 LTS as well :)
:shock: I fixed it. ;)

Re: Rotate Sprite with Offset

Posted: Mon Sep 19, 2016 11:00 pm
by Demivec
It is a nice example of moving a sprite with an offset. Thank you for that. :)