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

Author Topic: [SOLVED] Applying a sf::Transfrom to a sf::Sprite  (Read 2444 times)

0 Members and 1 Guest are viewing this topic.

moistweb

  • Newbie
  • *
  • Posts: 18
    • View Profile
[SOLVED] Applying a sf::Transfrom to a sf::Sprite
« on: June 24, 2014, 01:20:33 am »
Hello everyone,
I've been recently working with the Animated Sprite in the Wiki. And so I came across an operation that requires retrieving the current active sprite from the AnimatedSprite, with all its transforms ( to be specefic, this operation is collision detection, I need to pass the sprite with all the transforms applied to it in order to test for collision). The problem is that the AnimatedSprite doesn't use sprites to output the animation result ( it uses sf::Vertex if I'm not wrong).  This class is derived from sf::Transformable, so it's possible to retrieve the sf::Transform from it, but the problem is that I couldn't find a way to apply this transform to the sprite that gets passed to the collision detection function. Any help guys, I really need to fix this !
« Last Edit: June 24, 2014, 08:22:33 pm by Reda Lahdili »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10916
    • View Profile
    • development blog
    • Email
AW: Applying a sf::Transfrom to a sf::Sprite
« Reply #1 on: June 24, 2014, 08:13:48 am »
You shouldn't work with the actual sprite when it comes to collision logic. The sprite represents the graphical aspect of your entity. Instead you could work directly with transformables.

Your post is a bit all over the place, so what exactly is the problem? Or rather what can't you figure out?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

moistweb

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Applying a sf::Transfrom to a sf::Sprite
« Reply #2 on: June 24, 2014, 04:00:39 pm »
Well actually I use the collision methods written in the Github Wiki :
https://github.com/SFML/SFML/wiki/Source%3A-Simple-Collision-Detection-for-SFML-2
To test for Pixel Perfect Collision we need to pass two sprites. So I thought if I could merge the sf::Transform of the AnimatedSprite with a sprite ( including setting its texture and textureRect), I could represent the actual state of the AnimatedSprite, and therefore it is usable as an argument in the PixelPerfectTest().
Just in case my question wasn't clearly formatted, I would like some way to apply an sf::Transform to a newly created sprite. Or better, another way to test collision with AnimatedSprite. What I couldn't figure out is a way to achieve the same result in the following code :
 
sf::Sprite playerCollisionSprite;
sf::Transform animatedPlayerSpriteTransform=  animatedPlayerSprite.getTransform();
playerCollisionSprite.setTransform(animatedPlayerSpriteTransform); // this function is actually what I'm looking for

if ( Collision::BoundingBoxTest(enemySprite, collisionSprite)
{
    // process collision ( modify health or generate explosion animation...)
}
 
« Last Edit: June 24, 2014, 04:11:37 pm by Reda Lahdili »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Applying a sf::Transfrom to a sf::Sprite
« Reply #3 on: June 24, 2014, 04:15:25 pm »
Quote
Just in case my question wasn't clearly formatted, I would like some way to apply an sf::Transform to a newly created sprite.
This is not the right question to ask. You can apply a transform to a sprite at draw time, but this is not what you want. What you want is the sprite texture (ok), textureRect (ok), transform (ok), globalBounds (textureRect * transform), and... that it for the collision detection part, if I read the wiki tutorial carefully.

So instead of searching something complicated, you should have decomposed the wiki algorithm and you would have found out that you already have everything that you need ;)
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10916
    • View Profile
    • development blog
    • Email
Re: Applying a sf::Transfrom to a sf::Sprite
« Reply #4 on: June 24, 2014, 04:15:57 pm »
I can't really advise in using that code as is. The main problem being, that for each test the texture data gets pulled from the GPU memory to the RAM, which is a very expensive operation. Instead you should pre-cache the image data and only apply the transformation onto the pixel data. That way, you wouldn't need an sprite either.
Besides that pixel perfect collision is most likely a overkill, some custom bounding box would probably work just as well, without being as performance intensive.

As for the question, there's no "setTransform" which probably indicates that it's not the best way to handle things. So the only thing that you could do is reconstruct the sprite manually.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Applying a sf::Transfrom to a sf::Sprite
« Reply #5 on: June 24, 2014, 04:17:00 pm »
Quote
Instead you should pre-cache the image data and only apply the transformation onto the pixel data.
Isn't it exactly what the wiki code does? There's a BitmapMask manager with a cache.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10916
    • View Profile
    • development blog
    • Email
Re: Applying a sf::Transfrom to a sf::Sprite
« Reply #6 on: June 24, 2014, 04:21:33 pm »
Isn't it exactly what the wiki code does? There's a BitmapMask manager with a cache.
Oh, I seem to have understood the code wrong, so yeah I guess that's okay. :D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

moistweb

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Applying a sf::Transfrom to a sf::Sprite
« Reply #7 on: June 24, 2014, 04:32:27 pm »
I think I got your points guys. What I think of doing now is modify the AnimatedSprite code to use sf::Sprite instead of vertices ( this is fairly simple, this is what I sould have done if I made my own Animation class), that way I could leave the same members in this class and add another function getActiveSprite() to pass it to the collision function. I think I'm on the right track.

moistweb

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Applying a sf::Transfrom to a sf::Sprite
« Reply #8 on: June 24, 2014, 08:14:39 pm »
Guys my problem has been solved. I modified the AnimatedSprite to work with a sprite instead of vertices. I canceled the inheritance and made a sf::Sprite as a private member. This latter sprite represents the currentSprite, its texture gets set in the setAnimation() ( see the code in the wiki), and its frame rectangle ( sf::FloatRect) gets updated by each update().
To be honest you can't do much with the sf::Vertex method in the wiki, it's better to use sprites to actually use all sf::Sprite's methods.