Jump to content
  • Advertisement
Sign in to follow this  
matt3d

If Statement Threshold

This topic is 3942 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!