46
General / Re: How to implement character control with the mouse
« on: September 13, 2017, 07:50:23 pm »Can you tell me how correctly or incorrectly I implemented it?
Does your implementation work like you expect? Then you did it correctly .
There is one thing which might not work like you expect: Your character will move faster when farther away from the cursor, because you take the distance vector and not the direction in your calculation. So I would recommend the following for your movement vector:
if ( Mouse::isButtonPressed(Mouse::Right) )
{
Vector2f totalMovement;
totalMovement.x = Mouse::getPosition(window).x - hero_sprite.getPosition().x;
totalMovement.y = Mouse::getPosition(window).y - hero_sprite.getPosition().y;
// Create a unit vector, to move at constant speed
totalMovement /= std::sqrt(std::pow(totalMovement.x, 2) + std::pow(totalMovement.y, 2))
hero_sprite.move(totalMovement * (1.f/1000.f)); //1/1000 should then be choosen bigger like 5
}
{
Vector2f totalMovement;
totalMovement.x = Mouse::getPosition(window).x - hero_sprite.getPosition().x;
totalMovement.y = Mouse::getPosition(window).y - hero_sprite.getPosition().y;
// Create a unit vector, to move at constant speed
totalMovement /= std::sqrt(std::pow(totalMovement.x, 2) + std::pow(totalMovement.y, 2))
hero_sprite.move(totalMovement * (1.f/1000.f)); //1/1000 should then be choosen bigger like 5
}
This will make your character move at the same speed independent from the distance to the mouse
AlexAUT