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

Author Topic: Moving a sprite to a clicked point?  (Read 9580 times)

0 Members and 1 Guest are viewing this topic.

5gum

  • Newbie
  • *
  • Posts: 27
    • View Profile
Moving a sprite to a clicked point?
« 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Moving a sprite to a clicked point?
« Reply #1 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

5gum

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Moving a sprite to a clicked point?
« Reply #2 on: December 04, 2013, 06:10:47 pm »
Oh, that's a pity :(
Do you know which simple C++ library provides that?
Maybe SDL?
« Last Edit: December 04, 2013, 06:42:17 pm by 5gum »

5gum

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Moving a sprite to a clicked point?
« Reply #3 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 ;)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Moving a sprite to a clicked point?
« Reply #4 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 might help you. But you won't get around profound vector algebra knowledge if you want to develop games.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Moving a sprite to a clicked point?
« Reply #5 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"?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Moving a sprite to a clicked point?
« Reply #6 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
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

5gum

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Moving a sprite to a clicked point?
« Reply #7 on: December 04, 2013, 08:34:41 pm »
Thanks, that looks successfully, I'll try that tomorrow.

5gum

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Moving a sprite to a clicked point?
« Reply #8 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 ;)

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Moving a sprite to a clicked point?
« Reply #9 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).

5gum

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Moving a sprite to a clicked point?
« Reply #10 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

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Moving a sprite to a clicked point?
« Reply #11 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.