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

Author Topic: Problem with collision on Resizing  (Read 1495 times)

0 Members and 1 Guest are viewing this topic.

excentio

  • Newbie
  • *
  • Posts: 21
    • View Profile
Problem with collision on Resizing
« on: December 23, 2014, 12:35:18 am »
Hello guys, I have a problem, I draw some squares in my window and they change their color, when I guide my cursor over them, but when I change manually the screen size, their collision is still on the last point, where it was, when those buttons haven't been resized, so, when I guide my cursor over them, there's nothing, but when I guide my cursor on that last place, squares change their color, any ideas what can I do ?

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Problem with collision on Resizing
« Reply #1 on: December 23, 2014, 12:36:43 am »
Can you post the code you are using to resize and the code you are using the do the collision detection?

excentio

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Problem with collision on Resizing
« Reply #2 on: December 23, 2014, 01:03:52 am »
GUI.cpp code
Vector2f GUI::mousePosition(RenderWindow& _window){
        Vector2f pos(Mouse::getPosition(_window));
        return pos;
}

void GUI::CreateButton(Vector2f position, Vector2f size, String text, Texture buttonIMG[3])
{
                buttonSettings rect; // creating button

                rect.buttonIMG = buttonIMG[0];
                rect.buttonGUIDANCE = buttonIMG[1];
                rect.buttonCLICK = buttonIMG[2];

                rect.btns.setSize(size);
                rect.btnText.setFont(font);
                rect.btnText.setCharacterSize(16);
                rect.btnText.setColor(Color(200,200,200));
                rect.btnText.setString(text);
                rect.btnText.setPosition(rect.btns.getPosition().x + (rect.btns.getSize().x/2 - rect.btns.getSize().x/5) , rect.btns.getPosition().y + (rect.btns.getSize().y/3)); // setting text in the middle
                buttons.push_back(rect);
}
 
int GUI::OnButtonClick(RenderWindow& _window) {
        for(btnID = 0; btnID != buttons.size(); btnID++) {
                        FloatRect bounds(buttons[btnID].btns.getGlobalBounds());

                if(bounds.contains(mousePosition(_window)) && event.type == Event::MouseButtonPressed) {
                        if(event.mouseButton.button == Mouse::Left) {
                                                        buttons[btnID].btns.setTexture(&buttons[btnID].buttonCLICK); // on button click
                                return btnID;
                        }
                }
                                if(bounds.contains(mousePosition(_window))) { // on button guidance
                                        buttons[btnID].btns.setTexture(&buttons[btnID].buttonGUIDANCE);
                                }
                                else {
                                  buttons[btnID].btns.setTexture(&buttons[btnID].buttonIMG); // button idle
                                }

        }
}

GUI.h
typedef struct buttonSettings {
        Text btnText;
                RectangleShape btns;
                Vector2f size;
                Texture buttonIMG, buttonGUIDANCE, buttonCLICK;
        };

vector<buttonSettings> buttons;
 

main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;
using namespace sf;

Texture img[3];

string getPath()
{
  char result[ MAX_PATH ];
  return string( result, GetCurrentDirectory(100, result));
}

int main() {
        RenderWindow window(VideoMode(1024,768), "game"); // window initialisation
        window.clear();

        for(unsigned short i = 0; i<3; i++) {
                img[i].loadFromFile(getPath() + "\\GUI\\BUTTON\\BUTTON_" + to_string(i) + ".png");
        }

        gui.CreateButton(Vector2f(window.getSize().x/2, window.getSize().y/18), Vector2f(200, 70), "", img); // creating button
         window.display();

 while(window.isOpen()) {
while(window.pollEvent(Event event) {
gui.OnButtonClick(window);
}
}
}

here, all working code, but textures changes, not color and I'm resizing window manually, so all the elements are scaling

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with collision on Resizing
« Reply #3 on: December 23, 2014, 03:03:07 am »
Sounds like you're not converting the co-ordinates from actual pixel co-ordinates (the mouse position) to the world co-ordinates (the positions of the graphics in the view).
This should solve your problem:
http://www.sfml-dev.org/tutorials/2.2/graphics-view.php#coordinates-conversions
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

excentio

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Problem with collision on Resizing
« Reply #4 on: December 23, 2014, 12:06:38 pm »
Sounds like you're not converting the co-ordinates from actual pixel co-ordinates (the mouse position) to the world co-ordinates (the positions of the graphics in the view).
This should solve your problem:
http://www.sfml-dev.org/tutorials/2.2/graphics-view.php#coordinates-conversions
Thanks dude, I'll give you a cookie, works now :D