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

Author Topic: sf::ConvexShape and sf::Mouse  (Read 3578 times)

0 Members and 3 Guests are viewing this topic.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
sf::ConvexShape and sf::Mouse
« on: December 21, 2020, 09:20:00 pm »
Hello,

Hope you all are doing well and getting ready for the holidays. I had a quick question regarding sf::ConvexShape.

sf::Vector2f startPos{ 0,0 };
        sf::ConvexShape shape{ 3 };
        shape.setPoint(0, { 100,0 });
        shape.setPoint(1, { 100,100 });
        shape.setPoint(2, startPos);

        shape.setFillColor(sf::Color::Transparent);
        shape.setOutlineThickness(5.f);
        shape.setOutlineColor({ 120,0,0 });
        shape.setOrigin(150, 150);
        shape.setPosition(640, 384);

and in update:
auto mousePos = sf::Mouse::getPosition(window);
if(clicked)
        shape.setPoint(2, sf::Vector2f(mousePos));
 

Now all I'm trying to do is have point 2 follow the mouse position. The mouse position seems way off though. I'm not using a view, it's just a barebone project. I've tried sf::Mouse::getPosition(window), I've tried using mapPixelToCoords(sf::Mouse::getPosition(window)). But mapCoordsToPixel has got me the most accurate results, yet still it's off.

Anyone see what my problem could be? I'm trying to setup a little sandbox to really get familiar and have a good understanding with cos/sin.

Thanks!
« Last Edit: December 21, 2020, 09:21:37 pm by SFMLNewGuy »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::ConvexShape and sf::Mouse
« Reply #1 on: December 22, 2020, 08:11:15 am »
The shape's points are defined in local cooridinates. This means that the position and origin of the shape, which are non-zero in your example, are then applied to produce the final positions. If you want the point to be exactly where the mouse is, then your shape can't have a position and origin (or you must take them in account when computing the local position of the point).
Laurent Gomila - SFML developer

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: sf::ConvexShape and sf::Mouse
« Reply #2 on: December 22, 2020, 08:42:52 pm »
EDIT: I got the mouse to be pin-point, by removing the origin, but my question regarding it would still be nice to be answered. Thanks

Hey Laurent,

Thanks for helping me again, hope you are doing well. Do you mind explaining why it can't have an origin and position? I understand that they are local coordinates and then the setPosition puts them where you want.

I got the mouse position relative to the window and then subtracted the position of the triangle. It's pretty close, but based on what you said, it requires some change to be exact/pin-point. I'm also confused about why this is different than using like sf::CircleShape(10,3) and doesn't respond with the mouse the same?

The reason I'm not using the circle shape for a triangle is I didn't see a way to set individual points, thus I'm using sf::ConvexShape.

Below is a picture of my project. The white arrow is approximately where my mouse is.

Uploaded picture: https://ibb.co/7zGXd2K
Source Code: https://codeshare.io/5v3xw4
« Last Edit: December 22, 2020, 08:50:17 pm by SFMLNewGuy »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::ConvexShape and sf::Mouse
« Reply #3 on: December 23, 2020, 09:21:18 am »
Quote
Do you mind explaining why it can't have an origin and position?
It can, but these will offset the final position of the point, and it will not be under the mouse. Unless, as I said, you take them in account when computing the local coordinates of the point.

Quote
I'm also confused about why this is different than using like sf::CircleShape(10,3) and doesn't respond with the mouse the same?
I don't understand the question. How can you compare that to sf::CircleShape, since you can't set the position of the circle's points like you do with a convex shape?
Laurent Gomila - SFML developer

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: sf::ConvexShape and sf::Mouse
« Reply #4 on: December 24, 2020, 01:52:16 am »
Thanks Laurent,

Quote
I don't understand the question. How can you compare that to sf::CircleShape, since you can't set the position of the circle's points like you do with a convex Shape?

Yeah, sorry for not explaining that better. I just thought since you can set the position of the sf::CircleShape(10,3) and it takes into consideration the origin and local points already that sf::ConvexShape would do the same? Does that make more sense? I thought it would be the same based on being derived from shape. Unless I'm just not understanding it correctly.

Or was it purposely left like that because at specific times you would need it to not take the position and origin into account? It's the only reason I could think of and I don't know why setPosition wouldn't take it all into account? Maybe that's a stupid question  :-[, sorry  ;D?

Thanks Laurent.

PS: Random question about this message board, how do you highlight a word on here so that it has the quote backdrop behind it but still stays in the sentence? Usually, where I make a word bold, people put that quote backdrop behind it too make it easier to read.

TLDR; Why are the setPosition functions of sf::ConvexShape and sf::CircleShape different? One takes into account the local and origin and the other you have to manually add them. In what type of situation would you need it like that? Thanks

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::ConvexShape and sf::Mouse
« Reply #5 on: December 24, 2020, 08:20:33 am »
setPosition is exactly the same for ConvexShape and CircleShape. It's also the same for all SFML entities, since it's defined in the Transformable base class.

I'm sorry but I really don't understand your question/problem, so I can't give you a correct answer.

A Shape, either a convex shape that you define manually or another pre-defined one for which SFML computes the points, has a set of points defined in local coordinates. Then, like any other drawable/transformable entity, these points are transformed with the origin, translation, rotation and scale of the entity to produce their final position on screen. I don't know what else to say.
Laurent Gomila - SFML developer