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

Author Topic: Better Solution for this? - MoveObject To Current Mouse Position  (Read 1747 times)

0 Members and 1 Guest are viewing this topic.

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Hi there, I have some code to move an object in sync to the mouse moment in X... I am using getPostion(rw); but I was wondering if this is the best way to do it.. my application seams to be having a slight strobie look to it.. and I was wondering if this can be fixed.. maybe my methods is not a good solution.

I am passing a float for time, but as of yet it is not getting used.. and the frame rate of the window is set to 60.

void goArkanoid::Update(sf::RenderWindow& rw, float TimeElapsed)
{
        sf::Vector2f CurrentMousePos((float)sf::Mouse::getPosition(rw).x, 671);
        sf::Event event;
        rw.pollEvent(event);
       
        if (event.type == sf::Event::MouseMoved)
        {

                if (GetPosition().x < sf::Mouse::getPosition(rw).x)
                {
                        GameObject::SetTile(RIGHT);
                }

                if (GetPosition().x > sf::Mouse::getPosition(rw).x)
                {
                        GameObject::SetTile(LEFT);
                }

                GameObject::SetPosition(CurrentMousePos);
                std::cout<<"xM = "<<CurrentMousePos.x<<"\n"<<" "<<sf::Mouse::getPosition().x<<"\n\n";
        }

        if (GetPosition().x == sf::Mouse::getPosition(rw).x)
        {
                GameObject::SetTile(STOPPED);
        }
}
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Better Solution for this? - MoveObject To Current Mouse Position
« Reply #1 on: June 24, 2012, 09:20:44 am »
I don't think you need both realtime input (sf::Mouse) and one-time events (sf::Event). But if you use the latter, always have a loop to poll the window, or you might lose events.

What do you exactly want to do? To move an object depending on the position or on the movement (i.e. change in position) of the mouse cursor?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: Better Solution for this? - MoveObject To Current Mouse Position
« Reply #2 on: June 24, 2012, 12:22:15 pm »
are you meant to have a single event for the entire window.. or lots of events all over the place? I have changed the code now that a reference to the window event is sent into all function and nothing seamed to change apart form there is no event in a loop anymore.

Um I am just fooling about with the moment class for my objects, this is the mouse movement part.. but I am also doing a free moving one , and I have see ncode that uses some kind of mathematics based on time to get a velocity.. but I am not sure I understand it
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Better Solution for this? - MoveObject To Current Mouse Position
« Reply #3 on: June 24, 2012, 02:16:56 pm »
You didn't answer my question. As long as we don't know how the objects should behave depending on your mouse position, it's difficult to help you.

And take a look at the SFML tutorials and the API documentation to see how events and realtime input are used correctly.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything