# PureBasic Forum

 It is currently Sat Dec 16, 2017 7:50 pm

 All times are UTC + 1 hour

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Rotate sprite around pivotPosted: Mon Nov 28, 2016 9:20 pm
 Enthusiast

Joined: Wed Sep 18, 2013 11:54 am
Posts: 301
Location: France
Hey,

As far as I know, there's no native way to rotate around a given point (a "pivot"). And because my maths are terrible, I'd like to know what exactly should I do to do that. When the pivot is at SpriteWidth()/2 and SpriteHeight()/2, RotateSprite() is enough because it rotates around the center of the sprite, but not when I change it.

I searched on a few website, but everything is bound to a language that include native functions to do this kind of stuff. Maybe I missed a PB function?

Top

 Post subject: Re: Rotate sprite around pivotPosted: Tue Nov 29, 2016 1:03 am

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3425
Location: Utah, USA
Joubarbe wrote:
When the pivot is at SpriteWidth()/2 and SpriteHeight()/2, RotateSprite() is enough because it rotates around the center of the sprite, but not when I change it.

RotateSprite() rotates around the upper left corner, not the center point as you mentioned.

What you would use is TransformSprite().

There's more than a few examples on the forum, here's one:
http://www.purebasic.fr/english/viewtopic.php?f=13&t=63579&p=473711#p473711

_________________

Last edited by Demivec on Tue Nov 29, 2016 1:53 am, edited 1 time in total.

Top

 Post subject: Re: Rotate sprite around pivotPosted: Tue Nov 29, 2016 1:39 am
 Enthusiast

Joined: Wed Sep 21, 2011 9:11 am
Posts: 580
Location: France
Quote:
As far as I know, there's no native way to rotate around a given point (a "pivot")
It is true, there is no native function to change the anchor point of a sprite.

A solution with this code
Code:
Structure newSprite
id.i
x.f
y.f
anchorX.f
anchorY.f
angle.f
EndStructure

Declare AnchorSprite(*sprite.newSprite, x.f, y.f)
Declare RotateSpriteAroundAnchor(*sprite.newSprite, angle.f, initialAngle.f = 0, calcSlope.b = #True)

InitSprite() : InitKeyboard()

OpenWindow(0, 0, 0, 800, 600, "Rotate Around Anchor", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)

;Setup Sprite
Global sprite.newSprite\id = CreateSprite(-1, 64, 64)
With sprite
StartDrawing(SpriteOutput(\id))
Box(0, 0, 64, 64, RGB(255, 215, 0))
Box(12, 55, 40, 2, RGB(0, 0, 0))
Box(2, 60, 60, 2, RGB(0, 0, 0))
StopDrawing()

;Setup position
\x = 400
\y = 100
EndWith
AnchorSprite(sprite, 400, 300)

;Render
Repeat
Repeat
Event = WindowEvent()
Select Event
Case #PB_Event_CloseWindow
End
EndSelect
Until Event=0

ClearScreen(RGB(184, 134, 11))

With sprite
DisplaySprite(\id, \x, \y)
RotateSpriteAroundAnchor(sprite, 0.5)
EndWith

ExamineKeyboard()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)

;Setup Sprite Anchor
Procedure AnchorSprite(*sprite.newSprite, x.f, y.f)
Protected dx.d, dy.d
With *sprite
\anchorX = x
\anchorY = y
dx.d = \x - \anchorX  ;Horizontal distance
dy.d = \y - \anchorY  ;Vertical Distance
\radius = (Sqr(dx*dx + dy*dy)) ;Distance between anchor and sprite position
\angle = ATan2((\x - \anchorX), (\y - \anchorY) ) * 180 / #PI ;Angle between anchor and sprite position
EndWith
EndProcedure

;Rotate sprite around anchor
Procedure RotateSpriteAroundAnchor(*sprite.newSprite, angle.f, initialAngle.f = 0, calcSlope.b = #True)
With *sprite
If \angle = 360 : \angle = 0 : EndIf
\angle + angle
\x = \anchorX + \radius * Cos(\angle * #PI / 180)
\y = \anchorY + \radius * Sin(\angle * #PI / 180)
If calcSlope = #True
RotateSprite(\id, ATan2((\x - \anchorX), (\y - \anchorY) ) * 180 / #PI + initialAngle, #PB_Absolute)
EndIf
EndWith
EndProcedure

_________________

➽ Windows 10 - PB 5.45 LTS & PB 5.60

http://falsam.com & EasySprite.js
http://purebasic.chat - Code PureBasic
Pure Basic Francophone Community

Sorry for my bad english and the Dunning–Kruger effect.

Top

 Post subject: Re: Rotate sprite around pivotPosted: Tue Nov 29, 2016 9:21 am
 Enthusiast

Joined: Wed Sep 18, 2013 11:54 am
Posts: 301
Location: France
Thanks!

Top

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

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 4 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