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

Author Topic: Movement  (Read 2462 times)

0 Members and 1 Guest are viewing this topic.

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Movement
« on: May 05, 2017, 04:37:53 pm »
hey, uh..there's a little problem here.. I'm trying to make experiment with movement based on angle..(only 0 works fine)

I wonder what's wrong..
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cmath>
#include <iostream>

class User
{
private :
    sf::RectangleShape Rect;
    const float speed;
    const float rotation;
public :
    User() :
        speed(0.8),
        rotation(0.2)
    {
        Rect.setSize(sf::Vector2f(38, 17));
        Rect.setOrigin(Rect.getSize().x / 2, Rect.getSize().y / 2);
        Rect.move(Rect.getSize().x / 2, Rect.getSize().y / 2);
    }
    sf::Shape &getShape()
    {
        return Rect;
    }
    void Action(const sf::RenderWindow &window)
    {
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            Rect.rotate(rotation);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            Rect.rotate(-(rotation));
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            sf::Vector2f Direction;
            Direction.x = speed * std::cos(Rect.getRotation());
            Direction.y = speed * std::sin(Rect.getRotation());
            Rect.move(Direction);
        }
        check(window);
    }
    void check(const sf::RenderWindow &window)
    {
        if (Rect.getPosition().x < 0) Rect.move(speed, 0);
        else if (Rect.getPosition().x > window.getSize().x) Rect.move(-(speed), 0);
        if (Rect.getPosition().y < 0) Rect.move(0, speed);
        else if (Rect.getPosition().y > window.getSize().y) Rect.move(0, -(speed));
    }
};

int main()
{
    sf::RenderWindow window(sf::VideoMode(900, 600), "some window");
    User user;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) window.close();
        }
        user.Action(window);
        window.clear(sf::Color::Black);
        window.draw(user.getShape());
        window.display();
        std::cout << user.getShape().getRotation() << std::endl;
    }
}

 

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Movement
« Reply #1 on: May 05, 2017, 04:54:48 pm »
Interestingly, someone else just made a post right before you trying to solve the same problem. Are you perhaps working together?

Your problem is that Rect.getRotation() returns the angle in degrees, but sin and cos expect the angle to be in radians.

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: Movement
« Reply #2 on: May 05, 2017, 05:17:45 pm »
Interestingly, someone else just made a post right before you trying to solve the same problem. Are you perhaps working together?

Your problem is that Rect.getRotation() returns the angle in degrees, but sin and cos expect the angle to be in radians.

nope..we are not working together..

I see.. so it expects radians

edit : also does the location of the center affects the movement ??? (I have the origin in the center...(most likely))
« Last Edit: May 05, 2017, 05:20:05 pm by Flaze07 »

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: Movement
« Reply #3 on: May 05, 2017, 05:33:23 pm »
ayy, it works thanks man

 

anything