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

Author Topic: How to create a point and click mechanism  (Read 5086 times)

0 Members and 1 Guest are viewing this topic.

Stuff

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
How to create a point and click mechanism
« on: January 17, 2020, 04:08:14 am »
I have been trying to make an online civil war RTS and then i came into a problem.
For about a week I've been trying to find a way to make a mechanism that makes the player follow the mouse when clicked. I've tried using 'for' loops and that was a mess, i even tried using one of the solutions from a another post back in 2013, i got an error.

here was the code i used,

 
sf::Vector2f totalMovement(sf::Mouse::getPosition(window).x) - battalion.getPosition().x, sf::Mouse::GetPosition(window).y - battalion.getPosition().y;

battalion.move(totalMovement * (1.f/30.f));

it would be a huge thanks if you could fix this code, or provide me new ones.
I've been using sfml for 3 weeks now so don't expect me to know everything yet.

Sincerely Stuff.

(i have the codeblocks version of sfml) 
« Last Edit: January 17, 2020, 09:07:12 pm by Stuff »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to create a point and click mechanism
« Reply #1 on: January 17, 2020, 08:32:29 am »
Here is a commented (untested) code with step by step explanations:

// on click event
{
    auto clicked = window.convertPixelToCoords(sf::Mouse::getPosition(window)); // get clicked position converted to scene coordinates
    auto direction = clicked - battalion.getPosition(); // compute direction to travel to reach the clicked point
    direction = direction / std::sqrt(direction.x * direction.x + direction.y * direction.y); // normalize
}

// at every update
constexpr auto speed = 10.; // some constant, number of scene units to travel per second
car.move(direction * speed); // why 'car' while it was 'battalion' before??

// probably some check to see if entity has reached its destination, to stop moving

Don't hesitate to ask for more details :)
Laurent Gomila - SFML developer

Stuff

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: How to create a point and click mechanism
« Reply #2 on: January 17, 2020, 11:25:50 pm »
thank you for helping, but the line of code,
window.convertPixelToCoords
does not compile stating that the window (the main window) doesn't go with the .convertPixelToCoords line.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to create a point and click mechanism
« Reply #3 on: January 18, 2020, 09:26:29 am »
It's mapPixelToCoords, sorry.

But this was just a starting point, to give you an idea of how it could be done. It's not supposed to be a working solution that you'd just copy and paste ;)
Laurent Gomila - SFML developer

Stuff

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: How to create a point and click mechanism
« Reply #4 on: January 21, 2020, 01:46:08 am »
Big thanks for helping me.

it works!

The only thing I need last is a code that makes the player keep moving to the direction of the mouse
after the click, is it possible?

Sincerely stuff. 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to create a point and click mechanism
« Reply #5 on: January 21, 2020, 07:56:17 am »
This is really straight-forward. You should try by yourself, and if you have issues then show us what you did and we'll help you to make it work ;)
Laurent Gomila - SFML developer

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: How to create a point and click mechanism
« Reply #6 on: January 22, 2020, 07:54:55 pm »
Hi Stuff

If you want the player to run after the mouse cursor, then you can do this (on every frame in your main loop):

int x = mouseX - playerX;
int y = mouseY - playerY;

double angle = Math.Atan2(y, x);   // I don't know if in C++ it is std::atan2(y, x);

double playerStep = 5;  // or what you prefer

playerX += Math.Cos(angle) * playerStep;
playerY -= Math.Sin(angle) * playerStep;


This would make the player run after the mouse cursor. I don't know if any sign (+/-) is wrong, I always get a bit confused with angles, but if it is, it is just that.

Stuff

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: How to create a point and click mechanism
« Reply #7 on: January 26, 2020, 07:48:35 pm »
what I meant by:


a code that makes the player keep moving to the direction of the mouse
after the click


is when after you click to the direction where you want the player it goes there and at the same time, you don't have to hold the mouse just for the player to follow it. What I'm saying is it's like an RTS point and click


It would be a joy for anyone to try to help.

Sincerely Stuff.

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: How to create a point and click mechanism
« Reply #8 on: January 27, 2020, 10:54:22 pm »
Hi Stuff

The idea is the same, when you click, you would need to get the mouse (x, y), then you would need to make the player just go to that point, and the way to do that is the same, with angles and trigo functions. This, if you want to make the player go in a direct way (oblique), I mean, not going like squares.

Another thing I would add is to check when the player has reached the point, and then make it stop.

And what about if you click again before it has reached the point?

Stuff

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: How to create a point and click mechanism
« Reply #9 on: February 22, 2020, 11:21:32 pm »
Dear Tigre Pablito,

Is there any way you can make the code more simplified and more math savvy to lessen complications in the future. It would be a huge help.

Sincerely Stuff