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

Author Topic: Why does my shape does not revolve around its center.  (Read 3146 times)

0 Members and 1 Guest are viewing this topic.

rajhansk

  • Newbie
  • *
  • Posts: 21
    • View Profile
Why does my shape does not revolve around its center.
« on: September 01, 2017, 06:21:27 pm »
This is the code I have written, and after pressing R I want it to rotate around itself but it takes center outside the shape and revolves in a weird way.

#include <SFML/Graphics.hpp>

//#include <cstdlib>
//#include <ctime>
//#include<iostream>
using namespace std;
int main()
{


    sf::RenderWindow window(sf::VideoMode(1366, 768), "First render");
    sf::CircleShape Circle(50,6);


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::G))
    {
        Circle.setFillColor(sf::Color(100,100,100));

    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        Circle.move(0,-1);
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        Circle.move(0,1);
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        Circle.move(-1,0);
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        Circle.move(1,0);
        }
        if(sf::Keyboard::isKeyPressed((sf::Keyboard::LShift))&&sf::Keyboard::isKeyPressed((sf::Keyboard::Right)))
            Circle.move(5,0);
if(sf::Keyboard::isKeyPressed((sf::Keyboard::LShift))&&sf::Keyboard::isKeyPressed((sf::Keyboard::Left)))
            Circle.move(-5,0);
if(sf::Keyboard::isKeyPressed((sf::Keyboard::LShift))&&sf::Keyboard::isKeyPressed((sf::Keyboard::Up)))
            Circle.move(0,-5);
if(sf::Keyboard::isKeyPressed((sf::Keyboard::LShift))&&sf::Keyboard::isKeyPressed((sf::Keyboard::Down)))
            Circle.move(0,5);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::R))
   Circle.rotate(3);

        window.clear();
        window.draw(Circle);
       // window.draw(shape2);
        window.display();
    }

    return 0;
}
« Last Edit: September 01, 2017, 06:47:47 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Why does my shape does not revolve around its center.
« Reply #1 on: September 01, 2017, 06:50:22 pm »
Quote
after pressing R I want it to rotate around itself but it takes center outside the shape and revolves in a weird way
Wanting something is not enough to make it work, usually some code is needed ;)

See there, especially the section about the origin:
https://www.sfml-dev.org/tutorials/2.4/graphics-transform.php#transforming-sfml-entities
Laurent Gomila - SFML developer

rajhansk

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Why does my shape does not revolve around its center.
« Reply #2 on: September 02, 2017, 03:11:06 am »
I am trying to work on my code , Thanks for reply :) :)

rajhansk

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Why does my shape does not revolve around its center.
« Reply #3 on: September 02, 2017, 06:00:50 pm »
Do I have to set origin of my circle to center and then rotate?

rajhansk

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Why does my shape does not revolve around its center.
« Reply #4 on: September 02, 2017, 06:31:14 pm »
how does the set origin works?

if I have a circle of 100 unit radius then if I want to set its origin at center then i have written
entity.setOrigin(100,100); but what if I have a triangle and I need to set origin of the triangle ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Why does my shape does not revolve around its center.
« Reply #5 on: September 02, 2017, 06:44:09 pm »
Quote
Do I have to set origin of my circle to center and then rotate?
Just try it yourself and see if it works ;)

Quote
how does the set origin works?
It is explained in the documentation and tutorials.

Quote
if I have a circle of 100 unit radius then if I want to set its origin at center then i have written
entity.setOrigin(100,100)
Sounds good.

Quote
but what if I have a triangle and I need to set origin of the triangle ?
What's the problem with a triangle?
Laurent Gomila - SFML developer

kepler0

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Why does my shape does not revolve around its center.
« Reply #6 on: September 09, 2017, 06:25:14 pm »
The simplest way to center the origin of anything is to divide the bounds by 2, for example:
object.setOrigin(object.getGlobalBounds().width / 2, object.getGlobalBounds().height / 2);
Zzz

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Why does my shape does not revolve around its center.
« Reply #7 on: September 10, 2017, 10:32:31 am »
Origin is in local coordinates, this the formula should use getLocalBounds() instead of getGlobalBounds().
Laurent Gomila - SFML developer