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

Author Topic: Need serious help with collision  (Read 1411 times)

0 Members and 1 Guest are viewing this topic.

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Need serious help with collision
« on: October 26, 2015, 06:53:47 pm »
Ok so i'm trying to do some basic collision, but I can not for the life of me figure it out. I never could when i was doing this a year ago either. I got window bounds collision down, but i cant seem to make good collision with an object. I figured it would be just the same as a window collision but inverted but no. It's really starting to anger me. Someone please help. I've attached the red and blue squares to this post so you can use them to test the code.

I'm having trouble with "Object Collision Version 1"

#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;

void LoadImage(sf::Texture* texture)
{
    texture->loadFromFile("Red_Square.png");
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "App");
    window.setFramerateLimit(30);

    //Player
    sf::Texture texture;
    sf::Sprite sprite;

    LoadImage(&texture);

    sprite.setTexture(texture);

    //Calculate sprite origin - Will get the center of the sprite no matter what size it is
    int spriteOriginX = sprite.getGlobalBounds().height / 2;
    int spriteOriginY = sprite.getGlobalBounds().height / 2;

    cout << "Sprite Center: " << spriteOriginX << " " << spriteOriginY << endl; //Debug

    sprite.setOrigin(spriteOriginX, spriteOriginY);


    //Object
    sf::Texture objectTexture;
    sf::Sprite objectSprite;

    if(!objectTexture.loadFromFile("Blue_Square.png"))
    {
        cout << "Error loading blue square" << endl;
    }

    objectSprite.setTexture(objectTexture);

    objectSprite.setPosition(100, 100);

    //Calculate sprite origin - Will get the center of the sprite no matter what size it is
    int objectSpriteOriginX = objectSprite.getGlobalBounds().height / 2;
    int objectSpriteOriginY = objectSprite.getGlobalBounds().height / 2;

    cout << "objectSprit Center: " << objectSpriteOriginX << " " << objectSpriteOriginY << endl; //Debug

    objectSprite.setOrigin(objectSpriteOriginX, objectSpriteOriginY);

    sf::Event event;

    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                    window.close();
                break;

                case sf::Event::Resized:
                    sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
                    window.setView(sf::View(viewArea));
                break;
            }
        }

        //Movement
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        {
            sprite.move(0, -3);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            sprite.move(-3, 0);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        {
            sprite.move(0, 3);
        }
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        {
            sprite.move(3, 0);
        }

        //Collision Version 2 - Window collision only
        if(sprite.getPosition().y - spriteOriginY <= 0) //W
        {
            sprite.move(0, 3);
        }
        if(sprite.getPosition().x - spriteOriginX <= 0) //A
        {
            sprite.move(3, 0);
        }
        if(sprite.getPosition().y + spriteOriginY >= window.getSize().y) //S
        {
            sprite.move(0, -3);
        }
        if(sprite.getPosition().x + spriteOriginX >= window.getSize().x) //D
        {
            sprite.move(-3, 0);
        }

        //Object Collision Version 1
        if(sprite.getPosition().y >= objectSprite.getPosition().y) //W
        {
            sprite.setPosition(objectSprite.getPosition().y - objectSpriteOriginY, sprite.getPosition().x);
        }
        /*
        if(sprite.getPosition().x == objectSpriteOriginX <= objectSprite.getPosition().x) //A
        {
            sprite.move(3, 0);
        }
        if(sprite.getPosition().y == objectSpriteOriginY >= objectSprite.getPosition().y) //S
        {
            sprite.move(0, -3);
        }
        if(sprite.getPosition().x == objectSpriteOriginX >= objectSprite.getPosition().x) //D
        {
            sprite.move(-3, 0);
        }
*/


        window.clear(sf::Color::White);
        window.draw(sprite);
        window.draw(objectSprite);
        window.display();
    }

    return 0;
}
 

« Last Edit: October 26, 2015, 07:32:51 pm by Chay Hawk »


 

anything