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

Author Topic: Rotation origin  (Read 7125 times)

0 Members and 1 Guest are viewing this topic.

freesoul

  • Newbie
  • *
  • Posts: 19
    • View Profile
Rotation origin
« on: October 06, 2012, 08:42:03 pm »
Hello, I need to draw some stuff with it's origin point (0, 0), but I need to rotate these stuff as the origin was in the center. Is it possible to do this? have two origins: one for rotation, other one for positioning? if not, I suggest to add this feature, thanks !

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Back to C++ gamedev with SFML in May 2023

freesoul

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Rotation origin
« Reply #2 on: October 06, 2012, 09:13:42 pm »
I've read that but I don't get it  :o

I want to do absolute rotations given some degrees. I've read about sf::Transform, but I don't know how to use that
« Last Edit: October 06, 2012, 09:20:45 pm by freesoul »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Rotation origin
« Reply #3 on: October 06, 2012, 09:22:27 pm »
Quote
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. To do such things, use sf::Transform directly.
I don't know what you don't get about the transform.
« Last Edit: October 06, 2012, 09:25:24 pm by FRex »
Back to C++ gamedev with SFML in May 2023

freesoul

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Rotation origin
« Reply #4 on: October 06, 2012, 09:24:23 pm »
Quote
To do such things, use sf::Transform directly.
How?  ;D

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Rotation origin
« Reply #5 on: October 06, 2012, 09:26:45 pm »
Pass transform to draw as last argument.
Back to C++ gamedev with SFML in May 2023

freesoul

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Rotation origin
« Reply #6 on: October 06, 2012, 09:32:59 pm »
I don't know what "transform" I should pass as argument :S  I would thank for a simple example.

I want to keep my position coords as (0, 0). So how to rotate at center?

Or an alternative to this (i actually dont know if this is correct):
sprite.setOrigin(center_x, center_y);
sprite.setRotation(angle);
sprite.setOrigin(-center_x, -center_y);

btw, sf::transform::rotate is it absolute or relative?
« Last Edit: October 06, 2012, 09:42:19 pm by freesoul »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Rotation origin
« Reply #7 on: October 06, 2012, 09:38:48 pm »
{
sf::Transform trans;
trans.rotate(your_angle,your_centre_as_2_floats_or_1_vector2f);
app.draw(your_drawable,trans);
}

No, this is not correct, transform will recompute itself and rotate around current origin.
There's nothing absolute in transform, it's a wraper for a matrix so you just keep combining matrices with every operation. It's quite complicated and the order of operation matters(I think, I'm not sure).
Why can't you just do smt.move(smt.getOrigin()); on your object?
« Last Edit: October 06, 2012, 09:49:51 pm by FRex »
Back to C++ gamedev with SFML in May 2023

freesoul

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Rotation origin
« Reply #8 on: October 06, 2012, 10:14:38 pm »
Oh thanks. Well I can't do that move because I need to keep the global position relative to other sprites I display.

I've done this and it works :)
transf.rotate(mIt->fDegrees,mIt->spMissile.getPosition().x + mIt->halfWidth, mIt->spMissile.getPosition().y + mIt->halfHeight);
_window.draw(mIt->spMissile, transf);

But I don't know why I have to pass the global center position of the sprite to rotate it, but im glad it works !


Well, I needed to :

- calculate and save half width / height (2 int) for each item.
- save degrees in each items for post-using, instead of using it directly
- declare and define a new local variable sf::transform
- pass it as argument and add global sprite position

all that to do a simple rotation. I still think it would be simpler to add to sf::Transformable a SetRotationOrigin() member (by default rotation origin would be 0,0), but its ok
« Last Edit: October 06, 2012, 10:24:40 pm by freesoul »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Rotation origin
« Reply #9 on: October 06, 2012, 10:32:40 pm »
That's probably because of the order of applying sprites and your transforms. It's quite complicated and confusing (linear algebra). You probably shouldn't mix transforms and transformables too much if you don't fully understand what's happening.

Quote
all that to do a simple rotation. I still think it would be simpler to add to sf::Transformable a SetRotationOrigin() member (by default rotation origin would be 0,0), but its ok
It's your design that is ineffective. The way transformables make their transform object is not by using transform methods but directly computing 3x3 matrix for it. That'd probably be too chaotic/impossible to do with separate origins for everything.
« Last Edit: October 06, 2012, 10:39:59 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Rotation origin
« Reply #10 on: October 06, 2012, 10:56:45 pm »
sf::Transformable uses the same origin for position, rotation and scale. It can be specified with the setOrigin() function. If you want a hierarchy of drawable objects, so that each one's coordinates are relative to the parent, use this approach:
class MyParent : public sf::Drawable, public sf::Transformable
{
public:
     // overrides sf::Drawable::draw()
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states)
    {
        // getTransform() inherited from sf::Transformable
        states.transform *= getTransform();

        target.draw(myChild, states);
    }
private:
    ...
};

If you need even more flexibility, take a look at SFML's low-level API. Compose your own transforms with sf::Transform and take a sf::VertexArray to draw the object.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: