Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colission with player sprite #70

Closed
Rikora opened this issue Sep 23, 2016 · 6 comments
Closed

Colission with player sprite #70

Rikora opened this issue Sep 23, 2016 · 6 comments

Comments

@Rikora
Copy link

Rikora commented Sep 23, 2016

Hey!

I have created a tilemap with simple rectangular platforms and I have added an object layer and marked the platforms in order to perform colission. I have checked your example folder for the b2d map and used the same code for the debug shapes so I could see the outlines (red) of the object layer objects.

Now I want the player sprite in the project to perform colission with the platforms, which shouldn't be too complicated as the sprite is a rectangular shape and the platforms are rectangular shapes aswell. Though, I can't get it to work, what would be an easy way to do it?

@Rikora Rikora closed this as completed Sep 24, 2016
@fallahn
Copy link
Owner

fallahn commented Sep 24, 2016

Hi, as you 've closed this I'll assume you found a solution - there are a couple of links you can check out though:
http://trederia.blogspot.co.uk/2013/10/collision-detection-with-tiled-maps.html
http://trederia.blogspot.co.uk/2016/02/2d-physics-101-pong.html

Alternatively my SFML frame work xygine has built in tmx/collision support (check the 2D tilemap demo in the included example)

@Rikora
Copy link
Author

Rikora commented Sep 24, 2016

Yeah, I solved it and thanks for the links 👍 But if you solve one problem, another one pops up, hehe...

So, right now it's detecting colission with the player sprite and the platforms, so I can jump on them and everything works fine that way. But, as it seems, the colission is constantly checking all of the objects in the layer, something which could be fixed by using a quadtree, which you mentioned in your blog and on the homepage of this repository.

So I simply did:

ml.UpdateQuadTree(sf::FloatRect(0.f, 0.f, 800.f, 600.f));
 std::vector<tmx::MapObject*> objects = ml.QueryQuadTree(player.getPlayer().getGlobalBounds());

and then I check for colission with the objects in the layer. But it seems like it's not working as expected as the debug text prints out a mix of 'true' (if colission occur) and 'false' (colission didn't occur), when it should printout only 'true' (if I'm intersecting with a platform) as I should only be checking with each platform at a time.

//Check for colission
        for (auto& layer : layers)
        {
            if (layer.type == tmx::ObjectGroup)
            {
                for (auto& obj : layer.objects)
                {
                    if (layer.name == "Collide")
                    {
                        if (obj.GetAABB().intersects(player.getPlayer().getGlobalBounds()))
                        {
                            std::cout << "True" << std::endl;
                            whileJump = false;
                            player.accelarationValue = 0;
                        }

                        else
                            std::cout << "False" << std::endl;
                    }
                }
            }
        }

Do you have any ideas? @fallahn

@Rikora Rikora reopened this Sep 24, 2016
@fallahn
Copy link
Owner

fallahn commented Sep 24, 2016

No, that sound right. If a layer or quad tree query returns 3 platforms and you only intersect one, then the output would be

true
false
false

or similar, as you're only colliding with one of the 3 platforms. Try also outputting the layer object count, and see if it matches up with the number of true/false statements in the console

@Rikora
Copy link
Author

Rikora commented Sep 24, 2016

Hmm, oh that sounds quite right, yeah. But still, I'm having an issue how to make the player unable to continue walking when he's actually in midair, which is actually why I wanted it to only print 'true' when I was intersecting with one platform. Because if it would, I could have a boolean in there which would turn true if I was intersecting and then false if I wouldn't intersect. Then I could use this boolean to restrict the player movement and simply let the player fall free if not on the platform. But as it is now, this boolean would switch from true to false even if I was on one single platform, making this approach useless. What would be the recommended approach? :) @fallahn

@fallahn
Copy link
Owner

fallahn commented Sep 24, 2016

This might help :)

@Rikora
Copy link
Author

Rikora commented Sep 24, 2016

Yep, I have to look into the art of game design a bit more. Thanks for the help anyways and the effort you put into the library.

@Rikora Rikora closed this as completed Sep 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants