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

Author Topic: Collision Detection Issue  (Read 2029 times)

0 Members and 1 Guest are viewing this topic.

CodingNewbie

  • Newbie
  • *
  • Posts: 6
    • View Profile
Collision Detection Issue
« on: October 19, 2013, 12:15:44 am »
Hello everyone.  I am using the TMX Map Loader to load the .tmx files from the Tiled Map Editor.  In the documentation they have a section covering collision detection with this snippet of code:

bool collision;
    for(auto layer = mll.GetLayers().begin(); layer != mll.GetLayers().end(); ++layer)
    {
        if(layer->name == "Test")
        {
            for(auto object = layer->objects.begin(); object != layer->objects.end(); ++object)
            {
                                collision = object->Contains(point);
            }
        }
    }
 

For the line
collision = object->Contains(point);
, am I right to use something along the lines of animatedSprite.getPosition() in place of point?  Also, when I try to use that code to determine whether or not Collision is true or false, it is always true.  For example, if I make a move condition for my sprite stating that they can move if collision is false, else they cannot move, they will never be able to move.

I'm sure this didn't come out as clearly as it sounded in my head.  Any help with this would be greatly appreciated!


Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Collision Detection Issue
« Reply #1 on: October 19, 2013, 12:21:55 am »
It sounds like you're testing collision against every tile in the map, in which case being unable to move is exactly what we would expect unless he was outside the map entirely.

CodingNewbie

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Collision Detection Issue
« Reply #2 on: October 19, 2013, 12:22:30 am »
Let me place him outside of the map and test that...be right back.

....nope, still can't move even outside of the map boundaries.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Collision Detection Issue
« Reply #3 on: October 19, 2013, 12:25:54 am »
Well I doubt you want him outside the map in the first place.  My point was you should only be colliding with tiles you don't want him moving through.  Surely there are tiles he can move through.

Also, since you never pasted complete/minimal code (in particular, we can't see your movement code), for all we know it's not even trying to move and the collision stuff is irrelevant.

CodingNewbie

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Collision Detection Issue
« Reply #4 on: October 19, 2013, 12:28:05 am »
The more I look at this the more it doesn't make sense to me.  Collision will always be true as far as I can tell because it's initially set to be true, and then all the conditions that would change it are inside of those loops, but once the loops cease to run collision will still be true.

Here is some code:
// set up AnimatesSprite
    AnimatedSprite animatedSprite(sf::seconds(0.13));
    animatedSprite.setAnimation(walkingAnimationDown);
    animatedSprite.pause();
    animatedSprite.setPosition(700, 400);
       
        bool collision;
    for(auto layer = mll.GetLayers().begin(); layer != mll.GetLayers().end(); ++layer)
    {
        if(layer->name == "Test")
        {
            for(auto object = layer->objects.begin(); object != layer->objects.end(); ++object)
            {
                                collision = object->Contains(animatedSprite.getPosition());
            }
        }
    }

    sf::Clock frameClock;

        sf::Music music;

        if(!music.openFromFile("Pallet Town Theme.ogg"))
                std::cout << "ERROR: Can't find Pallet Town Theme.ogg" << std::endl;

        music.play();
        music.setLoop(true);

    float speed = 0.045f;
    bool bNoKeyWasPressed = true;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                window.close();
                        if (event.type == sf::Event::LostFocus)
                        {
                                music.pause();
                               
                        }
                        if (event.type == sf::Event::GainedFocus)
                                music.play();
        }
               
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationUp)
            {
                 animatedSprite.setAnimation(walkingAnimationUp);
            }
            animatedSprite.play();
                        if(collision = false)
                                animatedSprite.move(0, -speed);
                        else
                                animatedSprite.move(0, 0);
            bNoKeyWasPressed = false;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationDown)
            {
                animatedSprite.setAnimation(walkingAnimationDown);
            }
            animatedSprite.play();
            animatedSprite.move(0, speed);
            bNoKeyWasPressed = false;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationRight)
            {
                animatedSprite.setAnimation(walkingAnimationRight);
            }
            animatedSprite.play();
            animatedSprite.move(speed, 0);
            bNoKeyWasPressed = false;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            if(animatedSprite.getAnimation() != &walkingAnimationLeft)
            {
                animatedSprite.setAnimation(walkingAnimationLeft);
            }
            animatedSprite.play();
            animatedSprite.move(-speed, 0);
            bNoKeyWasPressed = false;
        }

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Collision Detection Issue
« Reply #5 on: October 19, 2013, 12:34:54 am »
Oh wow it really is just an uninitialized bool?  No wonder this isn't working.

I'd throw out that code and write something that actually makes sense yourself.  I don't know anything about TMX but collision with tilemaps is never complicated.

 

anything