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

Author Topic: More optimized way to move?  (Read 1208 times)

0 Members and 1 Guest are viewing this topic.

Ancell

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
More optimized way to move?
« on: May 04, 2014, 11:10:13 pm »
Hello,

I was wondering if their is a more optimized way to do this?

I know about the playerSprite,move(position); function but it doesn't do what I want it to.

This code works just fine and the movement is smooth but just by looking at it calling the playerSprite.setPosition(); every update call doesn't seem very fast to me. IDK maybe that is the fastest way to do it.

void Player::update(float delta)
{
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    {
        position.y -= Player::VELOCITY * delta;
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) || sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    {
        position.y += Player::VELOCITY * delta;
    }

    playerSprite.setPosition(position);
}
« Last Edit: May 04, 2014, 11:12:20 pm by Ancell »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: More optimized way to move?
« Reply #1 on: May 04, 2014, 11:24:21 pm »
So you think there is a more optimized way to change a float value?

Quote
doesn't seem very fast to me.

Why don't you run a profiler then and actually see how fast it it?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Ancell

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: More optimized way to move?
« Reply #2 on: May 04, 2014, 11:27:42 pm »
From what I have learned previously while programming calling the playerSprite.setPosition(position); function every update doesn't seem very fast to me.

I will run a profiler and see if what kind if results I get.

Thanks for the help and advice.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: More optimized way to move?
« Reply #3 on: May 04, 2014, 11:28:28 pm »
This code works just fine and the movement is smooth but just by looking at it calling the playerSprite.setPosition(); every update call doesn't seem very fast to me.
Other games run a whole physics simulation in the same time, and you are worried about a single function call that might not even be one (inlining)? :o

Premature optimization is evil.

The actual problem with your code is that input handling is coupled with the player class. You should handle it externally, and set the player's velocity from outside. The update() method would then only apply the velocity.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: