Rotation to angle / to position?
Posted: Tue Jul 23, 2019 11:09 am
Hi,
I'd like to reposition my line on the red dot after every rotation. In other words, I would like the line to rotate around the dot. I read a few threads, and they all mention that the default behavior of RotateSprite() is to rotate around the top left 0,0. It's obviously not the case.
1/ Am I missing something, or did PureBasic change that behavior?
2/ In my project, I'm always using #PB_Absolute with an "angle" variable, and I don't need to do the rotation in real time. I would simply do Rotate(*sprite, 45), and boom, the line would have a 45° and the top would be on the red dot. Being terrible at maths, I would really appreciate the magic formula for this
3/ Also, a bonus question: is there a function that gives the angle to a point? I thought there was something like that in the Sprite library, but I'm maybe confusing with another language. My goal would be to have: RotateTo(*sprite, x.d, y.d).
Thanks.
Code: Select all
EnableExplicit
#screen_width = 1920
#screen_height = 1080
#app_title = "TITLE"
Define sprite_mouse.i, sprite_fps.i, sprite_line.i, sprite_dot.i
Define frame.i, frame_refresh, frame_time.i, event.i
Global delta.d
InitKeyboard() : InitMouse() : InitSprite()
OpenWindow(0, 0, 0, #screen_width, #screen_height, #app_title, #PB_Window_BorderLess | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, #screen_width, #screen_height, #False, 0, 0, #PB_Screen_SmartSynchronization)
sprite_mouse = CreateSprite(#PB_Any, 7, 7)
StartDrawing(SpriteOutput(sprite_mouse))
Box(0, 0, OutputWidth(), OutputHeight(), #Black)
Box(0, 0, OutputWidth() - 2, OutputHeight() - 2, #White)
StopDrawing()
sprite_fps = CreateSprite(#PB_Any, 40, 20)
sprite_dot = CreateSprite(#PB_Any, 2, 2)
StartDrawing(SpriteOutput(sprite_dot))
Box(0, 0, OutputWidth(), OutputHeight(), RGB(255, 0, 0))
StopDrawing()
sprite_line = CreateSprite(#PB_Any, 2, 100)
StartDrawing(SpriteOutput(sprite_line))
Box(0, 0, OutputWidth(), OutputHeight(), #White)
StopDrawing()
MouseLocate(DesktopMouseX(), DesktopMouseY())
Repeat
Repeat
event.i = WindowEvent()
If event = #PB_Event_CloseWindow : Break 2 : EndIf
Until event = 0
ClearScreen(RGB(50, 65, 70))
ExamineKeyboard() : ExamineMouse()
MouseLocate(DesktopMouseX(), DesktopMouseY())
DisplaySprite(sprite_line, #screen_width / 2, #screen_height / 2)
RotateSprite(sprite_line, 1, #PB_Relative)
DisplaySprite(sprite_dot, #screen_width / 2, #screen_height / 2)
DisplaySprite(sprite_fps, 0, 0)
DisplaySprite(sprite_mouse, MouseX(), MouseY())
; FPS and Delta Time
frame + 1
If ElapsedMilliseconds() > frame_refresh
StartDrawing(SpriteOutput(sprite_fps))
DrawText(0, 0, "fps:" + frame)
StopDrawing()
frame_refresh = ElapsedMilliseconds() + 1000
frame = 0
EndIf
delta = (ElapsedMilliseconds() - frame_time) / 1000
frame_time = ElapsedMilliseconds()
FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape)
1/ Am I missing something, or did PureBasic change that behavior?
2/ In my project, I'm always using #PB_Absolute with an "angle" variable, and I don't need to do the rotation in real time. I would simply do Rotate(*sprite, 45), and boom, the line would have a 45° and the top would be on the red dot. Being terrible at maths, I would really appreciate the magic formula for this
3/ Also, a bonus question: is there a function that gives the angle to a point? I thought there was something like that in the Sprite library, but I'm maybe confusing with another language. My goal would be to have: RotateTo(*sprite, x.d, y.d).
Thanks.