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

Author Topic: Issue with Joystick axes  (Read 2680 times)

0 Members and 1 Guest are viewing this topic.

rubikshift

  • Newbie
  • *
  • Posts: 4
    • View Profile
Issue with Joystick axes
« on: June 13, 2013, 07:54:02 pm »
Hi,
On the beginning i'd like to say, i'm learning English for few years, and i could make mistakes (wrong write something and etc.).
Few days ago i started learning sfml 2.0 on Visual Studio 2012. I created a simple program, which draw a shape and user can move it using joystick. I have a issue with axes, in source code i use only axis X and Y, but when i start my program and tried it i saw that when i do anything with joystick a shape moves. When i pressed A, B, X, Y or use right analog stick. I want that the shape moves only when i use left analog stick.
Please help.

kralo9

  • Newbie
  • *
  • Posts: 44
    • View Profile
    • Email
Re: Issue with Joystick axes
« Reply #1 on: June 13, 2013, 08:22:25 pm »
I think you are mixing up joystick with controller. Controller is the thing for xbox/ps/etc with 2 sticks and joystick is for the computer with one stick. Maybe this is your problem and I think that controllers aren't supported by SFML...
I'm totally exhausted. I've got 3 children and no money! Why can't I have no children and 3 money? - Homer S.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Issue with Joystick axes
« Reply #2 on: June 14, 2013, 08:06:58 am »
Joystick in SFML means "a device with buttons and axes".

Please show your code, it seems like you're doing something wrong.
Laurent Gomila - SFML developer

rubikshift

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Issue with Joystick axes
« Reply #3 on: June 14, 2013, 06:54:24 pm »
#include <SFML\Graphics.hpp>
#include <SFML\System\Clock.hpp>
#include <iostream>

int main()
{
        sf::Clock clock;
        sf::ContextSettings settings;
        sf::Vector2f wektor(sf::Joystick::getAxisPosition(0, sf::Joystick::X), sf::Joystick::getAxisPosition(0, sf::Joystick::Y));

        sf::CircleShape ksztalt (50);
        ksztalt.setPosition(400,300);
        ksztalt.setFillColor(sf::Color::Green);
        ksztalt.setOutlineColor(sf::Color::Black);
        ksztalt.setOutlineThickness(8);

        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window", sf::Style::Default, settings);
       
        settings.antialiasingLevel = 16;

        if(sf::Joystick::isConnected(0))
                std::cout << "Joystick is connected :)" << std::endl;
        else
                std::cout <<"Joystick is not connected :(" << std::endl;

        int buttoncount = sf::Joystick::ButtonCount;
                std::cout << buttoncount << std::endl;

        while (window.isOpen())
        {

                wektor.x = sf::Joystick::getAxisPosition(0, sf::Joystick::X);
                wektor.y = sf::Joystick::getAxisPosition(0, sf::Joystick::Y);
               
                sf::Joystick::update();

                window.clear(sf::Color::White);
                window.draw(ksztalt);
                window.display();
       
                sf::Event event;

        while (window.pollEvent(event))
        {      
                        if (event.type == sf::Event::Closed)
                window.close();
                       
                        else   
                       
                                if(sf::Joystick::isButtonPressed(0, 1))
                                        ksztalt.setFillColor(sf::Color::Green);
                               
                                if(sf::Joystick::isButtonPressed(0, 2))
                                        ksztalt.setFillColor(sf::Color::Red);
                       
                                if(sf::Joystick::isButtonPressed(0, 3))
                                        ksztalt.setFillColor(sf::Color::Yellow);

                                if(sf::Joystick::isButtonPressed(0, 0))
                                        ksztalt.setFillColor(sf::Color::Blue);

                                ksztalt.move(wektor.x*clock.getElapsedTime().asSeconds()/15, wektor.y*clock.getElapsedTime().asSeconds()/15);

                }
        }
        return EXIT_SUCCESS;
}

Ok, here is a code.
« Last Edit: June 14, 2013, 07:11:53 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Issue with Joystick axes
« Reply #4 on: June 14, 2013, 07:17:13 pm »
You're doing several mistakes that are explicitely documented in the tutorials ;)

1. Calling any sf::Joystick function before sf::Joystick::update will give wrong results (cf. tutorial).

2. You don't need sf::Joystick::update inside your game loop, since you have an event loop (cf. tutorial).

3. Mixing event loop and real-time inputs doesn't make sense (cf. tutorial).

4. Your 'else' only applies to the first 'if'; it seems like you forgot to put everything inside a { } block.

5. You move your shape whenever any event occurs. You should rework your logic ;)
Laurent Gomila - SFML developer

rubikshift

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Issue with Joystick axes
« Reply #5 on: June 14, 2013, 09:46:36 pm »
Thanks Laurent,
I'm just learning SFML (also c++, but longer). I like when someone give me advice (most when it is about elementary things  :) ). I'll try tomorrow rework my program.

rubikshift

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Issue with Joystick axes
« Reply #6 on: June 16, 2013, 12:42:05 pm »
I tried to rework the code, but I' don't know what i'm doing wrong. I don't understand everything what i should do. I don't know what else i should do.
[EDIT] Ok, it works, i maybe i did something wrong in actual version, but it works.
#include <SFML\Graphics.hpp>
#include <SFML\System\Clock.hpp>
#include <iostream>

int main()
{
        sf::Clock clock;
        sf::ContextSettings settings;
       

        sf::CircleShape ksztalt (50);
        ksztalt.setPosition(400,300);
        ksztalt.setFillColor(sf::Color::Green);
        ksztalt.setOutlineColor(sf::Color::Black);
        ksztalt.setOutlineThickness(8);

        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window", sf::Style::Default, settings);
       
        settings.antialiasingLevel = 16;

        if(sf::Joystick::isConnected(0))
                std::cout << "Joystick is connected :)" << std::endl;
        else
                std::cout <<"Joystick is not connected :(" << std::endl;

        int buttoncount = sf::Joystick::ButtonCount;
                std::cout << buttoncount << std::endl;
        sf::Vector2f wektor(sf::Joystick::getAxisPosition(0, sf::Joystick::X), sf::Joystick::getAxisPosition(0, sf::Joystick::Y));

        while (window.isOpen())
        {
float cos1 = wektro.x;
float cos2 = wektor.y;
                window.clear(sf::Color::White);
                window.draw(ksztalt);
                window.display();
       
                sf::Event event;

        while (window.pollEvent(event))
        {      
                        if (event.type == sf::Event::Closed)
                window.close();
                }      
                wektor.x = sf::Joystick::getAxisPosition(0, sf::Joystick::X);
                wektor.y = sf::Joystick::getAxisPosition(0, sf::Joystick::Y);
                if((wektor.x != cos1) || (wektor.y != cos2) || ((wektor.x != cos1) && (wektor.y != cos2)))
                        ksztalt.move(wektor.x/10, wektor.y/10);
                std::cout << wektor.x << "\t\t" << wektor.y << std::endl;
                if(sf::Joystick::isButtonPressed(0, 1))
                        ksztalt.setFillColor(sf::Color::Green);
                               
                if(sf::Joystick::isButtonPressed(0, 2))
                        ksztalt.setFillColor(sf::Color::Red);
                       
                if(sf::Joystick::isButtonPressed(0, 3))
                        ksztalt.setFillColor(sf::Color::Yellow);

                if(sf::Joystick::isButtonPressed(0, 0))
                        ksztalt.setFillColor(sf::Color::Blue);

        }
        return EXIT_SUCCESS;
}

 
NOTE: cos1 and cos2 aren't cosinous, cos (correct coś) in polish means something, i use this word because i haven't idea.
« Last Edit: June 16, 2013, 01:16:15 pm by rubikshift »

 

anything