Sign in to follow this  

If Statement Threshold

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a statement like... if(mNeedleRotation != mNeedleRotationGoal ) mNeedleRotation is updated every tick and when mNeedleRotation == mNeedleRotationGoal, the needle will no longer be updated. However, the goal could potentially be 15 and the needle rotation potentially might skip over EXACTLY 15. Is there a way I can put a threshold into the if statement to get values near 15? Like 15.5 - 16.5, etc... Thanks!

Share this post


Link to post
Share on other sites
Assuming a language with the fabs() function (floating point absoulute value) you can use

if(fabs(mNeedleRotation - mNeedleRotationGoal) < 1.0)

Or something similar.

Share this post


Link to post
Share on other sites
Just write down what you said in the last line, but in code [smile]

You need to work out what your acceptable limits are.

eg: (assuming C++)

const float SOME_MAGIC_NAMED_CONSTANT = ...;
float distance = fabs(mNeedleRotation - mNeedleRotationGoal);
if( distance < SOME_MAGIC_NAMED_CONSTANT ) {
}

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
    const float SOME_MAGIC_NAMED_CONSTANT = ...;
float distance = fabs(mNeedleRotation - mNeedleRotationGoal);
if( distance < SOME_MAGIC_NAMED_CONSTANT ) {
}


The problem with that is that if mNeedleRotation has passed mNeedleRotationGoal, but by more than SOME_MAGIC_NAMED_CONSTANT. For example, what if SOME_MAGIC_NAMED_CONSTANT is set to 0.1 and mNeedleRotation is 15.2? Of course, you could make the value of SOME_MAGIC_NAMED_CONSTANT bigger, but that will make the condition less accurate.

Take a look at this. It will detect when mNeedleRotation crosses mNeedleRotationGoal.
    ...
float delta = mNeedleRotation - mOldNeedleRotation;
if ( delta >= 0 && mNeedleRotation >= mNeedleRotationGoal ||
delta < 0 && mNeedleRotation <= mNeedleRotationGoal )
{
...
}
mOldNeedleRotation = mNeedleRotation;
...

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
Take a look at this. It will detect when mNeedleRotation crosses mNeedleRotationGoal.
    ...
float delta = mNeedleRotation - mOldNeedleRotation;
if ( delta >= 0 && mNeedleRotation >= mNeedleRotationGoal ||
delta < 0 && mNeedleRotation <= mNeedleRotationGoal )
{
...
}
mOldNeedleRotation = mNeedleRotation;
...


Also known as:
    ...
if ( (mNeedleRotation - mNeedleRotationGoal) *
(mOldNeedleRotation - mNeedleRotationGoal) > 0.0)
{
// Still on the same side of the goal.
}
mOldNeedleRotation = mNeedleRotation;
...

Share this post


Link to post
Share on other sites

This topic is 3725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this