SFML community forums

Help => Graphics => Topic started by: 5gum on December 04, 2013, 05:16:03 pm

Title: Moving a sprite to a clicked point?
Post by: 5gum on December 04, 2013, 05:16:03 pm
Hey,
Is it possible to move a sprite to a clicked position?
So not only set a new position of the sprite but really move it with "animation".
Currently I've just a line from the center of the sprite to the clicked point. Maybe it can help, maybe not...
Or is there already a simple command in SFML and I just don't know it?

I'm beginner in SFML so please don't be too strict and if it's possible tell me a simple solution ;)

Thanks,
5gum
Title: AW: Moving a sprite to a clicked point?
Post by: eXpl0it3r on December 04, 2013, 05:29:43 pm
You'll have to do the interpolation between point A and B yourself. SFML doesn't provide such a feature. ;)
Title: Re: Moving a sprite to a clicked point?
Post by: 5gum on December 04, 2013, 06:10:47 pm
Oh, that's a pity :(
Do you know which simple C++ library provides that?
Maybe SDL?
Title: Re: Moving a sprite to a clicked point?
Post by: 5gum on December 04, 2013, 07:41:27 pm
Is there no one who can call me a library which supports my wish?

My game and me would be really happy ;)
Title: Re: Moving a sprite to a clicked point?
Post by: Nexus on December 04, 2013, 08:18:25 pm
It's very easy to implement.

1. Compute the vector from sprite position to mouse position by taking their difference
2. Scale that vector to the desired velocity, yielding v
3. During each frame of length dt, move the sprite by v * dt

For the vector operations, the Thor library (http://www.bromeon.ch/libraries/thor/v2.0/doc/_vector_algebra2_d_8hpp.html) might help you. But you won't get around profound vector algebra knowledge if you want to develop games.
Title: Re: Moving a sprite to a clicked point?
Post by: Raincode on December 04, 2013, 08:20:23 pm
A-----
 .----
  .--y
   .--
    .-
--x--B
 

You want to get from point A to B. Calculate the total difference which you habe to move:

x = B.x - A.x
y = B.y - A.y

example:
A(2|5)
B(6|3)
Your total movement: (4,-2);

Now if you want a fluid movement, apply a factor 0 < f < 1 to this. Say you want the animation to take one second and you want 30 FPS.

GameLoop:
OnClick:
totalMovement.x = mousePos.x - ObjectPos.x
totalMovement.y = mousePos.y - ObjectPos.y
Object.move(totalMovement * (1/30)) //PseudeCode. Make sure you use floating point division
 

Edit: Too slow, I posted it anyway.

P.S.: What is ment by "scaling that vector to the desired velocity, yielding v"?
Title: Re: Moving a sprite to a clicked point?
Post by: Nexus on December 04, 2013, 08:31:35 pm
You want to get from point A to B. Calculate the total difference which you habe to move:

x = B.x - A.x
y = B.y - A.y
Don't do it component-wise. That's exactly why we have vectors:
sf::Vector2f diff = B - A;


P.S.: What is ment by "scaling that vector to the desired velocity, yielding v"?
The difference vector will not always have the same length, but you probably want to move the object at a constant velocity. Therefore, you have to scale the vector to a specific length. With Thor, you can do that as follows:
sf::Vector2f diff = ...;
float speed = ...;
sf::Vector2f v = speed * thor::unitVector(diff);
// v has length speed, and same direction as diff
Title: Re: Moving a sprite to a clicked point?
Post by: 5gum on December 04, 2013, 08:34:41 pm
Thanks, that looks successfully, I'll try that tomorrow.
Title: Re: Moving a sprite to a clicked point?
Post by: 5gum on December 05, 2013, 01:24:19 pm
Sorry for my bad knowledge but I learn SFML since 4 days.

@Raincode, I tried your code:

if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Right))
{
sf::Vector2f totalMovement(sf::Mouse::getPosition(mMainWindow).x - rect_car.getPosition().x, sf::Mouse::GetPosition(mMainWindow).y - rect_car.getPosition().y);

car.move(totalMovement * (1/30));
}
 

But then he underlines the * on line 5
What do I wrong?

PS: Sry for my bad English, if it's possible speak simple English please ;)
Title: Re: Moving a sprite to a clicked point?
Post by: fallahn on December 05, 2013, 03:23:16 pm
totalMovement is a vector of float type so can only be multiplied by float values. Try (1.f / 30.f).
Title: Re: Moving a sprite to a clicked point?
Post by: 5gum on December 05, 2013, 03:34:41 pm
Lots of Thanks, it works :)
Now I set you all to the second page of the game credits :D
Title: Re: Moving a sprite to a clicked point?
Post by: Raincode on December 05, 2013, 08:57:46 pm
Lol just for future reference, don't expect my weird code to work^^

Thanks for the explanation, Nexus!;) I tested it again and noticed exactly that problem.