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

Author Topic: Problem with sprite movement.  (Read 6587 times)

0 Members and 1 Guest are viewing this topic.

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Problem with sprite movement.
« on: December 14, 2014, 10:47:30 pm »
I am new to SFML and am having a problem with my sprites and mouse events. I'm trying to make it so that when I right click somewhere, the sprite moves to that location slowly (not just warps). I have managed to get it partly working with only one problem. If I right click somewhere the sprite will move towards that position only a few pixels meaning I have to click over and over again until it gets there. Think of League of Legends or any RTS game for an example. You click on the map where you want the unit to go, and he goes. In my current code I must keep clicking as my sprite is only moving a few pixels per click, instead of a steady movement. Here is a snippet of what I've got so far, let me know if you need more.

                        case sf::Event::MouseButtonPressed:

                                if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right))
                                {
                                        sf::Vector2f totalMovement(sf::Mouse::getPosition(window).x - darthVader.getPosition().x, sf::Mouse::getPosition(window).y - darthVader.getPosition().y);

                                        darthVader.move(totalMovement * (1.f / 30.f));
                                }
 

Cpl.Bator

  • Hero Member
  • *****
  • Posts: 540
    • View Profile
Re: Problem with sprite movement.
« Reply #1 on: December 14, 2014, 11:46:15 pm »
your event was fired one time when you click.
you mix event and move, it's bad way. you must use some flag for your problem :


if (event.type == sf::Event::MouseButtonPressed)
{
    if (event.mouseButton.button == sf::Mouse::Right)
    {
        button_right = true;
    }
}

if (event.type == sf::Event::MouseButtonReleased)
{
    if (event.mouseButton.button == sf::Mouse::Right)
    {
        button_right = false;
    }
}



...
...
after event
...
...

if(button_right==true)
{
        // do move here
}
 

process your event , and after, and only after move your stuff.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #2 on: December 14, 2014, 11:51:08 pm »
Or more simply, he can use real-time input properly by calling sf::Mouse::isButtonPressed() unconditionally every frame, rather than only at the moment when the mouse button is first pressed.

If your goal was to check if the mouse pressed event was for the left or right mouse button, you're supposed to check event.mouseButton.button.  Reread the tutorial about all the event types.

Also, sf::Vector2f has overloaded arithmetic operators, so subtracting the mouse and sprite positions should be one operation, not two.

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #3 on: December 14, 2014, 11:52:43 pm »
Hmmm. Not sure I fully understand what you guys are saying. I'll read what you guys said over again and try to make sense of it.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #4 on: December 15, 2014, 12:08:58 am »
Basically, reread the two tutorials about this:

http://www.sfml-dev.org/tutorials/2.1/window-events.php <-- event-based input
http://www.sfml-dev.org/tutorials/2.1/window-inputs.php <-- real-time input

These are two very different ways of handling input that mean completely different things, so using both APIs to process a single input never really makes any sense, but that's what your snippet of code is doing.

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #5 on: December 15, 2014, 12:31:46 am »
Alright I am making progress here. I've changed the code up.

while (window.isOpen())
        {
                sf::Event event;

                if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                {
                        sf::Vector2f totalMovement(sf::Mouse::getPosition(window).x - darthVader.getPosition().x, sf::Mouse::getPosition(window).y - darthVader.getPosition().y);

                        darthVader.move(totalMovement * (1.f / 10.f));
                }

 

The problem now is that it all happens sooo fast. I'm pretty sure it's sliding/animating to the mouse position but at light speed. How would I slow that down?

Cpl.Bator

  • Hero Member
  • *****
  • Posts: 540
    • View Profile
Re: Problem with sprite movement.
« Reply #6 on: December 15, 2014, 12:37:37 am »
Probably my english is not correct... i'm french.

you mix Event ( probably in your event loop ) and movement.
you need to know , when an event was fired ( your mouse click ) , is fired just ONE time.
that's explain your problem : you click over and over again to reach the correct position.

and you mix real time input : sf::Mouse::isButtonPressed() and event input : pollEvent()
is not correct , listen Ixrec, and reread the tutorial and my first reply.


edit :

too fast ? use deltatime, use search on the forum.

@++

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #7 on: December 15, 2014, 12:41:06 am »
@Cpl.Bator: Too late, he already fixed that problem.

@GunnDawg: The 1/10 speed constant is entirely under your control.  Nothing's stopping you from changing it.

Remember, this is code that gets executed every single frame.  Add some cout statements to tell you how long each frame (ie, iteration of the game loop) actually takes, and how much you're moving the sprite each time.  It should be fairly obvious why it's "too fast".

I would worry about frame-independent movement (ie, deltatime) after you have this basic understanding of what your game loop is actually doing.  It is something you should do, but the mistakes you're making right now are even more important.

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #8 on: December 15, 2014, 12:49:39 am »
I guess I just don't know how to implement this.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #9 on: December 15, 2014, 01:14:05 am »
Your current speed:
darthVader.move(totalMovement * (1.f / 10.f));

One tenth of your current speed:
darthVader.move(totalMovement * (1.f / 100.f));

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #10 on: December 15, 2014, 01:24:18 am »
Yeah I've been messing with that and found that when changing it to (1.f / 100.f)); the sprite will move slower(good), but if I release the mouse button the sprite stops. I have to hold the mouse button down for the sprite to reach its destination(the mouse cursor). Seems like it would be as simple as changing sf::Mouse::isButtonPressed to sf::Mouse::isReleased, but that's not an option.
« Last Edit: December 15, 2014, 01:39:10 am by GunnDawg »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #11 on: December 15, 2014, 01:40:52 am »
If you wanted the sprite to move regardless of whether the mouse button is pressed or not, why would you only call move() when it is pressed? :/

In that case, you want a variable to remember the last place the user clicked, and every frame move the character toward that position.

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #12 on: December 15, 2014, 01:45:57 am »
If you wanted the sprite to move regardless of whether the mouse button is pressed or not, why would you only call move() when it is pressed? :/

In that case, you want a variable to remember the last place the user clicked, and every frame move the character toward that position.

I figured I was doing that already. Do you know of any video or text tutorials that teach doing EXACTLY what I am trying to do so I can follow it and just copy the code. I seem to learn better when seeing the code I'm trying to implement working, so I can then study it. Otherwise I'm just kind of lost and trying random things.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #13 on: December 15, 2014, 02:14:28 am »
The official SFML tutorials are always the safest bet (almost all the others we've ever seen are just wrong).  They have plenty of sample code snippets in them.  You need to read them all anyway to stand any chance of knowing what you're doing.

For complete programs, the examples that come with SFML itself should be fine for the really basic stuff like this.

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #14 on: December 15, 2014, 02:22:40 am »
so because  "sf::Mouse::isButtonPressed" isn't what I am after for this example then can you point me in another direction?

 

anything