# PureBasic Forum

 It is currently Mon Feb 26, 2018 4:11 am

 All times are UTC + 1 hour

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Rotate Sprite with OffsetPosted: Mon Sep 19, 2016 6:27 pm

Joined: Sun Apr 27, 2003 8:12 am
Posts: 1978
Location: USA
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:
;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

_________________
www.posemotion.com

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

Mac: 10.9.5 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: XP SP3 / AMD 64 4000+ / 1GB DDR / ATI 9250

Even the vine knows it surroundings but the man with eyes does not.

Last edited by J. Baker on Mon Sep 19, 2016 10:19 pm, edited 1 time in total.

Top

 Post subject: Re: Rotate Sprite with OffsetPosted: Mon Sep 19, 2016 7:54 pm
 Enthusiast

Joined: Mon May 09, 2011 9:36 am
Posts: 357
Quote:
;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 !

Top

 Post subject: Re: Rotate Sprite with OffsetPosted: Mon Sep 19, 2016 8:38 pm

Joined: Fri Nov 09, 2012 11:04 pm
Posts: 1516
Location: Uttoxeter, UK
@J. Baker,

Nice example. Thank you for sharing.

MacBook Pro - PureBasic 5.50.

_________________
DE AA EB

Top

 Post subject: Re: Rotate Sprite with OffsetPosted: Mon Sep 19, 2016 10:21 pm

Joined: Sun Apr 27, 2003 8:12 am
Posts: 1978
Location: USA
VB6_to_PBx wrote:
Quote:
;PureBasic 4.53 LTS

you meant = 5.43 LTS instead
and it works great in 5.41 LTS as well

I fixed it.

_________________
www.posemotion.com

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

Mac: 10.9.5 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: XP SP3 / AMD 64 4000+ / 1GB DDR / ATI 9250

Even the vine knows it surroundings but the man with eyes does not.

Top

 Post subject: Re: Rotate Sprite with OffsetPosted: Mon Sep 19, 2016 11:00 pm

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3443
Location: Utah, USA
It is a nice example of moving a sprite with an offset. Thank you for that.

_________________

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 5 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 3 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite