Rotate Sprite with Offset

Advanced game related topics
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Rotate Sprite with Offset

Post 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
Last edited by J. Baker on Mon Sep 19, 2016 10:19 pm, edited 1 time in total.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 617
Joined: Mon May 09, 2011 9:36 am

Re: Rotate Sprite with Offset

Post by VB6_to_PBx »

;PureBasic 4.53 LTS
you meant = 5.43 LTS instead
and it works great in 5.41 LTS as well :)
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Rotate Sprite with Offset

Post by davido »

@J. Baker,

Nice example. Thank you for sharing.

MacBook Pro - PureBasic 5.50.
DE AA EB
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Rotate Sprite with Offset

Post 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. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Rotate Sprite with Offset

Post by Demivec »

It is a nice example of moving a sprite with an offset. Thank you for that. :)
Post Reply