SFML community forums

Help => General => Topic started by: zpawlo00 on March 24, 2018, 01:54:27 pm

Title: Collision detection
Post by: zpawlo00 on March 24, 2018, 01:54:27 pm
How can i get two sprites from different classes to put them to function which is created in third class?
Title: Re: Collision detection
Post by: eXpl0it3r on March 24, 2018, 02:27:21 pm
By doing it. :D

What's the issue?
Title: Re: Collision detection
Post by: zpawlo00 on March 24, 2018, 03:05:23 pm
I dont now how to do this
My classes:
Quote
#pragma once
#include <vector>
#include <SFML\Graphics.hpp>


class TBlocks:public sf::Drawable
{
public:
   TBlocks(float t_Y);
   TBlocks()=delete;
   virtual ~TBlocks() = default;

   void update_block();
   void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
   sf::Texture block_texture;
   sf::Sprite blockSprite;
   sf::Texture *bTexture = &block_texture;

private:
   

   
   std::string blocks[3];


};


Quote
#ifndef TPLAYER_H
#define TPLAYER_H
#include <SFML/Graphics.hpp>


class TPlayer : public sf::Drawable
{
public:

   TPlayer(float t_X, float t_Y);
   TPlayer() = delete;
   virtual ~TPlayer() = default;
   void update();

   float moveRight();
   float moveLeft();

   void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
   sf::Texture playerTexture;
   sf::Sprite playerSprite;
   sf::Texture *pTexture = &playerTexture;


private:
   
   


   const double playerVelocity{4};
   sf::Vector2f velocity{ playerVelocity, 0.f };




};

#endif // TPLAYER_H


and i want to use this fuction from third class:
Quote
bool TGame::checkCollision()
{
   if (SPRITE.getGlobalBounds().intersects(SPRITE2.getGlobalBounds())

}


Title: Re: Collision detection
Post by: eXpl0it3r on March 24, 2018, 05:35:24 pm
Implement a getBounds or similar function on both classes each retuning the global bound of their specific sprite.
Title: Re: Collision detection
Post by: zpawlo00 on March 24, 2018, 09:49:51 pm
eXpl0it3r I will appreciate if you will show me how to do it
Title: Re: Collision detection
Post by: eXpl0it3r on March 24, 2018, 11:48:52 pm
You know how to write a function, right?
You know how to get the global bound of a sprite, right?
Now put these two together in your block and player class.
Title: Re: Collision detection
Post by: zpawlo00 on March 25, 2018, 10:23:38 am
Quote
sf::FloatRect TBlocks::blockBound()
{
   return blockSprite.getGlobalBounds();

}
Quote
sf::FloatRect TPlayer::playerBound()
{
   return playerSprite.getGlobalBounds();
}
Quote
bool TGame::checkCollision()
{
   if (player->playerBound().instersects(block->blockBound()))
   {
      return true;
   }

}

}

}

Quote
error C2039: 'instersects': is not a member of 'sf::Rect<float>'
How can i correct this?
Title: Re: Collision detection
Post by: Grundkurs on March 25, 2018, 11:48:34 am
the error is probably triggered by a "typo": try "intersects" instead of "instersects".
Title: Re: Collision detection
Post by: zpawlo00 on March 25, 2018, 03:37:50 pm
Oh i didn't realise that thanks