SFML community forums

Help => General => Topic started by: Stuff on January 17, 2020, 04:08:14 am

Title: How to create a point and click mechanism
Post by: Stuff 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) 
Title: Re: How to create a point and click mechanism
Post by: Laurent 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 :)
Title: Re: How to create a point and click mechanism
Post by: Stuff 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.
Title: Re: How to create a point and click mechanism
Post by: Laurent 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 ;)
Title: Re: How to create a point and click mechanism
Post by: Stuff 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. 
Title: Re: How to create a point and click mechanism
Post by: Laurent 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 ;)
Title: Re: How to create a point and click mechanism
Post by: Tigre Pablito 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.
Title: Re: How to create a point and click mechanism
Post by: Stuff 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.
Title: Re: How to create a point and click mechanism
Post by: Tigre Pablito 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?
Title: Re: How to create a point and click mechanism
Post by: Stuff 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