#define mouseRPosX mouseRect.getPosition().x
#define mouseRPosY mouseRect.getPosition().y
#define mouseRSizeX mouseRect.getSize().x
#define mouseRSizeY mouseRect.getSize().y
#define rect1PosX rect1.getPosition().x
#define rect1PosY rect1.getPosition().y
#define rect1SizeX rect1.getSize().x
#define rect1SizeY rect1.getSize().y
i just defined some code for a rectshape and my drag ad select rectshape
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "TITLE", sf::Style::Close | sf::Style::Resize);
window.setFramerateLimit(60);
sf::RectangleShape rect1;
rect1.setSize(sf::Vector2f(50.0f, 50.0f));
rect1.setOutlineThickness(1.0f);
rect1.setOutlineColor(sf::Color::Red);
sf::RectangleShape mouseRect;
mouseRect.setSize(sf::Vector2f(0.0f, 0.0f));
mouseRect.setOutlineThickness(1.0f);
mouseRect.setOutlineColor(sf::Color::Green);
mouseRect.setFillColor(sf::Color(0, 100, 0, 100));
bool mouseSelecting = false;
bool objectSelected = false;
float mouseSelectPointX = 0;
float mouseSelectPointY = 0;
system("dir");
while (window.isOpen())
{
sf::Event events;
while (window.pollEvent(events))
{
switch (events.type)
{
case sf::Event::Closed:
{
window.close();
} break;
case sf::Event::MouseButtonPressed:
{
if(events.mouseButton.button == sf::Mouse::Left)
{
mouseSelecting = true;
mouseSelectPointX = mousePosX_F;
mouseSelectPointY = mousePosY_F;
mouseRect.setPosition(sf::Vector2f(mouseSelectPointX, mouseSelectPointY));
}
} break;
case sf::Event::MouseButtonReleased:
{
if (events.mouseButton.button == sf::Mouse::Left)
{
system("cls");
system("dir");
mouseSelecting = false;
mouseSelectPointX = mousePosX_F;
mouseSelectPointY = mousePosY_F;
if (((mouseRPosX + mouseRSizeX) >= rect1PosX &&
(mouseRPosY + mouseRSizeY) >= rect1PosY)
)
{
objectSelected = true;
}
else
{
objectSelected = false;
}
std::cout << "rec1Pos-x: " <<rect1.getPosition().x << "\nrec1Pos-y: " << rect1.getPosition().y << std::endl;
std::cout << "rect1Pos-x + rect1Siz-x: " << (rect1.getPosition().x + rect1.getSize().x) << "\nrect1Pos-y + rectSiz-y: " << (rect1.getPosition().y + rect1.getPosition().y) << std::endl;
std::cout << "mouseRectPos-x: " << mouseRect.getPosition().x << "\nmouseRectPos-y: " << mouseRect.getPosition().y << std::endl;
std::cout << "mouseRectPos-x + mouseRectSiz-x: " << (mouseRect.getPosition().x + mouseRect.getSize().x) << "\nmouseRectPos-y + mouseRectSiz-y: " << (mouseRect.getPosition().y + mouseRect.getSize().y) << std::endl;
mouseRect.setPosition(sf::Vector2f(0.0f, 0.0f));
}
} break;
}
}
window.clear(sf::Color(0, 0, 25, 255));
moveRect(rect1, 5.0f, 5.0f, window);
//std::cout << "object selected: " << objectSelected << std::endl;
if (mouseSelecting)
{
mouseRect.setSize(sf::Vector2f(mousePosX_F - mouseSelectPointX, mousePosY_F - mouseSelectPointY));
}
else
{
mouseRect.setSize(sf::Vector2f(0.0f, 0.0f));
}
if (objectSelected )
{
rect1.setFillColor(sf::Color::Green);
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right))
{
rect1.move(0.5f, 0.5f);
}
}
else
{
rect1.setFillColor(sf::Color::White);
}
window.draw(rect1);
window.draw(mouseRect);
window.display();
}
return 0;
}
if I press and drag toward +x(right) and + y(down) the "position" of "select rect" is in the top left corner which makes it easy to calculate collision relative to the object beeing selected from there.
however
if I press and drag toward -x(left) and -y(up) the"position" of "select rect" is in the bottom right corner which makes it weird to calculate collision from there.
it feels like i've implemented something wrong here.
any ideas?