Example #1
0
 bool testForStanding(sf::Sprite sp2) {
     if ((sp2.getGlobalBounds().left+sp2.getGlobalBounds().width > sprite.getPosition().x) &&
         (sp2.getGlobalBounds().left < sprite.getPosition().x+sprite.getTextureRect().width*sprite.getScale().x)) {
         return true;
     }
     return false;
 }
Example #2
0
bool overlap(sf::Sprite& sprite1, sf::Sprite& sprite2)
{
	sf::FloatRect rectangle1 = sprite1.getGlobalBounds();
	sf::FloatRect rectangle2 = sprite2.getGlobalBounds();

	// Rectangles with negative dimensions are allowed, so we must handle them correctly
	// Compute the min and max of the first rectangle on both axes
	float r1MinX = std::min(rectangle1.left, rectangle1.left + rectangle1.width);
	float r1MaxX = std::max(rectangle1.left, rectangle1.left + rectangle1.width);
	float r1MinY = std::min(rectangle1.top, rectangle1.top + rectangle1.height);
	float r1MaxY = std::max(rectangle1.top, rectangle1.top + rectangle1.height);

	// Compute the min and max of the second rectangle on both axes
	float r2MinX = std::min(rectangle2.left, rectangle2.left + rectangle2.width);
	float r2MaxX = std::max(rectangle2.left, rectangle2.left + rectangle2.width);
	float r2MinY = std::min(rectangle2.top, rectangle2.top + rectangle2.height);
	float r2MaxY = std::max(rectangle2.top, rectangle2.top + rectangle2.height);

	// Compute the intersection boundaries
	float interLeft = std::max(r1MinX, r2MinX);
	float interTop = std::max(r1MinY, r2MinY);
	float interRight = std::min(r1MaxX, r2MaxX);
	float interBottom = std::min(r1MaxY, r2MaxY);

	// If the intersection is valid (positive non zero area), then there is an intersection
	return ((interLeft <= interRight) && (interTop <= interBottom));
}
Example #3
0
 bool PixelPerfectTest(const sf::Sprite& Object1, const sf::Sprite& Object2, sf::Uint8 AlphaLimit) {
     sf::FloatRect Intersection;
     if (Object1.getGlobalBounds().intersects(Object2.getGlobalBounds(), Intersection)) {
         sf::IntRect O1SubRect = Object1.getTextureRect();
         sf::IntRect O2SubRect = Object2.getTextureRect();
         
         sf::Uint8* mask1 = Bitmasks.GetMask(Object1.getTexture());
         sf::Uint8* mask2 = Bitmasks.GetMask(Object2.getTexture());
         
         // Loop through our pixels
         for (int i = Intersection.left; i < Intersection.left+Intersection.width; i++) {
             for (int j = Intersection.top; j < Intersection.top+Intersection.height; j++) {
                 
                 sf::Vector2f o1v = Object1.getInverseTransform().transformPoint(i, j);
                 sf::Vector2f o2v = Object2.getInverseTransform().transformPoint(i, j);
                 
                 // Make sure pixels fall within the sprite's subrect
                 if (o1v.x > 0 && o1v.y > 0 && o2v.x > 0 && o2v.y > 0 &&
                     o1v.x < O1SubRect.width && o1v.y < O1SubRect.height &&
                     o2v.x < O2SubRect.width && o2v.y < O2SubRect.height) {
                     
                     if (Bitmasks.GetPixel(mask1, Object1.getTexture(), (int)(o1v.x)+O1SubRect.left, (int)(o1v.y)+O1SubRect.top) > AlphaLimit &&
                         Bitmasks.GetPixel(mask2, Object2.getTexture(), (int)(o2v.x)+O2SubRect.left, (int)(o2v.y)+O2SubRect.top) > AlphaLimit)
                         return true;
                     
                 }
             }
         }
     }
     return false;
 }
bool overlap(sf::Sprite& sprite1, sf::Sprite& sprite2)
{
    sf::FloatRect rectangle1 = sprite1.getGlobalBounds();
    sf::FloatRect rectangle2 = sprite2.getGlobalBounds();

    // Rectangles with negative dimensions are allowed, so we must handle them correctly
    // Compute the min and max of the first rectangle on both axes
    float r1MinX = std::min(rectangle1.left, rectangle1.left + rectangle1.width); //left
    float r1MaxX = std::max(rectangle1.left, rectangle1.left + rectangle1.width);//right
    float r1MinY = std::min(rectangle1.top, rectangle1.top + rectangle1.height); // top
    float r1MaxY = std::max(rectangle1.top, rectangle1.top + rectangle1.height); // bottom of the maincharactersprite

    // Compute the min and max of the second rectangle on both axes
    float r2MinX = std::min(rectangle2.left, rectangle2.left + rectangle2.width);//left
    float r2MaxX = std::max(rectangle2.left, rectangle2.left + rectangle2.width);//right
    float r2MinY = std::min(rectangle2.top, rectangle2.top + rectangle2.height);//top of the blocksprite
    float r2MaxY = std::max(rectangle2.top, rectangle2.top + rectangle2.height); // bottom

    // Compute the intersection boundaries
    float interLeft = std::max(r1MinX, r2MinX);//get the most right one
    float interTop = std::max(r1MinY, r2MinY);//get the higher one
    float interRight = std::min(r1MaxX, r2MaxX);// get the most left one
    float interBottom = std::min(r1MaxY, r2MaxY);// the the lowest one

    //want to check if bottom of the mainCharacterSprite and top of the Block sprite are overlaping

    // If the intersection is valid (positive non zero area), then there is an intersection
    return ((interLeft <= interRight) && (interTop <= interBottom));
}
Example #5
0
void move(float x, float y, sf::Sprite& sprite, sf::Sprite obstacle)
{
    sprite.move(x, y);

    if(sprite.getGlobalBounds().intersects(obstacle.getGlobalBounds()))
    {
        sprite.move(-x, -y);
    }
}
Example #6
0
bool overlap(sf::Sprite& sprite1, sf::Sprite& sprite2)
{
	sf::FloatRect rectangle1 = sprite1.getGlobalBounds();		//get top left corner position and bottom right position 
	sf::FloatRect rectangle2 = sprite2.getGlobalBounds();

	return rectangle1.left <= (rectangle2.left + rectangle2.width) &&
		rectangle2.left <= (rectangle1.left + rectangle1.width) &&
		rectangle1.top <= (rectangle2.top + rectangle2.height) &&
		rectangle2.top <= (rectangle1.top + rectangle1.height);
}
Example #7
0
sf::FloatRect DynamicBlock::getDetectionBox(sf::Sprite& p) const
{
	//Create a box around the block with space for the player:
	return sf::FloatRect
	(
		(_shape.getPosition().x - (p.getGlobalBounds().width / 2)),
		_shape.getPosition().y,
		(_shape.getSize().x + p.getGlobalBounds().width),
		(_shape.getSize().y + (p.getGlobalBounds().height / 2))
	);
}
Example #8
0
bool Game::bombsCollision(sf::Sprite characterSprite, sf::Sprite sprite)
{
	if (characterSprite.getGlobalBounds().intersects(sprite.getGlobalBounds()))
	{
		myLives -= 1;
		return true;
	}
	else
	{
		return false;
	}
}
Example #9
0
	bool CorrectM(sf::Sprite &s, const sf::RenderWindow &w)
	{
	if (
	sf::Mouse::getPosition(w).x > s.getGlobalBounds().left &&
	sf::Mouse::getPosition(w).y > s.getGlobalBounds().top &&
	sf::Mouse::getPosition(w).x < s.getGlobalBounds().left + s.getGlobalBounds().width &&
	sf::Mouse::getPosition(w).y < s.getGlobalBounds().top + s.getGlobalBounds().height
	)
	return 1;
	else
	return 0;
	}
Example #10
0
void Spawner::update(sf::Time h, InputHandler& input, int& score, int& iState, sf::Sprite& energy) {
	// Check for clicks
	std::shared_ptr<sf::RectangleShape> pMouseArea = nullptr;
	if (input.bLeftClick)
	{
		pMouseArea = std::shared_ptr<sf::RectangleShape>(new sf::RectangleShape);
		pMouseArea->setSize(sf::Vector2f(5, 5));
		sf::Vector2i mousePosition = input.mousePos;
		pMouseArea->setPosition(mousePosition.x, mousePosition.y);
	}

	// Update velocity
	for (int i = 0; i < entitiesSpawned.size(); i++) {
		entitiesSpawned.at(i).update(h);
		// Check for game over
		if (energy.getGlobalBounds().intersects(entitiesSpawned.at(i).getGlobalBounds()))
			iState = 3;

		// Check for kills
		if (pMouseArea != nullptr && entitiesSpawned.at(i).getGlobalBounds().intersects(pMouseArea->getGlobalBounds()))
		{
			// Kill
			entitiesSpawned.erase(entitiesSpawned.begin() + i);
			// Update score
			score += 10;
		}
	}
}
Example #11
0
 Button::Button(sf::Sprite selectedSprite, sf::Sprite unselectedSprite)
     :_buttonType(TSB), _state(0), _internalStateManaged(true)
 {
     _sprites.push_back(unselectedSprite);
     _sprites.push_back(selectedSprite);
     _clickBound = unselectedSprite.getGlobalBounds();
     updatePosition();
 }
Example #12
0
 Button::Button(sf::Sprite selectedSprite, sf::Sprite unselectedSprite, sf::Text text, AlignmentData pData)
     :_buttonType(TSTB), _textPositionData(pData), _state(0), _internalStateManaged(true)
 {
     _sprites.push_back(unselectedSprite);
     _sprites.push_back(selectedSprite);
     _texts.push_back(text);
     _clickBound = unselectedSprite.getGlobalBounds();
     updatePosition();
 }
// Initialising functions
// Detects if a mouse is hovering over a button based on the mouse position, and if it is, tints the button slightly grey.
// If it isn't then the tint is removed.
// This version of the function is for sprites
void cScreen::buttonHighlightDetect(sf::Vector2i &mousePos, sf::Sprite &button) {
	// If the mouse's current position is within the bounds of the button
	if (button.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
		// Set the tint of the button to RGB color 200,200,200
		button.setColor(sf::Color(200, 200, 200));
	}
	// If not set the tint of the button to white
	else button.setColor(sf::Color::White);
}
Example #14
0
bool PixelPerfectTest( const sf::Sprite& object1, const sf::Sprite& object2, sf::Uint8 AlphaLimit, const sf::Image & object1CollisionMask,  const sf::Image & object2CollisionMask )
{
    sf::FloatRect intersection;
    if ( object1.getGlobalBounds().intersects( object2.getGlobalBounds(), intersection ) )
    {
        //We've got an intersection we need to process the pixels
        //in that Rect.

        //Bail out now if AlphaLimit = 0
        if ( AlphaLimit == 0 ) return true;

        //There are a few hacks here, sometimes the TransformToLocal returns negative points
        //Or Points outside the image.  We need to check for these as they print to the error console
        //which is slow, and then return black which registers as a hit.

        sf::Vector2f o1v;
        sf::Vector2f o2v;
        //Loop through our pixels
        for ( int i = intersection.left; i < intersection.left+intersection.width; i++ ) //4ian : Rect now uses width/height.
        {
            for ( int j = intersection.top; j < intersection.top+intersection.height; j++ ) //4ian : Rect now uses width/height.
            {

                o1v = object1.getInverseTransform().transformPoint( i, j ); //Creating objects each loop :(
                o2v = object2.getInverseTransform().transformPoint( i, j );

                //Hack to make sure pixels fall within the Sprite's Image
                if ( o1v.x > 0 && o1v.y > 0 && o2v.x > 0 && o2v.y > 0 &&
                        o1v.x < object1CollisionMask.getSize().x && o1v.y < object1CollisionMask.getSize().y &&
                        o2v.x < object2CollisionMask.getSize().x && o2v.y < object2CollisionMask.getSize().y )
                {
                    //If both sprites have opaque pixels at the same point we've got a hit
                    if (( object1CollisionMask.getPixel( static_cast<int>( o1v.x ), static_cast<int>( o1v.y ) ).a > AlphaLimit ) &&
                        ( object2CollisionMask.getPixel( static_cast<int>( o2v.x ), static_cast<int>( o2v.y ) ).a > AlphaLimit ) )
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    return false;
}
Example #15
0
    void Sprite2VertexArray(const sf::Sprite & sprite,sf::VertexArray & vertices)
    {
        sf::IntRect texBox = sprite.getTextureRect();
        sf::FloatRect box = sprite.getGlobalBounds();

		vertices.append(sf::Vertex(sf::Vector2f(box.left, box.top), sf::Vector2f((float)texBox.left, (float)texBox.top)));
		vertices.append(sf::Vertex(sf::Vector2f(box.left, box.top + box.height), sf::Vector2f((float)texBox.left, (float)(texBox.top + texBox.height))));
		vertices.append(sf::Vertex(sf::Vector2f(box.left + box.width, box.top + box.height), sf::Vector2f((float)(texBox.left + texBox.width), (float)(texBox.top + texBox.height))));
		vertices.append(sf::Vertex(sf::Vector2f(box.left + box.width, box.top), sf::Vector2f((float)(texBox.left + texBox.width), (float)texBox.top)));
    }
Example #16
0
        const sf::FloatRect     getBounds()
                                {
                                    sf::FloatRect tempRect = sprite.getGlobalBounds();

                                    tempRect.top -= ObstacleBorder;
                                    tempRect.left -= ObstacleBorder;
                                    tempRect.width += ObstacleBorder;
                                    tempRect.height += ObstacleBorder;

                                    return tempRect;
                                }
Example #17
0
 Button::Button(sf::Sprite selectedSprite, sf::Sprite unselectedSprite, sf::Text text)
     :_buttonType(TSTB), _state(0), _internalStateManaged(true)
 {
     _textPositionData.alignmentX = zf::X_Center;
     _textPositionData.alignmentY = zf::Y_Center;
     _textPositionData.offset = sf::Vector2f(0,0);
     _sprites.push_back(unselectedSprite);
     _sprites.push_back(selectedSprite);
     _texts.push_back(text);
     _clickBound = unselectedSprite.getGlobalBounds();
     updatePosition();
 }
Example #18
0
//Checks for a collision between the character and the passed sprite:
Collision Character::checkCollision(sf::Sprite& target) const
{
	//Gets the sides of the character:
	//These are represented as 1 pixel wide rectangles:
	sf::FloatRect A = _sprite.getGlobalBounds();
	sf::FloatRect topA(A.left, A.top, A.width, 1);
	sf::FloatRect bottomA(A.left, (A.top + A.height), A.width, 1);
	sf::FloatRect leftA(A.left, A.top, 1, A.height);
	sf::FloatRect rightA((A.left + A.width), A.top, 1, A.height);

	//The rectangle of the target sprite, we will check to see
	//if the character is inside this rectangle:
	sf::FloatRect B = target.getGlobalBounds();

	//Checks for collisions in the corners:
	//Top left:
	if((B.intersects(topA)) && (B.intersects(leftA)))
		return COLLISION_TOPLEFT;

	//Top right:
	if((B.intersects(topA)) && (B.intersects(rightA)))
		return COLLISION_TOPRIGHT;

	//Bottom left:
	if((B.intersects(bottomA)) && (B.intersects(leftA)))
		return COLLISION_BOTTOMLEFT;

	//Bottom right:
	if((B.intersects(bottomA)) && (B.intersects(rightA)))
		return COLLISION_BOTTOMRIGHT;

	//Otherwise, it is a single side:
	//Top:
	if(B.intersects(topA))
		return COLLISION_TOP;

	//Bottom:
	if(B.intersects(bottomA))
		return COLLISION_BOTTOM;
	
	//Left:
	if(B.intersects(leftA))
		return COLLISION_LEFT;

	//Right:
	if(B.intersects(rightA))
		return COLLISION_LEFT;

	//Otherwise, there is no collision:
	return COLLISION_NONE;
}
bool CollisionFuncs::currentCollisionR(sf::Sprite& s0, sf::Sprite& s1){
	float e0Left = s0.getPosition().x;
	float e0Right = s0.getPosition().x + s0.getGlobalBounds().width;
	float e0Top = s0.getPosition().y;
	float e0Bottom = s0.getPosition().y + s0.getGlobalBounds().height;

	float e1Left = s1.getPosition().x - 1;
	float e1Right = s1.getPosition().x + s1.getGlobalBounds().width + 1;
	float e1Top = s1.getPosition().y;
	float e1Bottom = s1.getPosition().y + s1.getGlobalBounds().height ;


	// Has collided if all conditions are met
	if (e0Left <= e1Right &&
		e0Right >= e1Left &&
		e0Top <= e1Bottom &&
		e0Bottom >= e1Top) {
		return true;
	}
	else {
		return false;
	}
}
Example #20
0
void Renderer::draw(sf::Sprite const& sprite, ZIndex_t zindex, sf::RenderStates const& states)
{
    auto bounds = sprite.getGlobalBounds();
    if(isInScreen(bounds))
    {
        m_drawCallbacks.push_back({
            zindex,
            [&sprite, states] (sf::RenderTarget& renderer)
            {
                renderer.draw(sprite, states);
            }
        });
    }
}
        void Checkbox::update(Event event)
        {
            if(!event.type == sf::Event::MouseButtonPressed and !event.type == sf::Event::MouseMoved)
                return;

            sf::Vector2f mouseCoords = sf::Vector2f(sf::Mouse::getPosition().x * viewScale + screenCornerX, sf::Mouse::getPosition().y * viewScale + screenCornerY);

            if(event.type == sf::Event::MouseMoved)
                focus = box.getGlobalBounds().contains(mouseCoords);

            if(event.type == sf::Event::MouseButtonPressed and focus)
                state = !state;

            if(state)
                check.setColor(sf::Color(255, 255, 255, 255));
            else if(focus)
                check.setColor(sf::Color(255, 255, 255, 127));
            else
                check.setColor(sf::Color(255, 255, 255, 0));
        }
Example #22
0
void Bola::colidiu(sf::Sprite lightsaber){//Testar se realmente funcionou a colisao com sabre menor/maior
    if(this->getGlobalBounds().intersects((lightsaber.getGlobalBounds())))
    {
        laser2.play();
        if(this->getPosition().y<lightsaber.getPosition().y){
            speed.y *=(-1);

            float a=this->getPosition().x-(lightsaber.getPosition().x-(50*lightsaber.getScale().x));
            if(a<-10){//Lado esquerdo
                a*=-1;
                if(speed.x>0)
                    speed.x-=a*0.1;
                else
                    speed.x-=a*0.1;
            }else if(a>10){//Lado direito
                if(speed.x>0)
                    speed.x+=a*0.1;
                else
                    speed.x+=a*0.1;
            }
        }else
            speed.x*=-1;
    }
}
//otestad (har inga vettiga cirklar) att testa med)
bool CollisionManager::Circle_CircleCollision(const sf::Sprite& sprite1, const sf::Sprite& sprite2)
{
    // avståndet mellan cirklarnas mittpunkter
    sf::Vector2f distance =
    sf::Vector2f(sprite1.getGlobalBounds().left + sprite1.getGlobalBounds().width / 2.0f,
                 sprite1.getGlobalBounds().top + sprite1.getGlobalBounds().height / 2.0f)
    -
    sf::Vector2f(sprite2.getGlobalBounds().left + sprite2.getGlobalBounds().width / 2.0f,
                 sprite2.getGlobalBounds().top + sprite2.getGlobalBounds().height / 2.0f);
    
    float magnitudeOfDistanceSquared = distance.x * distance.x + distance.y * distance.y;
    // beräkna radien på båda objekten
    float radius1 = (sprite1.getTextureRect().width * sprite1.getScale().x +
                     sprite1.getTextureRect().height * sprite1.getScale().y)/4;
    float radius2 = (sprite2.getTextureRect().width * sprite2.getScale().x +
                     sprite2.getTextureRect().height * sprite2.getScale().y)/4;
    
    float maximumCollidingDistanceBetweenBoundings = (radius1 + radius1) * (radius2 + radius2);
    
    return (magnitudeOfDistanceSquared <= maximumCollidingDistanceBetweenBoundings);
}
    void display(sf::RenderWindow* window, std::string pathImage){
        open = true;
            t.loadFromFile(pathImage);
            s = sf::Sprite();
            s.setTexture(t);
            s.scale(window->getSize().y/s.getGlobalBounds().height,window->getSize().y/s.getGlobalBounds().height);
            s.setPosition(window->getSize().x/2 - s.getGlobalBounds().width/2, 0);
        while(open){
            
            sf::Event event;
            while (window->pollEvent(event)) {
                switch (event.type) {
                case sf::Event::Closed:
                    window->close();
                    break;
                case sf::Event::KeyPressed:
                    if (event.key.code == sf::Keyboard::Escape) window->close();
                    break;
                case sf::Event::MouseButtonPressed:
                    if (event.mouseButton.button == sf::Mouse::Left) {
                        open = false;
                    }
                    break;
                default:
                    break;
                }
                if( event.type == sf::Event::KeyPressed ||
                event.type == sf::Event::MouseButtonPressed||
                event.type == sf::Event::TouchEnded           )  open = false;
            }

            window->clear();
            window->draw(s);

            window->display();
                
        }
        
        sf::Clock timer;
        sf::Sprite dark;
        sf::Texture text;
        bool closing = true;
        text.loadFromFile("res/pics/black.png");
        
        dark.setTexture(text);
        dark.setOrigin(dark.getLocalBounds().width/2,dark.getLocalBounds().height/2);
        dark.setPosition(window->getSize().x/2,window->getSize().y/2);
        float scale = 1/dark.getGlobalBounds().width;;
        
        float time = 0;
        while(closing and wantAnimation){            
            dark.setScale(scale,scale);
            time += timer.restart().asSeconds();
            if(time > 0.1){
                scale *= 1.5;
                time = 0;
            }
            window->clear();
            window->draw(s);
            window->draw(dark);
            window->display();
            if(dark.getGlobalBounds().width > window->getSize().x) closing = false;
        }
        //clean events 
        sf::Event e; while (window->pollEvent(e)) { }
    }
Example #25
0
sf::Rect<float> Entity::getBoundingBox()
{
	return m_sprite.getGlobalBounds();
}
Example #26
0
 sf::Vector2f GetSpriteCenter (const sf::Sprite& Object)
 {
     sf::FloatRect AABB = Object.getGlobalBounds();
     return sf::Vector2f (AABB.left+AABB.width/2.f, AABB.top+AABB.height/2.f);
 }
/////////////////
// BOTTOM LEFT //
/////////////////
sf::Vector2f Fonction::bottomLeftCorner(const sf::Sprite& sprite)		{ return bottomLeftCorner(sprite.getGlobalBounds()); }
////////////
// CENTER //
////////////
sf::Vector2f Fonction::centerCorner(const sf::Sprite& sprite)		{ return centerCorner(sprite.getGlobalBounds()); }
///////////////
// TOP RIGHT //
///////////////
sf::Vector2f Fonction::topRightCorner(const sf::Sprite& sprite)			{ return topRightCorner(sprite.getGlobalBounds()); }
Example #30
0
bool checkForMouseTrigger(sf::Sprite &sprite, sf::RenderWindow &window)
{
	int mouseX = sf::Mouse::getPosition().x;
	int mouseY = sf::Mouse::getPosition().y;

	sf::Vector2i windowPosition = window.getPosition();

	if (mouseX > sprite.getPosition().x + windowPosition.x && mouseX < (sprite.getPosition().x + sprite.getGlobalBounds().width + windowPosition.x)
		&& mouseY > sprite.getPosition().y + windowPosition.y + 30 && mouseY < (sprite.getPosition().y + sprite.getGlobalBounds().height + windowPosition.y + 30))
	{
		if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
		{
			return true;
		}
		return false;
	}
	return false;
}