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 / checking for a negative angle/float issue

Author
Message
gsdblack
12
Years of Service
User Offline
Joined: 6th Dec 2011
Location:
Posted: 25th Feb 2014 03:49
I'm trying to rotate a sprite and have it stop at 0, 90, 179/-179, and -90 degrees. My code works fine for positive angle/float values but doesn't work when I try a negative numbers. Here's a piece of my code. Has anyone encountered this issue?


1. Not sure of version. I believe 108.~
2. Tier 2 - ios
3. I got the template from agk/IDE/Templates
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 25th Feb 2014 08:17
maye try compare both as integer not with a float.

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 25th Feb 2014 09:07
Markus is right. Never compare floating point numbers!

-- Jim - When is there going to be a release?
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 25th Feb 2014 09:30
it is because 90.0 as string in your editor is intern like this 90.00000098

AGK 108 (B)19 : Windows 8.1 Pro 64 Bit : AMD Radeon HD 6670
Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 25th Feb 2014 09:50
Quote: "it is because 90.0 as string in your editor is intern like this 90.00000098"


Yes, I think that is the case here. While I'n not proficient in C++, I think using the operator <= instead of == will work better. Also, you can make the angle wrap around from 0 to 360, which might feel more convenient because you can avoid negatives. Here's some T1 code to do that:

JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 25th Feb 2014 12:51
You MUST convert to an integer form to make comparisons on floats, otherwise. Can't remember the C/C++ equivalent, but in Delphi I would use trunc(x) to lose the unwanted imprecision.

-- Jim - When is there going to be a release?
Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 25th Feb 2014 13:12 Edited at: 25th Feb 2014 13:15
Another way to wrap the angle (from the community functions thread);


Quote: "You MUST convert to an integer form to make comparisons on floats"

I disagree, you can comparing floats directly if you provide some room for the rounding errors (tolerance)

(tier 1, but you get the idea)
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 25th Feb 2014 13:32
Marl - I know that - but it's a more complex way of doing it.

-- Jim - When is there going to be a release?
Marl
13
Years of Service
User Offline
Joined: 19th Nov 2011
Location: Bradford, UK
Posted: 25th Feb 2014 14:06 Edited at: 25th Feb 2014 14:06
I disagree again. It only seems more complex because of the abs function, internally a comparison is identical to a subtraction as I'm sure you're well aware.

And let's not forget that this is an alternative to converting two values to integers before comparing so we're looking at

if round( a ) = round( b )

vs

if abs( a-b ) < value

But my original issue was with the fact that people MUST do something a particular way without being given alternatives.

That is not good advice, it's prescription.
gsdblack
12
Years of Service
User Offline
Joined: 6th Dec 2011
Location:
Posted: 27th Feb 2014 05:16
Thanks guys. I went with wrap-around code. I tried comparing both integer and float nothing worked.

Login to post a reply

Server time is: 2024-11-25 02:30:45
Your offset time is: 2024-11-25 02:30:45