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

Author Topic: Entity origin irrelevant to rotation  (Read 3754 times)

0 Members and 1 Guest are viewing this topic.

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Entity origin irrelevant to rotation
« on: May 03, 2015, 09:55:12 pm »
I'm experiencing a problem with rotating a rectangle shape, I need it to rotate around the center of the rectangle, not the top left corner by default. However I have done .setOrigin(-100, -300), yet when I do .rotate(0.1) it still rotates from 0, 0 (the default, top left corner). I can't get my head round this, any ideas? It may be difficult to upload the relevant parts of my code, as my code is very large.
« Last Edit: May 03, 2015, 09:57:48 pm by oOhttpOo »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Entity origin irrelevant to rotation
« Reply #1 on: May 04, 2015, 03:23:29 am »
How would -100, -300 ever be the center?

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Entity origin irrelevant to rotation
« Reply #2 on: May 04, 2015, 11:27:40 am »
How would -100, -300 ever be the center?
That's the shape position I need it to have. I also experimented with -300, -300 as such but it still rotates from the corner. To clarify I need the shape to rotate around it's own center (around -100, -300). I simply don't know how you do this  ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Entity origin irrelevant to rotation
« Reply #3 on: May 04, 2015, 11:38:45 am »
The origin is in local coordinates. So there is no way (-100, -300) can be the center, given that (0, 0) is the top-left corner. The center can only have positive coordinates (computed as (width/2, height/2)).
Laurent Gomila - SFML developer

user123

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Entity origin irrelevant to rotation
« Reply #4 on: May 08, 2015, 05:14:41 pm »
Perhaps, in setOrigin, try something like (texture.getSize().x/2),(texture.getSize().y/2), that works for me.

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Entity origin irrelevant to rotation
« Reply #5 on: May 08, 2015, 07:50:40 pm »
Thanks for the responses, but I unfortunately came to no solution. Here's the code cut down the best I could:

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <Windows.h>
using namespace std;
#include <time.h>


bool targethashit = false;
bool gunfire = false;
bool leftisclicked = false;


int main()
{
        sf::RenderWindow window(sf::VideoMode(600, 600), "Window");

        sf::RectangleShape hill1(sf::Vector2f(200, 200));
        sf::RectangleShape hill2(sf::Vector2f(200, 400));
        sf::RectangleShape hill3(sf::Vector2f(100, 500));
        sf::RectangleShape hill4(sf::Vector2f(200, 100));
        sf::RectangleShape sniper(sf::Vector2f(20, 40));
        sf::RectangleShape snipergun(sf::Vector2f(50, 3));
        sf::RectangleShape bullet(sf::Vector2f(15, 3));
        sf::RectangleShape target1(sf::Vector2f(10, 10));

       
        sf::RectangleShape si14(sf::Vector2f(20, 5)); //create the entity i need to rotate





        bullet.setOrigin(-40, -375);
        target1.setOrigin(-550, -296);


        si14.setOrigin(-490, -375);                                      //set the origin of the entity i need to rotate
        si14.setFillColor(sf::Color::Green);


        bullet.setFillColor(sf::Color::Yellow);
        sniper.setOrigin(-30, -360);
        snipergun.setOrigin(-40, -375);
        hill1.setOrigin(0, -400);
        hill2.setOrigin(-150, -300);
        hill3.setOrigin(-350, -200);
        hill4.setOrigin(-450, -500);
        hill1.setFillColor(sf::Color::Green);
        hill2.setFillColor(sf::Color::Green);
        hill3.setFillColor(sf::Color::Green);
        hill4.setFillColor(sf::Color::Green);
        snipergun.setFillColor(sf::Color::Black);
        sniper.setFillColor(sf::Color::Red);

        while (window.isOpen())
        {


                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();

                }


                window.clear(sf::Color::Cyan);


               
         window.draw(si14);
       

       
                sf::FloatRect bulletb = bullet.getGlobalBounds();
                sf::FloatRect target1b = target1.getGlobalBounds();
               
                window.draw(snipergun);
                window.draw(sniper);
                window.draw(target1);
               
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        sniper.move(0.1, 0);
                        snipergun.move(0.1, 0);
                        bullet.move(0.1, 0);
                }

                si14.rotate(-0.1);
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
                {
                        sniper.move(0, -0.5);
                        snipergun.move(0, -0.5);
                        bullet.move(0, -0.5);
                }
               
                if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                {
                        si14.rotate(0.1);                                                                                                                       //it only rotates from the top left corner
                }
                       
                       
       
                       

                window.display();



        }
        return 0;

}
« Last Edit: May 08, 2015, 07:52:18 pm by oOhttpOo »

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Entity origin irrelevant to rotation
« Reply #6 on: May 08, 2015, 08:35:18 pm »
You're still setting the origins to negative numbers. This will never cause it to rotate around the centre. (0,0) is the top left corner of the rectangle shape and (width,height) is the bottom right. You should therefore find the size of the object and as people have said, set the origin to (width/2,height/2).

oOhttpOo

  • Newbie
  • *
  • Posts: 40
    • View Profile
Re: Entity origin irrelevant to rotation
« Reply #7 on: May 09, 2015, 04:05:54 pm »
I fixed it now thanks  :)