变换无视 sf::Sprite 的起源 [英] Transformations igrnores sf::Sprite's origin
问题描述
在 SFML 中转换精灵,不考虑它的新来源.
Transforming a sprite in SFML, does not regard it's new origin.
在我的例子中,sf::Sprite 围绕左上角 ({0,0}) 的轴旋转,不管它的原点是什么.之前使用 .setOrigin() 设置新原点无效.
In my case sf::Sprite is rotating around the axis that is in the left top corner ({0,0}) regardless its origin. Setting new origin with .setOrigin() earlier takes no effect.
我确信精灵更早地获得了正确的原点位置,即矩形的中心.
I am sure that sprite is getting the right origin position earlier which is center of rectangle.
我的代码:
在我的每个 Card 类构造函数中,我都设置了我的精灵的原点.
In each of my Card class constructors I set the origin of my sprite.
card_sprite.setOrigin(Card::get_default_single_card_size().x*Game::get_scale()/2,Card::get_default_single_card_size().y*Game::get_scale()/2);
然后在我的 Deck 类中,它的行为类似于 std::stack of Cards 我使用函数:
And then in my Deck class which behaves like std::stack of Cards I use function:
void Deck::push(const Card& crd)
{
push_back(crd);
..//
std::default_random_engine generator;
std::uniform_real_distribution<float> distributor(0,360);
top().setRotation(distributor(generator));
}
Card::setRotaion() 看起来像这样,(仍然围绕左上角旋转卡片):
Card::setRotaion() looks like this, (which stil rotates card around top left corner) :
void Card::setRotation(float angle)
{
card_sprite.setRotation(angle);
}
提前感谢您的帮助
推荐答案
实际上 sf::Transform
中的大多数方法都接受额外的参数来指定中心转换,根据 https://stackoverflow.com/users/7703024/super 对我的问题的评论主题:如何设置原点"sfml 中的变换
Actually most methods in sf::Transform
accept extra arguments to specify a center for the transformation, as per https://stackoverflow.com/users/7703024/super 's comment on my question on the same theme : How to "set the origin" of a Transform in sfml
我不太确定你的代码,但我可能遇到过类似的问题.
我解决"了它(以一种非常不理想的方式)在使用 sf::Transform
s 时,将每次对 sfml 绘图函数的调用替换为对自定义函数的调用.
I'm not too sure from your code, but I might've come up against a similar problem.
I "solved" it (in a very not ideal way) by replacing every call to a sfml drawing function with a call to a custom function when using sf::Transform
s.
例如:而不是做类似的事情:
eg: instead of doing something like:
window.draw(thing, my_transform);
我必须这样做:
draw_transformed(thing, my_transform, window)
draw_transformed
的代码如下所示:
void draw_transformed (sf::Shape const& thing, sf::Transform const& t, sf::RenderWindow& window) // cf note (1)
{
sf::Vector2f pos = thing.getPosition();
sf::Transform go_to_zero;
go_to_zero.translate(-pos);
sf::Transform go_back;
go_back.translate(pos);
sf::Transform conjugated_transform = go_back * t * go_to_zero ;
window.draw(thing, conjugated_transform);
}
(1) 我们不能使用 sf::Drawable
作为 thing
的类型,因为在 sfml 中并不是所有可绘制的东西都有 getPosition
> 方法,所以我们必须重载函数或者做一些复杂"的事情;超越这个例子.
(1) we can't use sf::Drawable
as the type of thing
because in sfml not all drawable things have a getPosition
method, so we have to overload the function or do something "complicated" to go beyond this example.
这篇关于变换无视 sf::Sprite 的起源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!