Rotating and Wrapping Nightmare

Previous topic - Next topic

spicypixel

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 :-)
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.


spicypixel

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
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

spacefractal

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...
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

spacefractal

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.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

spicypixel

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.
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Kitty Hello


MrTAToad

The program needs updating - the MOUSEAXIS values are a bit different now :)