Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7836561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:12:32+00:00 2026-06-02T14:12:32+00:00

I’m not asking how to implement this, it’s simple enough concept. What I’m having

  • 0

I’m not asking how to implement this, it’s simple enough concept. What I’m having trouble with is floats. Here is my code:

Position += mVelocity;

        if (!Keyboard.GetState().IsKeyDown(Keys.Left) && !Keyboard.GetState().IsKeyDown(Keys.Right) && !Keyboard.GetState().IsKeyDown(Keys.Down) && !Keyboard.GetState().IsKeyDown(Keys.Up))
        {
            if (mVelocity.X != 0.0F)
            {
                if (mVelocity.X > 0.0F)
                    mVelocity.X -= mFriction;
                if (mVelocity.X < 0.0F)
                    mVelocity.X += mFriction;
            }
            if (mVelocity.Y != 0.0F)
            {
                if (mVelocity.Y > 0.0F)
                    mVelocity.Y -= mFriction;
                if (mVelocity.Y < 0.0F)
                    mVelocity.Y += mFriction;
            }
        }


        Console.WriteLine(mVelocity.X + ", " + mVelocity.Y);

mFriction is 0.2F.

The problem is, if I move the player with a positive velocity (X or Y), the friction works as it should and his velocity reduces to 0.0. However, if I move him with a negative velocity, the friction adds, and for some reason, does not stop at 0.0, but goes up to 0.2 before stopping. Because of this, I have a player moving, and then when I let go of the keys, he begins sliding slowly in the opposite direction. Why is this? How can I fix it?

In addition, I tried this code for test:

            if (mVelocity.X == 0.2F)
                mVelocity.X = 0.0F;
            if (mVelocity.Y == 0.2F)
                mVelocity.Y = 0.0F;

But it gives me the exact same results as if I hadn’t added the code. Apparently, 0.2F is not equal to 0.2F. What could this be?

Finally, in the debug output, when the player is at rest from moving positively (stopped supposedly) his speeds for both x and y (depending on how I moved him) are 2.980232E-08. I don’t understand this. If I set his speed to 1, and I subtract exactly 0.2 every frame, why would it go any other way than 1.0, 0.8, 0.6, 0.4, 0.2, 0.0? Following that, if he is at rest from moving negatively, his speeds for x and y (again, depending on how I move him) are 0.2. Why would it not go -1, -0.8, -0.6, -0.4, -0.2, 0.0? Instead of going from 0.0 to 0.2 in excess.

This all is making next to no sense to me, and if anyone could clear this up for me I would be grateful. Any alternate solutions are also accepted. If any more code, or clarification is needed, I would be happy to provide it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:12:33+00:00Added an answer on June 2, 2026 at 2:12 pm

    The problem is that your equality test fails because of imprecision, so the character keeps moving for one extra frame, for instance.

    Floating-point numbers are fundamentally imprecise, so theoretically, 2.0f * 5.0f is not necessarily == 10.0f. It might be 9.999999f or 10.000001f. Don’t test if two floating point numbers are equal: test if they are within a sufficiently small interval, i.e.:

    // test approximate equality between f1 and f2
    bool areEqual(float f1, float f2) {
        return Math.Abs(f1 - f2) < EqualityThreshold; // where EqualityThreshold is a small constant
    }
    

    Also read http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.