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

Author Topic: [Solved] SetOrigin function makes the sprite disappear  (Read 8329 times)

0 Members and 1 Guest are viewing this topic.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
[Solved] SetOrigin function makes the sprite disappear
« on: September 04, 2012, 05:51:41 pm »
I searched in the forums and found someone with a similar problem, but I'd like to know the details. Everything is drawn properly and it even moves according to the semi-circle function I am using, but when I set origin to (400, 300) and run it, the sprite disappears, as if it had not even been drawn or displayed in the first place.

I've checked my code over and over, but found nothing wrong in it, it just goes mad when I set the origin, I'll post it just in case, but I doubt it's that.

int main()
{ ///Test to learn how to draw individual bullets
   sf::RenderWindow window(sf::VideoMode(800, 600), "Bullet Testing");
   sf::Event event;
   sf::IntRect d;
   sf::Image img; img.loadFromFile("Images\\Bullets\\HQtest.jpg");
   img.createMaskFromColor(sf::Color::Black, 0);
   sf::Texture Img;
   Img.loadFromImage(img, d);
   Img.setSmooth(true);
   sf::Sprite Sprite;
   sf::Sprite Sprite2;
   sf::Sprite Sprite3;
   Sprite.setTexture(Img, true); Sprite2.setTexture(Img, true); Sprite3.setTexture(Img, true);
   Sprite.setColor(sf::Color(255, 255, 0, 200));
   Sprite2.setColor(sf::Color(255, 0, 255, 200));
   Sprite3.setColor(sf::Color(0, 255, 255, 200));
   Sprite.setPosition(400, 300);
   Sprite2.setPosition(110, 0);
   Sprite3.setPosition(120, 0);

window.setFramerateLimit(60);
///For the circle function.
index x = -25; const short int r = 25;

    ///Game loop.
    while (window.isOpen())
    {   // check all the window's events that were triggered since the last iteration of the loop
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();

        window.draw(Sprite); window.draw(Sprite2); window.draw(Sprite3);
        Sprite.setOrigin(400, 300);
        if (x <= r )
        { Sprite.setPosition(x, sqrt((r * r) - (x * x))); ++x; }
     
        window.display();
    }
    }
    return 0;

}

Edit: Just made some tests only to find myself even more baffled than before, if I remove the if that handles the sprite's movement it actually appears, but at position (0, 0), if I remove the setOrigin function it properly appears at the center of the renderwindow. Is this all due to my graphics card or is there actually something wrong with the code?
« Last Edit: September 04, 2012, 10:53:57 pm by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: SetOrigin function makes the sprite disappear
« Reply #1 on: September 04, 2012, 06:48:15 pm »
The transform get's recalculated that most likely is intended. There is position, rotation, origin and scale but if any of these changes, entire transform does. You can't set position to something and then set origin to something else and rotate/whatever with new origin while keeping position same. The position, center of rotation and scaling of something transformable is position of it's origin.
Back to C++ gamedev with SFML in May 2023

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: SetOrigin function makes the sprite disappear
« Reply #2 on: September 04, 2012, 07:16:04 pm »
So what I would need to do before setting it's position is to set the origin of the given sprite in the point I want it to be displayed, then do something like this:

Sprite.setOrigin(400, 300);
Sprite.setPosition(Sprite.getOrigin());
 

Which I already did, to get nothing. The sprite doesn't even appear if I do this. Everytime I set the origin to something the sprite doesn't appear, whether I move it afterwards or not.

I know what the origin is and that it's used to affect transformations done after being told that it had a new origin and I intend to do that, so that it moves my sprite in a circular fashion around the given origin. That is my intention. I know that it will not mantain it's position, so my problem is not that, but rather it is that it doesn't move according to the origin set. in fact it doesn't even appear at all if I use the setOrigin function.

I didn't quite get your post so I did what I thought you told me to and tried to re-explain.

And the reason I am also blaming my graphics card is because of this:
http://en.sfml-dev.org/forums/index.php?topic=6696.msg44048
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: SetOrigin function makes the sprite disappear
« Reply #3 on: September 04, 2012, 07:31:21 pm »
Sprite.setOrigin(400, 300);
Sprite.setPosition(Sprite.getOrigin());
This should make the sprite appear in same spot as if you didn't move it at all. Does it appear then?
Back to C++ gamedev with SFML in May 2023

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: SetOrigin function makes the sprite disappear
« Reply #4 on: September 04, 2012, 07:33:10 pm »
It doesn't, that's why I made the thread.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: SetOrigin function makes the sprite disappear
« Reply #5 on: September 04, 2012, 07:38:38 pm »
Do other sprites appear?
Also, what class/type is that:
index x = -25
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: SetOrigin function makes the sprite disappear
« Reply #6 on: September 04, 2012, 07:53:29 pm »
To get efficient help from us, please write a minimal and complete code that reproduces the problem (one sprite, an image created in code so that we know its size and don't need additional files to run the test, etc.).
Laurent Gomila - SFML developer

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: SetOrigin function makes the sprite disappear
« Reply #7 on: September 04, 2012, 08:58:57 pm »
Do other sprites appear?
Also, what class/type is that:
index x = -25

I forgot to add that, it's just a typedef for short int, I use it for any number I may use as an index of some sort.
And other sprites do appear, the one that isn't shown is the one whose origin is set.
I'll attach the image and other than the image and the index variable there is nothing left out that you may need to run it.

I just noticed but I should have used:
index x = -r

instead of:

index x = -25

Since in order to make it a semi-circular function it has to have a domain from -r to r, where r is the radious. Just as a small side-note in case it is needed.

Edit: Here's the code:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!");
    sf::CircleShape shape(10.f);
    shape.setOrigin(100 , 100);
    shape.setFillColor(sf::Color::Green);

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

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

What happens is that with just using the function setOrigin (this time in a Circle Shape) the image is not shown, if I remove the line it is properly shown, else it's just a black screen. The same happens when I use an image, I may have made the explanation at first complicated, but that's the summary of what's happening.

[attachment deleted by admin]
« Last Edit: September 04, 2012, 09:25:29 pm by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: SetOrigin function makes the sprite disappear
« Reply #8 on: September 04, 2012, 10:03:16 pm »
It's the expected behaviour. The origin is the center point of all transformations, including the translation (position). And since the position is still (0, 0), the consequence is that the whole shape moves by an offset of (-100, -100), and goes out of the screen.
Laurent Gomila - SFML developer

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: SetOrigin function makes the sprite disappear
« Reply #9 on: September 04, 2012, 10:25:24 pm »
I finally understand. I thought that the function worked entirely different and the documentation hadn't any details so I made myself an entirely different mind image of it. Thanks for the help.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: [Solved] SetOrigin function makes the sprite disappear
« Reply #10 on: September 05, 2012, 01:00:31 am »
Quote
The origin of an object defines the center point for all transformations (position, scale, rotation)
Quote
By default, the sprite is positionned/rotated/scaled relatively to its top-left corner, because it is the local point (0, 0). But if we change the origin to be (5, 5), the sprite will be positionned/rotated/scaled around its center instead. And if we set the origin to (10, 10), it will be transformed around its bottom-right corner.

To keep the sf::Transformable class simple, there's only one origin for all the components. You cannot position the sprite relatively to its top-left corner while rotating it around its center, for example.
Seems informative and clear enough to me and easy to confirm through little experimentation.
Back to C++ gamedev with SFML in May 2023

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: [Solved] SetOrigin function makes the sprite disappear
« Reply #11 on: September 05, 2012, 11:15:52 pm »
I didn't read that last part well it appears, I just made the wrong assumption that the new origin was set in the given position and it didn't move the object in an offset, which is why I was so confused with what was happening. I did know that the origin was (and is) the center for all transformations, but I read other parts of the documentation and didn't touch the sf::Transformable part. In the end it was all too simple.

I tend to use solved threads to make small questions so I'll go ahead.

I've been reading about polar coordinates and they look pretty useful for what I am trying to do and many other things surely, so far I know SFML hasn't support for them, since it's something complementary and not strictly necessary. So my question is: Where can I find a library that has them or a way to implement them myself? I have read little and I am still investigating, but if you have a suggestion of where should I start it would be very good.
« Last Edit: September 05, 2012, 11:42:55 pm by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

 

anything