Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Normalizing speed  (Read 1847 times)

0 Members and 1 Guest are viewing this topic.

Roflsausage

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
[SOLVED] Normalizing speed
« on: October 15, 2017, 01:36:02 am »
Hello everybody. I am writing a space game where the players has velocity and coasts along if nothing stops them. This has been working well but I now want to add a sort of speed limit, as the player can increase their speed to infinite amounts. I have used Unity mostly in the past, and in this situation I would just normalize the velocity vector and multiply it by the maximum allowed velocity. I have tried to implement something like this in SFML, but am running into a strange issue. The code performs as it should when the player ONLY has an X or Y velocity, but as soon as they have a little bit of both, the players X and Y velocity are both set to 0. I know I must be making a mistake in my code somewhere but cannot figure it out.

EDIT: Here is all the code on pastebin so it is formatted properly, it is also the entire class.
https://pastebin.com/yUSpMSxE

Here is my code for normalizing the velocity vectors.
Quote
    void NormalizeVelocity(){
      currentVelocity = fabs(velocityX) + fabs(velocityY);
       if(currentVelocity > maxVelocity){
       //percentage that each axis is the of the total velocity
       int xp = fabs(velocityX) / (fabs(velocityX) + fabs(velocityY));
       int yp = fabs(velocityY) / (fabs(velocityX) + fabs(velocityY));
      
       //different normalization if value is negative
       if(velocityX > 0){
           velocityX = maxVelocity * xp;
       }
       else if(velocityX < 0){
           velocityX = -maxVelocity * xp;
       }
       if(velocityY < 0){
           velocityY = -maxVelocity * yp;
       }
       else if(velocityY > 0){
          velocityY = maxVelocity * yp;
       }
   }
        //player x and y position
       x += velocityX;
   y -= velocityY;
    }

As I described before, the code works to limit the velocity if the player has no diagonal movement. As soon as there is some velocity on both X and Y, the players X and Y velocity are set back to 0. I will also post my input code below as it may be related to that as well.

Quote
    void GetInput(){
        //get keyboard input
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
            AddYVelocity(.1);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
            AddYVelocity(-.1);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
            AddXVelocity(-.1);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
            AddXVelocity(.1);
        }
    }

Any help would be appreciated. I have been pulling my hair out over this for the last hour or so. Also sorry about the code formatting, I tried to get it to look good in the quotes but it kept wanting to indent in random locations.
« Last Edit: October 15, 2017, 03:02:50 am by Roflsausage »

Roflsausage

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Normalizing speed
« Reply #1 on: October 15, 2017, 03:02:27 am »
Hey guys, in case anybody is interested I figured this out by changing my method. Instead of setting the value manually, after the input is received and the acceleration values for X and Y are added, I checked if their total is greater than the max velocity. If so add to their current value the current velocity times the negative of the acceleration speed. I figure this is kinda like adding an opposing force to cancel out the one previously added.