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

Author Topic: Need help with tilemap collision (Platformer - style game)  (Read 760 times)

0 Members and 1 Guest are viewing this topic.

Cavi

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Need help with tilemap collision (Platformer - style game)
« on: December 13, 2017, 09:14:14 pm »
Hi! I'm in a rush, so let me explain quickly. I use SFML for only a month now, and I love it every bit so far, but I'm a stupid human being and bit a lot more that I can chew. Basically, my school hosts an event tommorow, and I wanted to get involved. I like to fiddle with programming and robots, so I did something like this a couple of times, but this time I think I was too cocky and decided to make a 2 player game that would be open for students to play, having only a week and little to no experience. I watched and read a lot of tutorials and got a basic engine running following videos from Sonar Systems on YouTube. Just a note, I did use his idea for an engine, but first I made sure to read it and try to understand it. At this point I know how it works, so I just went with it because of the deadline being close. The game is supposed to be something close to Super Smash Bros but with a point system. 2 players shoot each other while collecting coins for points. You lose some of them when you fly out of the map and get rewarded if you were the one to knock off the second player. A round lasts about 2-3 minutes. Simple enough, right? Not really. Collision turned out to be a lot harder than I thought it would be. Basically, I use a tilemap and a collision map, saved in a txt file, and I simply can't get it to work properly. The character just ignores the collision sometimes when I jump too high, I don't know how to make the player always stand on top of the tile, not in the middle of it or wherever the collision started, and also the gravity I've written does not work as intended. I'm gonna share files that handle movement and all the tiles. Excuse me for messy code, I'm trying to get it to work for so long now, I'll tidy it up when I'm sure it works. I will appreciate any help. And please, understand my situation, I know the best way is to simply read and learn, and try on small bits of code, but I have to get it to work by tomorrow, whatever it takes. If someone decides to help me, I promise I will not take up such projects with deadlines that are bigger than my experience, and I will put a lot more effort into learning to code properly.



#include "Player.hpp"


namespace cav
{
    sf::Sprite Player::Draw()
    {
        //this->_data->assets.LoadTexture("Player Sprite", PLAYER_SPRITE);
        //this->_sprite.setTexture(this->_data->assets.GetTexture("Player Sprite"));
        //return this->_sprite;
    }
    void Player::Move(sf::Sprite &object, bool &isJumping, bool &isGround)
    {
        std::cout << isGround;
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
        {
           velocity.x = WALK_SPEED;
           //if (facingRight == false)
           //{
               //object.setScale(4, 4);
               //facingRight = true;
           //}
           spritepos.y = Rt;
        spritepos.x++;
        if(spritepos.x * 8 >= 24)
        {
            spritepos.x = 0;
        }
        }
    else if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
        {
           velocity.x = -WALK_SPEED;
           //if (facingRight == true)
           //{
               //object.setScale(-4, 4);
               //facingRight = false;
           //}
           spritepos.y = Lf;
        spritepos.x++;
        if(spritepos.x * 8 >= 24)
        {
            spritepos.x = 0;
        }
        }else
        {
            velocity.x = 0;
        }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
        {
           velocity.y = -JUMP_SPEED;
           isGround = false;
           isJumping=true;
        }
        if(isGround == false)
        {
    if( object.getPosition().y + object.getTexture()->getSize().y < GROUND || velocity.y < 0)
        {
            if (isGround == false)
            {
            velocity.y += GRAVITY;
            }


           isJumping = false;
        }else
        {
            object.setPosition(object.getPosition().x, GROUND - object.getTexture()->getSize().y);
            velocity.y = 0;
        }
        }
        //if(isGround == true)
        //{
            //std::cout << "K O L I Z J A" << std::endl;
            //velocity.y = 0;
        //}
        if(velocity.y > 0 && isGround == true) velocity.y = 0;


        object.move(velocity.x, velocity.y);
    }
    void Player::Jump()
    {
        //Dodam pozniej
    }
    void Player::Shoot()
    {
        //Dodam pozniej
    }
    void Player::Update()
    {
            bottom = sprite.getPosition().y + sprite.getGlobalBounds().height;
            left = sprite.getPosition().x;
            right = sprite.getPosition().x + sprite.getGlobalBounds().width;
            top = sprite.getPosition().y;


            //sf::Vector2i topLeft(sf::Vector2i((int)left / 32, (int)top / 32));
            //sf::Vector2i topRight(sf::Vector2i((int)right / 32, (int)top / 32));
           // sf::Vector2i bottomLeft(sf::Vector2i((int)left / 32, (int)bottom / 32));
            //sf::Vector2i bottomRight(sf::Vector2i((int)right / 32, (int)bottom / 32));
            //tiles.clear();


            //tiles.push_back(topLeft);
            //if(std::find(tiles.begin(), tiles.end(), topRight) == tiles.end()) tiles.push_back(topRight);
            //if(std::find(tiles.begin(), tiles.end(), bottomLeft) == tiles.end()) tiles.push_back(bottomLeft);
            //if(std::find(tiles.begin(), tiles.end(), bottomRight) == tiles.end()) tiles.push_back(bottomRight);
    }
    void Player::Animation()
    {
        sprite.setTextureRect(sf::IntRect(spritepos.x * 8, spritepos.y * 8, 8, 8));


    }
}
 



#include "TileMap.hpp"
#include <fstream>
#include <iostream>
#include <string>
#include <cctype>
#include <sstream>


namespace cav
{
    void TileMap::getMap()
    {
       std::ifstream openfile("map1.txt");
       _tempMap.clear();
       _map.clear();


    if (openfile.is_open())
    {
        std::string tileLocation;
        openfile >> tileLocation;
        _tileTexture.loadFromFile(tileLocation);
        _tiles.setTexture(_tileTexture);
        while(!openfile.eof())
        {
            std::string str, value;
            std::getline(openfile, str);
            std::stringstream stream(str);


            while(std::getline(stream, value, ' '))
            {
                if(value.length() > 0)
                {
                std::string xx = value.substr(0, value.find(','));
                std::string yy = value.substr(value.find(',') + 1);


                int x, y, i, j;
                for(i = 0; i < xx.length(); i++)
                {
                    if(!isdigit(xx[i]))
                        break;
                }
                for(j = 0; j < yy.length(); j++)
                {
                    if(!isdigit(yy[j]))
                        break;
                }
                x = (i == xx.length()) ? atoi(xx.c_str()) : -1;
                y = (j == yy.length()) ? atoi(yy.c_str()) : -1;


                _tempMap.push_back(sf::Vector2i(x, y));
                }


            }


                _map.push_back(_tempMap);
                _tempMap.clear();
        }
    }
    }
    void TileMap::getColMap()
    {
       std::ifstream openfile("colmap.txt");
       std::vector<int> tempMap;
       _colMap.clear();
       tempMap.clear();


    if (openfile.is_open())
    {
        while(!openfile.eof())
        {
            std::string str, value;
            std::getline(openfile, str);
            std::stringstream stream(str);


            while(std::getline(stream, value, ' '))
            {
                if(value.length() > 0)
                {
                int a = atoi(value.c_str());
                tempMap.push_back(a);
                }


            }


                _colMap.push_back(tempMap);
                tempMap.clear();
        }
    }
    }
    void TileMap::drawMap(std::vector<sf::Sprite> &tilearray, sf::RenderWindow &window)
    {
        int k = 0;
        for(int i = 0; i < _map.size(); i++)
        {
            for(int j = 0; j < _map[i].size(); j++)
            {
                if(_map[i][j].x != -1 && _map[i][j].y != -1)
                {
                    _tiles.setPosition(j*32, i*32);
                    _tiles.setTextureRect(sf::IntRect(_map[i][j].x * 32, _map[i][j].y * 32, 32, 32));
                    window.draw(_tiles);


                }
            }
        }
    }
    void TileMap::drawColMap(Player &player, bool &isGround)
    {
        player.Update();
        //for(int i = 0; i < player.tiles.size(); i++)
        //{
            //if(_colMap[player.tiles[i].y][player.tiles[i].x] == 1)
            //{
               //isGround = true;
               //if(isGround == true)
               //{
                   //player.velocity.y = 0;
                   //break;
               //}
               //break;
            //}


        //}
        for(int i = 0; i < _colMap.size(); i++)
        {
            for(int j = 0; j < _colMap[i].size(); j++)
            {
                if(_colMap[i][j] == 1)
                {
                int bottom, top, left, right;
                bottom = i* 32 + 32;
                top = i * 32;
                right = j * 32 + 32;
                left = j * 32;
            //bottom = rect.getPosition().y + rect.getSize().y;
            //left = rect.getPosition().x;
            //right = rect.getPosition().x + rect.getSize().x;
            //top = rect.getPosition().y;


                if(player.right < left || player.left > right || player.top > bottom || player.bottom < top)
                {
                    isGround = false;


                }else
                {
                    isGround = true;
                    if(isGround == true)
                    {
                        std::cout << "Kolizja xDD" << std::endl;
                        player.velocity.y = 0;
                    }
                    return;
                }
                }




            }
        }
    }
}
 
The sections that are commented out are some other ways I tried to implement collision etc but they didn't work out so I left them in the code, but simply commented them out.
« Last Edit: December 13, 2017, 09:17:03 pm by Cavi »

 

anything