SFML Perfect Pixel collision detection

  • zpawlo02
    Likes 0

    Problem Description

    I want to use method from this tut https://www.youtube.com/watch?v=g5hFOANHjlQ

    but this error is pooping when im building project:

    1>b:\graka\graka\tgame.cpp(66): error C2664: 'bool Collision::PixelPerfectTest(const sf::Sprite &,const sf::Sprite &,sf::Uint8)': cannot convert argument 1 from 'TPlayer **' to 'const sf::Sprite &'
    1>b:\graka\graka\tgame.cpp(66): note: Reason: cannot convert from 'TPlayer **' to 'const sf::Sprite'
    1>b:\graka\graka\tgame.cpp(66): note: No constructor could take the source type, or constructor overload resolution was ambiguous

    #include "TPlayer.h"
    #include <SFML\Graphics.hpp>
    #include "Collision.h"
    #include <iostream>
    
    
    TPlayer::TPlayer(float t_X, float t_Y)
    {
    	
    	/*playerTexture.loadFromFile("player_texture.jpg");
    	if (!playerTexture.loadFromFile("player_texture.jpg"))
    	{
    		std::cout << "Load failed!";
    		
    	}
    	*/
    	
    	
    	
    	Collision::CreateTextureAndBitmask(playerTexture, "player_texture.jpg");
    	playerSprite.setTexture(playerTexture);
    	playerSprite.setPosition(t_X, t_Y);
    	playerSprite.setOrigin(sf::Vector2f(playerSprite.getTexture()->getSize().x * 0.5, playerSprite.getTexture()->getSize().y * 0.5));
    
    	
    
    }
    
    void TPlayer::draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
    	
    	target.draw(this->playerSprite, states);
    }
    
     void TPlayer::update()
    {
    	
    	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left) && this->moveLeft() >20)
    	{
    		
    		playerSprite.move(-7, 0);
    	}
    	else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right) && this->moveRight() < 880 )
    	{
    		
    		playerSprite.move(7, 0);
    	}
    	else
    	{
    		
    		playerSprite.move(0, 0);
    	}
    }
    
    float TPlayer::moveLeft()
    {
    	
    	return this->playerSprite.getPosition().x - playerSprite.getOrigin().x / 2.f;
    	
    }
    
    float TPlayer::moveRight()
    {
    	
    	return this->playerSprite.getPosition().x + playerSprite.getOrigin().x / 2.f;
    }
    
    
    #include "TBlocks.h"
    #include "Collision.h"
    #include <iostream>
    
    //ZROB RANDOMOWE BLOKI!!
    
    TBlocks::TBlocks(float t_X, float t_Y)
    {
    	
    	Collision::CreateTextureAndBitmask(block_texture, "block_1.png");
    	blockSprite.setTexture(block_texture);
    	blockSprite.setPosition(t_X, t_Y);
    	blockSprite.setOrigin(sf::Vector2f(blockSprite.getTexture()->getSize().x * 0.5, blockSprite.getTexture()->getSize().y * 0.5));
    }
    
    void TBlocks::draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
    	target.draw(this->blockSprite, states);
    }
    void TBlocks::update_block()
    {
    	blockSprite.move(0, 4);
    }
    #include "TGame.h"
    #include "TBlocks.h"
    #include "Collision.h"
    #include <SFML\Graphics.hpp>\
    
    #define WIDTH 900
    #define HEIGTH 950
    
    
    
    
    TGame::TGame(TPlayer * player_, TBlocks * block_ ) : player(player_), block(block_), mWindow(sf::VideoMode(WIDTH, HEIGTH), "GAME") 
    {
    	mWindow.setFramerateLimit(60);
    	
    }
    
    
    void TGame::run()
    {
    	while (mWindow.isOpen())
    	{
    		processEvents();
    		update_game();
    		render();
    	}
    }
    
    void TGame::processEvents()
    {
    	sf::Event event;
    	while (mWindow.pollEvent(event))
    	{
    		if (event.type == sf::Event::Closed)
    			mWindow.close();
    		break;
    	}
    }
    void TGame::gameClose()
    {
    	mWindow.close();
    }
    
    void TGame::render()
    {
    	mWindow.clear();
    
    	mWindow.draw(*block);
    	mWindow.draw(*player);
    	block->update_block();
    	player->update();
    
    
    	mWindow.display();
    }
    
    void TGame::update_game()
    {
    	
    
    }
    
    
    void TGame::checkCollision()
    {
    	if (Collision::PixelPerfectTest(&player, &block));
    	{
    		mWindow.close();
    	}
    
    }
    
    
    

     

  • Sonar Systems admin
    Likes 0

    Have you tried the code from the github page?


    This reply has been verified.
  • zpawlo02
    Likes 0

    Main code? No only on my code 

    #include <SFML\Graphics.hpp>
    #include "TPlayer.h"
    #include "TGame.h"
    #include "Collision.h"
    #include "TBlocks.h"
    
    #define WIDTH 900
    #define HEIGTH 950
    
    
    int main()
    {
    	TPlayer player(WIDTH / 2, HEIGTH / 2.5); // jakieś wsp.
    	TBlocks block(WIDTH / 2, HEIGTH / 100);
    
    	TGame game(&player, &block);
    	game.run();
    
    }

    This is my source

  • Sonar Systems admin
    Likes 0

    Try the code from the github page?

Login to reply