Giter Club home page Giter Club logo

Comments (4)

MarcDirven avatar MarcDirven commented on September 24, 2024 2

Hi Fallahn, NathanHoekstra is my collegue. It turned out we designed our map faulty. We designed the map with tiles only, while your library only can intersect with objects, which makes sence. Thanks alot for your help!

from tmxlite.

fallahn avatar fallahn commented on September 24, 2024 1

tmxlite differs from sfml-tmxloader in that it only parses the data stored in a tmx map file into a set of C++ objects. It doesn't support rendering with sfml directly like sfml-tmxloader does, although there is an example in the repository of how to render maps loaded by tmxlite with sfml.

To do collision detection I would do something like this:

  • Create a 'collision' or 'intersectable' layer in Tiled which is an object group type. Place down objects on this layer which represent the collision zones. You can use different shapes for your objects, but to start simple I recommend just using rectangles to create AABBs
  • Load the map with tmxlite and grab the layers vector from the Map object using getLayers(). Find your collision layer, and then get all the objects from that layer (see this example for how to do that).
  • Then for each object use getAABB() and convert it to sf::FloatRect and store it somewhere, for an example std::vector<sf::FloatRect> which will become your list of collision objects.

Once you have all your collision data loaded you can use it with a physics library like box2D or start creating your own collision. I've written an sfml tutorial here which also has links to other tutorials at the bottom. HTH!

from tmxlite.

fallahn avatar fallahn commented on September 24, 2024 1

I think the AL lib part is a separate error and should appear on its own line (it's to do with the program aborting). The actual error is the assertion failing - it checks to see if getType() is returning an object type layer, because that's what you're requesting, but it seems the layer you want is not the correct type.

You can do something like this:

for(const auto& layer : layers)
{
    if(layer->getType() == tmx::Layer::Type::Object)
    {
        //this is the layer you want, try getLayerAs() here.
    }
}

or if you want to use some STL magic:

auto result = std::find_if(layers.begin(), layers.end(),[](const tmx::Layer& l){return l->getType() == tmx::Layer::Type::Object;});
if(result != layers.end())
{
    const auto& objectLayer = result->getLayerAs<tmx::ObjectGroup>();
}
else
{
    std::cout << "Object layer not found\n";
}

from tmxlite.

NathanHoekstra avatar NathanHoekstra commented on September 24, 2024

Hi, thanks for the quick response!

Based on your information. I am using the following code. Note i am sure that my 3th Tiled layer is the layer i want to check.
std::vector<sf::FloatRect> bounds;
const auto& layer = map.getLayers()[2]; const auto& objects = layer->getLayerAs<tmx::ObjectGroup>().getObjects();
for (const auto& object : objects) {
tmx::FloatRect rect = object.getAABB();
bounds.push_back(sf::FloatRect(rect.left, rect.top, rect.width, rect.height));
}
But on runtime i get the following assertion error:
Expression: getType() == Type::Object AL lib: (EE) alc_cleanup: 1 device not closed
It gives this error on line 88 in the ObjectGroup.hpp file

from tmxlite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.