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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - guptarohan

Pages: [1]
1
General / collision on vertex array tile engine
« on: December 10, 2013, 06:39:35 pm »
Hello there,
I am making a simple 2d rpg with SFML,
I was wondering how to implement collision with my tile engine, which is based on the example on this page.
http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php.
How do I do the following:
a)check which tile the sprite (player) is on
b)specify which tiles are crossable
c)stop the player from going on those tiles

Thanks, Rohan

2
Graphics / Tile system
« on: December 10, 2013, 03:15:16 pm »
Hello there,
Question:
how does view function work with an array based tile engine map? Can I have one large tile map or do I need many small maps. Will I be rotate the view or use the viewport functions?

Explanation:
I am making a small 2d rpg with sfml. My tile engine is based on the vertex array example
http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php
I was planning on using sf::view to scroll the six different 'areas' of my single large tilemap(which is 6 times the screen). I wanted to use views like this
eg. 'view.move(200,200)' to scroll the area to area 2 when the house intersects with the player.
When I do this nothing happens! I think this may be because the vertex array style does not render off screen. I think a solution would be to make many small tilemaps and switch them out, but that would not allow me to manipulate other view functions.


#include <SFML/Graphics.hpp>  //includes
#include <SFML/System.hpp>
#include <iostream>
#include <vector>

class TileMap : public sf::Drawable, public sf::Transformable // see link, vertex array tile engine
{
public:

    bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
    {
        // load the tileset texture
        if (!m_tileset.loadFromFile(tileset))
            return false;

        // resize the vertex array to fit the level size
        m_vertices.setPrimitiveType(sf::Quads);
        m_vertices.resize(width * height * 4);

        // populate the vertex array, with one quad per tile
        for (unsigned int i = 0; i < width; ++i)
            for (unsigned int j = 0; j < height; ++j)
            {
                // get the current tile number
                int tileNumber = tiles[i + j * width];

                // find its position in the tileset texture
                int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
                int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);

                // get a pointer to the current tile's quad
                sf::Vertex* quad = &m_vertices[(i + j * width) * 4];

                // define its 4 corners
                quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
                quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
                quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
                quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);

                // define its 4 texture coordinates
                quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
            }

        return true;
    }

private:

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // apply the transform
        states.transform *= getTransform();

        // apply the tileset texture
        states.texture = &m_tileset;

        // draw the vertex array
        target.draw(m_vertices, states);
    }

    sf::VertexArray m_vertices;
    sf::Texture m_tileset;
};

int main(){

std::vector< std::vector<int> > map(4, std::vector<int>(4));  // Define the map


 const int level[] =
    {
0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 9, 7, 7, 7, 7, 7, 7, 7, 10, 10, //numbers stand for tiles (see key)
0, 2, 0, 0, 3, 0, 1, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 10, 10,
0, 2, 0, 1, 1, 1, 1, 0, 0, 0, 7, 7, 7, 7, 8, 7, 7, 7, 10, 10,
0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 10, 10,
0, 2, 0, 1, 0, 0, 0, 3, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
4, 2, 4, 4, 11, 4, 4, 4, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4, 2, 4, 11, 4, 11, 4, 4, 2, 4, 0, 5, 5, 5, 5, 5, 0, 2, 2, 2,
4, 2, 4, 4, 4, 4, 4, 4, 2, 4, 0, 5, 5, 5, 5, 5, 0, 2, 1, 1,
4, 2, 2, 2, 2, 2, 2, 2, 2, 4, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 3, 0, 0, 0, 0, 6, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

if  (player.getGlobalBounds().intersects(house.getGlobalBounds() ) ){    // if player intersects with house
    intersects = true; //set intersects to true
    view.move(200, 200);  // use the View.move() function to scroll the map to 'area' 2 (house)
}

window.draw(map);  //draws and displays map
window.display();

}
 
Thanks, Rohan(I am new to SFML and the forums)

Pages: [1]