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

Author Topic: Moving a shape along both axis using sf::Vector2f  (Read 3009 times)

0 Members and 1 Guest are viewing this topic.

dotty

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Moving a shape along both axis using sf::Vector2f
« on: February 20, 2012, 05:13:20 pm »
Hello, I read here a couple of days ago about using a Vector2f to process my movement, this seemed an obvious thing once I read it. However, I'm getting a little confused by it.

I have an Update() method which gets run every frame, I also have a ManageInput() method as well, which also get's run every frame.

The Update() method also runs this code

Code: [Select]

this->shape.Move( this->velocity );


this->velocity is obviously the Vector2f in question. The ManageInput() method is just a series of IF statements checking for W,A,S and D keyPresses. Here's a sample of this code

Code: [Select]

if (sf::Keyboard::IsKeyPressed( sf::Keyboard::W )) {
this->velocity = sf::Vector2f(0, -1) * this->deltatime;
}

if (sf::Keyboard::IsKeyPressed( sf::Keyboard::S )) {
this->velocity = sf::Vector2f(0, 1) * this->deltatime;
}

if (sf::Keyboard::IsKeyPressed( sf::Keyboard::A )) {
this->velocity = sf::Vector2f(-1, 0) * this->deltatime;
}

if (sf::Keyboard::IsKeyPressed( sf::Keyboard::D )) {
this->velocity = sf::Vector2f(1, 0) * this->deltatime;
}


However, using this code I can only move along 1 axis at a time.

If I modify this code to look like this

Code: [Select]

if (sf::Keyboard::IsKeyPressed( sf::Keyboard::W )) {
this->shape.Move(sf::Vector2f(0, -1)) * this->deltatime;
}

if (sf::Keyboard::IsKeyPressed( sf::Keyboard::S )) {
this->shape.Move(sf::Vector2f(0, 1)) * this->deltatime;
}

if (sf::Keyboard::IsKeyPressed( sf::Keyboard::A )) {
this->shape.Move(sf::Vector2f(-1, 0)) * this->deltatime;
}

if (sf::Keyboard::IsKeyPressed( sf::Keyboard::D )) {
this->shape.Move(sf::Vector2f(1, 0)) * this->deltatime;
}


It works as expected, allows me to both on both axis. How would I modify the top code to work as I want it to.

Elgan

  • Jr. Member
  • **
  • Posts: 77
    • AOL Instant Messenger - Flat+1,+17+st+Cl
    • View Profile
Moving a shape along both axis using sf::Vector2f
« Reply #1 on: February 20, 2012, 05:29:16 pm »
try
Code: [Select]

if (sf::Keyboard::IsKeyPressed( sf::Keyboard::W )) {
      this->velocity += ( sf::Vector2f(0, -1) * this->deltatime );
   }

   if (sf::Keyboard::IsKeyPressed( sf::Keyboard::S )) {
      this->velocity += sf::Vector2f(0, 1) * this->deltatime;
   }

if (sf::Keyboard::IsKeyPressed( sf::Keyboard::A )) {
      this->velocity += sf::Vector2f(-1, 0) * this->deltatime;
   }

   if (sf::Keyboard::IsKeyPressed( sf::Keyboard::D )) {
      this->velocity += sf::Vector2f(1, 0) * this->deltatime;
   }

kamui

  • Sr. Member
  • ****
  • Posts: 291
    • View Profile
Moving a shape along both axis using sf::Vector2f
« Reply #2 on: February 20, 2012, 05:29:41 pm »
Hello,

this because you're using sf::Events instead of sf::Input (if you're on SFM-1.6).

Read this Tutorial for more explanations.

Edit : oops, sorry  :D did'nt wrote until the end... Elgan is right ;) (you delete your first assignement with both "=" operations).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Moving a shape along both axis using sf::Vector2f
« Reply #3 on: February 20, 2012, 05:31:25 pm »
Code: [Select]
if (sf::Keyboard::IsKeyPressed( sf::Keyboard::W )) {
      this->velocity.y -= this->deltatime;
   }

   if (sf::Keyboard::IsKeyPressed( sf::Keyboard::S )) {
      this->velocity.y += this->deltatime;
   }

if (sf::Keyboard::IsKeyPressed( sf::Keyboard::A )) {
      this->velocity.x -= this->deltatime;
   }

   if (sf::Keyboard::IsKeyPressed( sf::Keyboard::D )) {
      this->velocity.x += this->deltatime;
   }
Laurent Gomila - SFML developer