0
\$\begingroup\$

So I've written a statement for releasing the ball based on a certain speed and for it to move in the direction of the mouse, like so:

            MouseX = stateOfTheMouse.X;
            MouseY = stateOfTheMouse.Y;
            difference = Vector2.Subtract(position, new Vector2(MouseX, MouseY));
            direction = Vector2.Normalize(difference);
            velocity.X -= ((float)(usedSpeed / 2));
            velocity.Y -= ((float)(usedSpeed / 2)); 

Then in my updateI call this

            position += velocity * direction;    

I assign usedSpeed as an integer with the press of a NumPad key 1-5 each on reflects the speed amount hence NumPad1 = speed of 1, etc.

Therefore, how would I make the velocity decelerate and slow down to a complete stop?

\$\endgroup\$
2
\$\begingroup\$

You can decelerate your object by multiplying its velocity with value smaller than 1 and greater than 0, the smaller the value, the faster it decelerates. For example:

public readonly float Friction = 0.98f;
/* ... */

public void Update(GameTime gameTime)
{
    /* your code here */

    velocity *= Friction; // Here we go
}
\$\endgroup\$
0
\$\begingroup\$

In your update method, just reduce the velocity (increase if negative, otherwise decrease)

var resistence = -0.05f; // change this
if (velocity.X != 0)
{
    if (velocity.X < 0) { velocity.X += resistance; }

    if (velocity.X > 0 ) 
    {
        velocity.X -= resistance;
    }
}

if (this.vel.Y != 0)
{
    if (this.vel.Y < 0) { this.vel.Y += resistance; }

    if (this.vel.Y > 0 && tempY == this.vel.Y) 
    { 
        this.vel.Y -= resistance; 
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.