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

Author Topic: Trying to place collision in a class  (Read 1428 times)

0 Members and 1 Guest are viewing this topic.

Fnak

  • Newbie
  • *
  • Posts: 22
    • View Profile
Trying to place collision in a class
« on: April 07, 2014, 09:12:26 pm »
I've been busy trying to place my collision code (so you can't run outside the window) in a class, but seemingly fail to make it work.

Here's my source code:


Quote
#include <SFML/Graphics.hpp>
#include <iostream>
 
using namespace std;
 
 
class PlayerCollision
{
public:
        PlayerCollision(sf::Sprite playerSprite)
        {
                if (playerSprite.getPosition().x <= 0)
                {
                        cout << "collision";
                        playerSprite.setPosition(0, playerSprite.getPosition().y);
                }
 
                if (playerSprite.getPosition().x >= 1536)
                {
                        playerSprite.setPosition(1536, playerSprite.getPosition().y);
                }
 
                if (playerSprite.getPosition().y <= 32 && playerSprite.getPosition().x >= 833)
                {
                        playerSprite.setPosition(playerSprite.getPosition().x, 32);
                }
        }
};
 
int main()
{
        enum Direction { Down, Left, Right, Up };
 
        sf::Vector2i source(1, Down);
 
        sf::Vector2i screenDimensions(800, 600);
 
        float frameCounter = 0, switchFrame = 100, frameSpeed = 500;
 
        sf::RenderWindow window;
        window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y), "SFML", sf::Style::Close);
 
        window.setFramerateLimit(60);
 
        sf::Texture pTexture;
        sf::Sprite playerSprite;
 
 
        sf::Texture bTexture;
        sf::Sprite bImage;
 
        sf::Clock clock;
 
        bool updateFrame = true;
        bool InMovement = false;
 
        if(!pTexture.loadFromFile("Content/player.png"))
                cout << "Error loading player image" << endl;
 
        if(!bTexture.loadFromFile("Content/bg.png"))
                cout << "Error loading background image" << endl;
 
        playerSprite.setTexture(pTexture);
        playerSprite.setPosition(70, 230);
        bImage.setTexture(bTexture);
 
        sf::View view;
 
        view.reset(sf::FloatRect(0, 0, screenDimensions.x, screenDimensions.y));
        view.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));
 
        sf::Vector2f position(screenDimensions.x / 2, screenDimensions.y / 2);
 
        while(window.isOpen())
        {
                sf::Event Event;
                while (window.pollEvent(Event))
                {
                        switch(Event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        }
                }
 
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        source.y = Up;
                        playerSprite.move(0, -3);
                        InMovement = true;
                }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        source.y = Down;
                        playerSprite.move(0, 3);
                        InMovement = true;
                }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        source.y = Right;
                        playerSprite.move(3, 0);
                        InMovement = true;
 
                }
                else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        source.y = Left;
                        playerSprite.move(-3, 0);
                        InMovement = true;
                }
                else
                {
                        InMovement = false;
                }
 
                if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
                        updateFrame = true;
                else if(sf::Mouse::isButtonPressed(sf::Mouse::Right))
                        updateFrame = false;
 
                if(updateFrame)
                        frameCounter += frameSpeed * clock.restart().asSeconds();
                else
                        frameCounter = 0;
 
                frameCounter = (updateFrame) ? frameCounter + frameSpeed * clock.restart().asSeconds() : 0;
 
                if(InMovement)
                {
                        if(frameCounter >= switchFrame)
                        {
                                frameCounter = 0;
                                source.x++;
                                if(source.x * 32 >= pTexture.getSize().x)
                                        source.x = 0;
                        }
                }
 
                if(playerSprite.getPosition().x + 16 > screenDimensions.x / 2)
                        position.x = playerSprite.getPosition().x + 16;
                else
                        position.x = screenDimensions.x / 2;
 
                if(playerSprite.getPosition().y + 16 > screenDimensions.y / 2)
                        position.y = playerSprite.getPosition().y + 16;
                else
                        position.y = screenDimensions.y / 2;
 
                if(playerSprite.getPosition().x - 16 > screenDimensions.x / 2)
                        position.x = playerSprite.getPosition().x - 16;
                else
                        position.x = screenDimensions.x / 2;
 
                if(playerSprite.getPosition().y - 16 > screenDimensions.y / 2)
                        position.y = playerSprite.getPosition().y - 16;
                else
                        position.y = screenDimensions.y / 2;
 
                view.setCenter(position);
 
                window.setView(view);
 
                PlayerCollision pCol(playerSprite);
 
                playerSprite.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
                window.draw(bImage);
                window.draw(playerSprite);
                window.display();
        }
 
        return 0;



As you can see, the collision is in a class, and I'm not sure how it works, exactly.

And by the way,

Quote
                if (playerSprite.getPosition().x <= 0)
                {
                        cout << "collision";
                        playerSprite.setPosition(0, playerSprite.getPosition().y);
                }

It actually outputs "collision" in the command prompt, but the collision just doesn't work.

Thanks in advance and have a good evening :)!
« Last Edit: April 07, 2014, 09:18:25 pm by Fnak »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Trying to place collision in a class
« Reply #1 on: April 07, 2014, 09:50:34 pm »
This is the kind of problem where stepping through your code with a debugger is by far the best way to figure things out.

Also, you need to be a LOT more precise than "the collision just doesn't work" if you want us to be able to help you.

But just from skimming your code, the part that jumps out to me as puzzling are the four blocks like this:
if(playerSprite.getPosition().x + 16 > screenDimensions.x / 2)
    position.x = playerSprite.getPosition().x + 16;
else
    position.x = screenDimensions.x / 2;
This looks like it means "if player is 16 pixels from the right edge, move 16 to the right, and if not, move him to the center of the screen".  Is that really what you were after?

Fnak

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Trying to place collision in a class
« Reply #2 on: April 07, 2014, 10:10:26 pm »
No, what you quoted:

Quote
if(playerSprite.getPosition().x + 16 > screenDimensions.x / 2)
    position.x = playerSprite.getPosition().x + 16;
else
    position.x = screenDimensions.x / 2;

has nothing to do with player collision, it sets the screen to follow the player, what I meant was this class:


Quote
class PlayerCollision
{
public:
        PlayerCollision(sf::Sprite playerSprite)
        {
                if (playerSprite.getPosition().x <= 0)
                {
                        cout << "collision";
                        playerSprite.setPosition(0, playerSprite.getPosition().y);
                }
 
                if (playerSprite.getPosition().x >= 1536)
                {
                        playerSprite.setPosition(1536, playerSprite.getPosition().y);
                }
 
                if (playerSprite.getPosition().y <= 32 && playerSprite.getPosition().x >= 833)
                {
                        playerSprite.setPosition(playerSprite.getPosition().x, 32);
                }
        }
};
 

together with:
Quote
PlayerCollision pCol(playerSprite);
in the main loop.

Sorry for not being clear enough :)

Thanks!
« Last Edit: April 07, 2014, 10:12:20 pm by Fnak »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Trying to place collision in a class
« Reply #3 on: April 07, 2014, 10:16:59 pm »
Now that you've pointed that out I think I can see what confused me.

That class should just be a function.  You aren't doing anything with the class other than invoking its constructor.

Admittedly that "function" seems sensible, but again you haven't really explained what "the collision just doesn't work" means so I'm not sure what to look for.

 

anything