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

Author Topic: Problem moving the character to mouse coords  (Read 2496 times)

0 Members and 1 Guest are viewing this topic.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Problem moving the character to mouse coords
« on: April 05, 2017, 11:25:12 pm »
Hi folks

I'm making a grid based diablo-like game and I have a question to ask:

currently I'm using this for moving the player from tile to tile:

sf::Vector2f direction = sf::Vector2f(mouse.x, mouse.y) - player.getPosition();
float magnitude = sqrt((direction.x * direction.x) + (direction.y * direction.y));
sf::Vector2f unitVector(direction.x / magnitude, direction.y / magnitude);
player.move(unitVector * PlayerSpeed * elapsed_time.asSeconds());

the problem is - the player doesn't stop moving! ever!

I tried putting the last line in an if loop like - if player position != mouse coords - but couldn't make it work.

How do I make the player stop once it reaches the clicked tile (mouse.x and y)?


Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem moving the character to mouse coords
« Reply #1 on: April 05, 2017, 11:32:50 pm »
If magnitude is zero (or maybe even close enough), you don't need to (indeed should not) calculate unitVector as it's dividing by zero. Also, if the magnitude's considered zero, there's no need to move the player ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Problem moving the character to mouse coords
« Reply #2 on: April 05, 2017, 11:42:38 pm »
To expand on Hapax's answer, if your magnitude is less than the player's speed you will overshoot the clicked tile and move past it. You should be able to check for this condition and simply put your player at the clicked tile's location if the magnitude is smaller than the player's speed.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Problem moving the character to mouse coords
« Reply #3 on: April 06, 2017, 06:56:25 am »
ok so let's say the player starts at position 0,32 ...

I click on 192,128 to move the player

that makes the magnitude 214.663 (it's 4 isometric tiles away)

PlayerSpeed is 100f

it just keeps going!

I'm using 128x64 isometric tiles,
my "test player" is a circle with a radius of 10, centered on a tile
since it's grid based, the minimum distance to move is never less than a whole tile.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10998
    • View Profile
    • development blog
    • Email
Re: Problem moving the character to mouse coords
« Reply #4 on: April 06, 2017, 10:59:01 am »
Code doesn't just become sentient, so you can assume that something in the code is wrong. In order to figure out what is wrong, you first check with pen and paper, whether your math is correct.
If it is correct, then you go on and check whether all the values are correct. Is the mouse position what you expect it to be (is it relative to the window and is it in the world coordinates)? Is the player position as expected? Are the calculated values as expected?
If all the values are what you expect them to be, you can move on to checking whether the game logic makes sense. Does your code ensure that the player doesn't move at one point?

For checking values you can either use the debugger and its variable inspection functionality or use simple std::cout statements.

My guess would be right now, that you're not properly get the mouse position.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Problem moving the character to mouse coords
« Reply #5 on: April 06, 2017, 01:32:53 pm »
My values are correct because when I use a simple "player.setPosition" instead of the code I posted in my original post, the player is relocated to the exact position where I click.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Problem moving the character to mouse coords
« Reply #6 on: April 06, 2017, 04:39:30 pm »
Solved for now, thank you for all the replies.

Made an if loop to check positions. Previously tried the same thing with a while loop but it kept crashing for some reason.

 

anything