GLBasic forum

Main forum => GLBasic - en => Topic started by: spicypixel on 2013-Jul-07

Title: Rotating and Wrapping Nightmare
Post by: spicypixel on 2013-Jul-07
I am trying to rotate a sprite the quickest route from its existing angle to a given angle. The issue I'm having is for example if I'm at 45 degrees and I want to rotate to 315 degrees. I presently have something similar to...

pseudo-code
Code (glbasic) Select

ANGLE# = 45

IF SHIPANGLE# > ANGLE# THEN DEC SHIPANGLE#
IF SHIPANGLE# < ANGLE# THEN INC SHIPANGLE#


Works for lots of cases apart from the 0/360 threshold, anyone have any pseudo-code or suggestions please. It's late might just be my brain at this time :-)
Title: Re: Rotating and Wrapping Nightmare
Post by: MrTAToad on 2013-Jul-07
Would this value wrapping routine help : http://www.glbasic.com/forum/index.php?topic=7069.msg57811#msg57811

Title: Re: Rotating and Wrapping Nightmare
Post by: spicypixel on 2013-Jul-07
Quote from: MrTAToad on 2013-Jul-07
Would this value wrapping routine help : http://www.glbasic.com/forum/index.php?topic=7069.msg57811#msg57811

I spotted this earlier MrT but fortunately I've just found a solution that works well and is neat for the code I have, but thank you for your reply  :good:

This is what I have working...ANGLE# (to rotate to) is set at various points in the code...
Code (glbasic) Select

// Clockwise
IF PLAYER[ships%].ANGLE# < ANGLE#
IF ANGLE# > (PLAYER[ships%].ANGLE# + 180)
DEC PLAYER[ships%].ANGLE#
IF PLAYER[ships%].ANGLE# < 0 THEN PLAYER[ships%].ANGLE# = 359
        ELSE
INC PLAYER[ships%].ANGLE#
ENDIF
ENDIF

// Anti-Clockwise
IF PLAYER[ships%].ANGLE# > ANGLE#
IF ANGLE# < (PLAYER[ships%].ANGLE# - 180)
INC PLAYER[ships%].ANGLE#
IF PLAYER[ships%].ANGLE# > 359 THEN PLAYER[ships%].ANGLE# = 0
ELSE
DEC PLAYER[ships%].ANGLE#
ENDIF
ENDIF
Title: Re: Rotating and Wrapping Nightmare
Post by: spacefractal on 2013-Jul-07
I'm have that sort of code to detect which way the sprite should return. I'm used that in my mouse game. I'm are away now, but will give you the function used when om online again home...
Title: Re: Rotating and Wrapping Nightmare
Post by: spacefractal on 2013-Jul-07
not extracrly what you want, but here is the follow code you might can use:

Code (glbasic) Select

FUNCTION GetFollowingAngle: x, y, ang, TurnSpeed#, x2, y2
LOCAL targetAngle = 0
targetAngle=ATAN(y-y2, x-x2)
targetAngle=360-MOD(targetAngle,360)

LOCAL BugAngle = ang
LOCAL normAngle = targetAngle - BugAngle

IF (normAngle < 0)
normAngle = normAngle + 360
ENDIF
IF (normAngle >= 360)
normAngle = normAngle - 360
ENDIF

IF (normAngle<=180 AND normAngle > 0)
BugAngle = BugAngle + TurnSpeed
IF (BugAngle >= 360)
BugAngle = BugAngle - 360
ENDIF
ELSEIF (normAngle > 180 AND normAngle < 360)
BugAngle = BugAngle - TurnSpeed
IF (BugAngle < 0)
BugAngle = BugAngle + 360
ENDIF
ENDIF
RETURN BugAngle
ENDFUNCTION


But im think you got the idea.
Title: Re: Rotating and Wrapping Nightmare
Post by: spicypixel on 2013-Jul-08
Quote from: Ocean on 2013-Jul-07
Quote from: spicypixel on 2013-Jul-07
I am trying to rotate a sprite the quickest route from its existing angle to a given angle. The issue I'm having is for example if I'm at 45 degrees and I want to rotate to 315 degrees. I presently have something similar to...

pseudo-code
Code (glbasic) Select

ANGLE# = 45

IF SHIPANGLE# > ANGLE# THEN DEC SHIPANGLE#
IF SHIPANGLE# < ANGLE# THEN INC SHIPANGLE#


Works for lots of cases apart from the 0/360 threshold, anyone have any pseudo-code or suggestions please. It's late might just be my brain at this time :-)

for each rotation you need for your next SHOWSCREEN, you need to do the complete rotation, i.e. not from 45 to 315 but from 0 to 315 or whatever angle is your next current angle.

I want to show the rotation angle by angle, I can easily snap to the angle I require, however I managed to sort it ^ above. Thanks for the replies all of you.
Title: Rotating and Wrapping Nightmare
Post by: Kitty Hello on 2013-Jul-10
I think i solved this many years ago here:
http://www.glbasic.com/forum/index.php?topic=296.msg988#msg988
Title: Re: Rotating and Wrapping Nightmare
Post by: MrTAToad on 2013-Jul-11
The program needs updating - the MOUSEAXIS values are a bit different now :)