It is currently Mon Dec 11, 2017 1:25 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Rotate Sprite with Offset
PostPosted: Mon Sep 19, 2016 6:27 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Apr 27, 2003 8:12 am
Posts: 1963
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
 Profile  
Reply with quote  
 Post subject: Re: Rotate Sprite with Offset
PostPosted: Mon Sep 19, 2016 7:54 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Mon May 09, 2011 9:36 am
Posts: 346
Quote:
;PureBasic 4.53 LTS


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

_________________
 
PureBasic .... making little electrons do what you want !


Top
 Profile  
Reply with quote  
 Post subject: Re: Rotate Sprite with Offset
PostPosted: Mon Sep 19, 2016 8:38 pm 
Offline
Addict
Addict

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

Nice example. Thank you for sharing.

MacBook Pro - PureBasic 5.50.

_________________
DE AA EB


Top
 Profile  
Reply with quote  
 Post subject: Re: Rotate Sprite with Offset
PostPosted: Mon Sep 19, 2016 10:21 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Apr 27, 2003 8:12 am
Posts: 1963
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 :)

:shock: 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
 Profile  
Reply with quote  
 Post subject: Re: Rotate Sprite with Offset
PostPosted: Mon Sep 19, 2016 11:00 pm 
Offline
Addict
Addict
User avatar

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

_________________
Image


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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 forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye