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

Author Topic: Collision detection  (Read 2744 times)

0 Members and 1 Guest are viewing this topic.

zpawlo00

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Collision detection
« 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Collision detection
« Reply #1 on: March 24, 2018, 02:27:21 pm »
By doing it. :D

What's the issue?
« Last Edit: March 24, 2018, 05:29:35 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zpawlo00

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Collision detection
« Reply #2 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())

}



eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Collision detection
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zpawlo00

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Collision detection
« Reply #4 on: March 24, 2018, 09:49:51 pm »
eXpl0it3r I will appreciate if you will show me how to do it

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Collision detection
« Reply #5 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zpawlo00

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Collision detection
« Reply #6 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?
« Last Edit: March 25, 2018, 11:10:25 am by zpawlo00 »

Grundkurs

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Re: Collision detection
« Reply #7 on: March 25, 2018, 11:48:34 am »
the error is probably triggered by a "typo": try "intersects" instead of "instersects".
« Last Edit: March 25, 2018, 11:50:31 am by Grundkurs »

zpawlo00

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Collision detection
« Reply #8 on: March 25, 2018, 03:37:50 pm »
Oh i didn't realise that thanks

AzkaIsHere

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: Collision detection
« Reply #9 on: April 20, 2018, 12:28:34 am »
Hey man I am struggling with trying to do a collision detection with classes too and I can't get to do it, can you reply please with the complete process of how you did it? :D

 

anything