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

Author Topic: sprite from square to rhombus  (Read 743 times)

0 Members and 1 Guest are viewing this topic.

MichelangeloSFML/

  • Newbie
  • *
  • Posts: 17
    • View Profile
sprite from square to rhombus
« on: June 16, 2022, 05:16:21 am »
I would like to take my sprite and skew it from a square to a rhombus. To slowly transition from square into various degrees of the rhombus and then back. How can I accomplish this?

Thanks in advance.

kojack

  • Sr. Member
  • ****
  • Posts: 315
  • C++/C# game dev teacher.
    • View Profile
Re: sprite from square to rhombus
« Reply #1 on: June 16, 2022, 09:33:05 am »
It took a little fighting with SFML's sprite transforms, but this works:
float shear = 0;
sf::Transform tr = sprite.getTransform();
const float *m = tr.getMatrix();
sf::Transform shearT(m[0], shear, m[12], m[1], m[5], m[13], m[3], m[7], m[15]);
tr = shearT * tr.getInverse();
window.draw(sprite, tr);

The shear variable is a float with a value of zero for a normal sprite. 1.0 would make it shear at 45 degrees to the right. -1.0 would go left, etc.

It might have been easier to use something like a vertex array instead of a sprite, most of the maths above were fighting with some of the hard coded sprite behavior (To shear without making the position incorrect we need to reverse the order of multiplication of transforms, which SFML doesn't allow, so the inverse is to cancel out the bit we don't want). But I like a challenge. :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: sprite from square to rhombus
« Reply #2 on: June 16, 2022, 10:29:07 am »
Might also be easier to achieve with a sf::Shape
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/