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

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

0 Members and 1 Guest are viewing this topic.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with sprite movement.
« Reply #15 on: December 15, 2014, 03:16:16 am »
I think I understand the interface you're trying to achieve. Click once and it'll travel to that location regardless of what you are doing with the mouse afterwards unless you click for it to go somewhere else.
If thats right, I imagine the process would be something as follows:
  • test the mouse event to see if there's been a right-click,
  • if so, store the current mouse position in a variable called target,
  • test to see if the sprite isn't already at the target location,
  • if it isn't, move the sprite towards the target location.
The first part (first two bullet points) is working with events and is in the event loop. As linked earlier, you'll need to understand this: http://www.sfml-dev.org/tutorials/2.1/window-events.php
The last part (last two bullet points) is similar to what you were doing already except you move towards the stored target location instead of the mouse position.

As a final note, to regulate the speed of the sprite, you could multiply the movement by the amount of time passed in that frame. This is known as delta time.
For working with time, look at this tutorial about using sf::Clock: http://www.sfml-dev.org/tutorials/2.1/system-time.php .
For more indepth timing control, have a read of this article: http://gafferongames.com/game-physics/fix-your-timestep/ (it's a bit more advance and I have had to read it several times to fully comprehend it but it's really worth it!)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #16 on: December 15, 2014, 03:35:34 am »
Thanks. I suppose I'll scrap this and start over with a new approach.

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #17 on: December 15, 2014, 08:32:33 am »
So I've come up with this solution that seems to do the trick. I'll post my solution/code for you to see. Like I said "it works" though I'm not sure if this is the proper or most efficient way to do something like this. Can it be done easier? Will this bite me in the rear later on? Any input is appreciated.

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

                if (sf::Mouse::isButtonPressed(sf::Mouse::Right) && totalMovementSet == false)
                {
                        totalMovementSet = true;
                        totalMovement.x = sf::Mouse::getPosition(window).x - darthVader.getPosition().x;                       
                        totalMovement.y = sf::Mouse::getPosition(window).y - darthVader.getPosition().y;

                        mousePoint.x = sf::Mouse::getPosition(window).x;
                        mousePoint.y = sf::Mouse::getPosition(window).y;
                }

                if (totalMovementSet == true)
                {
                        darthVader.move(totalMovement * (1.f / 8000.f));
                }

                if (darthVader.getPosition().x > mousePoint.x - 10 && darthVader.getPosition().x < mousePoint.x + 10 && darthVader.getPosition().y > mousePoint.y - 10 &&
                        darthVader.getPosition().y < mousePoint.y + 10)
                {
                        totalMovementSet = false;
                }
 

the sprite now moves to the location that I right click and stops until I click somewhere else.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #18 on: December 15, 2014, 08:40:30 am »
Yes, that's exactly how it should be done.

Now that the structure is correct, the only significant issues I see are smaller things like:

- The
&& totalMovementSet == false
explicitly prevents the player from changing totalMovement until the current "movement" is completed; are you sure you want to do that?

- The vector operations in your code can be significantly simplified in a few places:
            totalMovementSet = true;
            totalMovement = sf::Mouse::getPosition(window) - darthVader.getPosition();
            mousePoint = sf::Mouse::getPosition(window);
and
sf::Vector2f distFromMouse = darthVader.getPosition() - mousePoint;
if (abs(distFromMouse.x) < 10 && abs(distFromMouse.y) < 10) { // I think this is what your if() means

- Technically, in this snippet you're declaring an sf::Event object and then never using it, but I assume you'll want to write at least a line of code to handle the Close event later, so it's not exactly a problem.
« Last Edit: December 15, 2014, 08:47:59 am by Ixrec »

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #19 on: December 15, 2014, 08:47:00 am »
Correct that is not what I want to happen. I'd like for them to be able to move where and when they want and not be locked out while moving from point A to point B


EDIT: I do use that event further down for like you said, to close. I only provided essential code for this.
« Last Edit: December 15, 2014, 08:49:37 am by GunnDawg »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with sprite movement.
« Reply #20 on: December 15, 2014, 03:25:23 pm »
You could use events instead to respond to mouse clicks if you wanted to only change the target when clicked but if you want the target to follow the mouse when dragging with the right button, your approach looks great (although you technically can use events for that too).

I'm glad you got it to do what you were looking for and didn't just give up. It sounded like you were going to do that when you wrote "I'll scrap this"  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #21 on: December 15, 2014, 10:45:46 pm »
You could use events instead to respond to mouse clicks if you wanted to only change the target when clicked but if you want the target to follow the mouse when dragging with the right button, your approach looks great (although you technically can use events for that too).

I'm glad you got it to do what you were looking for and didn't just give up. It sounded like you were going to do that when you wrote "I'll scrap this"  :P

I would rather it not follow the mouse on right click but instead go to where the mouse cursor is at when right clicking, which is does fine at the moment. I am now trying to get it to move to a different mouse location in the middle of its movement if case the player decides before he gets to the destination that he wants to go somewhere else. Take an RTS game for example. Lets say you accidentally start sending your army/units into the enemy base, you would be able to go "wow wait, no", and right click somewhere else. Trying to do that. At the moment once you right click somewhere, you're committed to moving there, not good!

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #22 on: December 15, 2014, 10:57:30 pm »
Simply removing the "&& totalMovementSet == false" should accomplish that.

GunnDawg

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Problem with sprite movement.
« Reply #23 on: December 16, 2014, 12:03:24 am »
Simply removing the "&& totalMovementSet == false" should accomplish that.

Ah yes, that did the trick. Sorry for the beginner oversights. I still have issues comprehending what some stuff is doing and why. Appreciate the help guys. I've accomplished (with your guys help of course) what this thread was designed for :)