Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Wrap angle without dummy sprite?

Author
Message
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 23rd Apr 2012 01:17
Anyone have an good solution to wrap an angle without an dummy sprite?
iam a bit stuck with this
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 23rd Apr 2012 11:22
You mean like this?


Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 23rd Apr 2012 20:47
Quote: "You mean like this?"

So simple that i feel stupid
Sometimes so does the brain get stuck when i have the solution in plain site.
I made some small changes to it and its a few fps faster then my old
I use this alot in my raycaster

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd Apr 2012 21:02
Here's a version that lets you update the angle and fix it at the same time (I use it in about 5 places, so far):



I used the one Cliff just provided and added the bits that help simplify my code. Thank you Cliff and baxslash.

Cheers,
Ancient Lady
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 23rd Apr 2012 21:29
Quote: "I used the one Cliff just provided and added the bits that help simplify my code. Thank you Cliff and baxslash."

Thats really clever
I will try it in my code
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 23rd Apr 2012 22:26 Edited at: 23rd Apr 2012 22:26
30+ years of programming let's you fake clever very well. Really, it's just being lazy.

Cheers,
Ancient Lady
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 23rd Apr 2012 23:13
That's a good one AL

Clever solutions do tend to be ways to make things easier for ourselves!

I tend to use this version a lot to find out which way to turn towards something. It returns a value between -180 and 180 (left or right of zero).

Cliff I'd be surprised if adding that extra conditional statement is any quicker than my original version. If anything the first line of your function is no quicker for values between 0 and 360 and is actually slower for other values because you're running two checks before you do the while statements.

Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 23rd Apr 2012 23:36
Quote: "Cliff I'd be surprised if adding that extra conditional statement is any quicker than my original version. If anything the first line of your function is no quicker for values between 0 and 360 and is actually slower for other values because you're running two checks before you do the while statements."

I actually noticed it my self
And have removed them
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 24th Apr 2012 00:17
I might have a version which eliminates the needs for while loops.



I've given it a few tests and it's passed them all but could you guys give it a few more tests just to check?

Cheers

Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 24th Apr 2012 00:41 Edited at: 24th Apr 2012 00:59
i will test it tomorrow its time to sleep

Edited........
Ok i tested it today
i made some changes and it seams pretty fast?
If anyone else wants to try it?

Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 24th Apr 2012 01:11
Found this on StackOverflow and works for floats.

Just requires a floor function.

Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 24th Apr 2012 01:22 Edited at: 24th Apr 2012 01:25
Quote: "i made some changes and it seams pretty fast?"

In theory, the Fmod method will be faster on the larger angles, e.g if a# and v# added up to 10,000 degrees (unlikely but anyway) then it would simply perform the calculation on that and won't have to go through a number of iterations in a while loop. However, since the Fmod calculations are more complex than those in the while loop it could actually take longer to calculate smaller angles e.g -360 <= a#, v# <= 360.

Edit: And Dar13's method could be faster than both!

baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 24th Apr 2012 02:49
Quote: "And Dar13's method could be faster than both!"

Yes, I just wonder if it works for negative values? Too tired to check. It's past midnight and I've been programming all day and night

Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 24th Apr 2012 03:05
Quote: "Yes, I just wonder if it works for negative values?"

I actually only checked negative numbers, as the math for positive numbers seemed right.

Using the code: -90.5 -> 269.5 and -365.5 -> 354.5.
Even works for really large negative angles: -720.5 -> 359.5

Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 24th Apr 2012 09:14
Quote: "I actually only checked negative numbers, as the math for positive numbers seemed right.

Using the code: -90.5 -> 269.5 and -365.5 -> 354.5.
Even works for really large negative angles: -720.5 -> 359.5"

So far have it worked
Will your code only work for floats?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 24th Apr 2012 17:18
I'm not sure that this actually works for negative numbers less than 360.



The floor function returns an integer value.

Making: floor(-90.5/360) = 0
Then: -90.5 - 0 * 360 = -90.5

And:
floor(-365.5/360) = -1
-365.5 - (-1 * 360) = -725.5

But it did look a very cool function.

Cheers,
Ancient Lady
Marl
12
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 24th Apr 2012 19:49 Edited at: 24th Apr 2012 20:10
Quote: "The floor function returns an integer value.

Making: floor(-90.5/360) = 0
Then: -90.5 - 0 * 360 = -90.5

And:
floor(-365.5/360) = -1
-365.5 - (-1 * 360) = -725.5
"

print(floor(-90.5/360))
print(floor(-365.5/360))

actually produces;
-1
-2
You're thinking of Trunk().. From AGK/IDE/Help/reference/Core/Floor.htm
Quote: "Rounds a float to the next lowest integer. This differs from Trunc when using negative numbers, Trunc( -1.6 ) is -1 but Floor( -1.6 ) is -2."


so the function is good.

Here's some test code.

which shows it over a decent range.

edit: test code & results pic

This get's my vote for function of the week

Nice find Dar13.

Attachments

Login to view attachments
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 24th Apr 2012 19:59 Edited at: 24th Apr 2012 20:01
I stand corrected. You are absolutely right.

That makes Dar13's the coolest of the functions.

And anything that reduces the number of lines of code in Tier1 AppGameKit is great.

Cheers,
Ancient Lady
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 24th Apr 2012 22:36
Agreed, that's a neat solution

Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 24th Apr 2012 23:29
Admittedly, I found it on StackOverflow so I don't deserve much credit. I just added some #'s to make it Tier1-compatible.

Login to post a reply

Server time is: 2024-04-18 02:44:24
Your offset time is: 2024-04-18 02:44:24