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

Author Topic: Problem with collision SFML 2.0  (Read 1465 times)

0 Members and 1 Guest are viewing this topic.

smiaro

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Problem with collision SFML 2.0
« on: December 30, 2013, 10:27:17 am »
Hello, I have problem with collisions, my character ignores conficlting sprites.
I have also problem because he can jumps if he is in air :(
Please correct my code, I have to pass my C++ project on university.

Player.cpp
#include <SFML/Graphics.hpp>
#include "Player.h"
#include "Level.h"


void Player::Draw(sf::RenderWindow & gameWindow)
{
        const float gravity = 0.9;
        int groundHeight = 368;
        sf::Vector2f velocity(sf::Vector2f(0, 0));
        float moveSpeed = 2.0f;
        float jumpSpeed = 10.0f;

        Level level;

        enum Direction { Down,Left, Right, Up }; //{0,1}
        sf::Vector2i source(1, Down);

        gameWindow.setKeyRepeatEnabled(false);

        sf::Texture playerImage;
        playerImage.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/player.png");
        sf::Sprite player(playerImage);
        player.setPosition(0, 50);

        while (gameWindow.isOpen()) {
                sf::Event event;
                while (gameWindow.pollEvent(event)) {
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                source.y = Right;
                                if (level.Collision(level.cabinets) || level.Collision(level.microwaves) || level.Collision(level.fridges) ||
                                        level.Collision(level.boxs) || level.Collision(level.chairs) || level.Collision(level.ovens) ||
                                        level.Collision(level.tables))
                                        velocity.x = 0;
                                else
                                        velocity.x = +moveSpeed;
                        }
                        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                source.y = Left;
                                if (level.Collision(level.cabinets) || level.Collision(level.microwaves) || level.Collision(level.fridges) ||
                                        level.Collision(level.boxs) || level.Collision(level.chairs) || level.Collision(level.ovens) ||
                                        level.Collision(level.tables))
                                        velocity.x = 0;
                                else
                                velocity.x = -moveSpeed;
                        }
                        else
                        {
                                source.y = Down;
                                velocity.x = 0;
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
                                velocity.y = -jumpSpeed;
                        if (event.key.code == sf::Keyboard::Escape)
                                return;
                        break; 
                }

                player.move(velocity.x, velocity.y);
                if (player.getPosition().y < groundHeight || velocity.y < 0)
                {
                        velocity.y += gravity;
                }
                else
                {
                        player.setPosition(player.getPosition().x, groundHeight - player.getScale().y);
                        velocity.y = 0;
                }

                source.x++;
                if (source.x * 32 >= playerImage.getSize().x)
                        source.x = 0;

                player.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
                level.Draw(gameWindow);
                gameWindow.draw(player);
                gameWindow.display();
                gameWindow.clear();
        }

}
 

Level.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Level.h"
#include "Player.h"

void Level::Load1(sf::RenderWindow& gameWindow) {
        Player player;
        Draw(gameWindow);
        player.Draw(gameWindow);
}

void Level::Draw(sf::RenderWindow& gameWindow) {
        sf::Texture cabinet,bg,microwave,fridge,table,oven,box, chair;
        cabinet.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/cabinet.png");
        bg.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/bg.png");
        microwave.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/microwave.png");
        fridge.loadFromFile("C:/Users/Marcin/Desktop/Teddies/Release/Images/fridge.png");
        sf::Sprite cabinets(cabinet);
        sf::Sprite bgs(bg);
        sf::Sprite microwaves(microwave);
        sf::Sprite fridges(fridge);
        bgs.setPosition(0, 0);
        cabinets.setPosition(0, 405);
        fridges.setPosition(137, 295);
        microwaves.setPosition(30, 367);
        gameWindow.draw(bgs);
        gameWindow.draw(cabinets);
        gameWindow.draw(microwaves);
        gameWindow.draw(fridges);
}

//DODANE DLA SPROBOWANIA KOLIZJI
bool Level::Collision(sf::Sprite x)
{
        Player p;

        sf::IntRect playerRect(

                p.player.getPosition().x,
                p.player.getPosition().y,
                p.player.getGlobalBounds().width,
                p.player.getGlobalBounds().height
                );

        sf::IntRect spriteRect(

                x.getPosition().x,
                x.getPosition().y,
                x.getGlobalBounds().width,
                x.getGlobalBounds().height
                );

        if (playerRect.intersects(spriteRect))
                return true;
        else
                return false;
}

////
 

Level.h
#pragma once
class Level
{
public:
        void Load1(sf::RenderWindow& gameWindow);
        void Draw(sf::RenderWindow& gameWindow);

        //DODANE DLA SPROBOWANIA KOLIZJI
        sf::Sprite cabinets;
        sf::Sprite microwaves;
        sf::Sprite fridges;

        bool Collision(sf::Sprite x);
        ////
};

 

Player.h
#pragma once
class Player
{
public:
        void Draw(sf::RenderWindow& gameWindow);
       
        //DODANE DLA SPROBOWANIA KOLIZJI
        sf::Sprite player;
        ////
};


 

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Problem with collision SFML 2.0
« Reply #1 on: December 30, 2013, 11:09:53 am »
Create an onGround boolean variable, set it to true when your player is on the ground or false otherwise. Then only let the player jump when onGround is true.

In Level::Collision, Player p is not your real player variable. How can you expect your real player to react with other sprites?

smiaro

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Problem with collision SFML 2.0
« Reply #2 on: December 30, 2013, 11:26:02 am »
So how to change this player ?;>

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem with collision SFML 2.0
« Reply #3 on: December 30, 2013, 06:58:30 pm »
What is unclear at G.'s description? It should be straightforward to translate them into code...

Concerning the "real player" issue, you should rather pass the player as a parameter than declare it locally.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: