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

Author Topic: Collision causes problem with movement  (Read 2143 times)

0 Members and 1 Guest are viewing this topic.

MehCool

  • Newbie
  • *
  • Posts: 3
    • View Profile
Collision causes problem with movement
« on: January 05, 2015, 05:36:10 pm »
My problem is that, when I move LEFT and im not colliding with anything(except the tile that is underneath), my player slows down. That is cause this code detects collision when moving on left side whether im colliding or not:

void HandleCollision(Entity& e, std::vector<std::vector<Entity>>& entities)
{
        int sx = (e.position.x + (entitySize.x / 2)) / 16,
                sy = (e.position.y + (entitySize.y / 2)) / 16;

        int sizeY = entities.size();
        int sizeX = entities[0].size();
        int minY = getMin((sy - 6), 0);
        int minX = getMin((sx - 6), 0);
        int maxY = getMax((sy + 6), sizeY);
        int maxX = getMax((sx + 6), sizeX);

        int collision_x = 0,
                collision_y = 0;

        for (int y = minY; y < maxY; y++)
        {
                for (int x = minX; x < maxX; x++)
                {
                        Entity gotEntity = entities[y][x];
                               
                        if (gotEntity.type != Category::wall)
                                continue;

                        sf::FloatRect intersection;
                        if (GetBoundingRect(e).intersects(GetBoundingRect(gotEntity), intersection))
                        {
                                if (e.position == gotEntity.position)
                                        e.position.x += entitySize.x;

                                sf::Vector2f direction = gotEntity.position - e.position;
                                sf::Vector2f offset;

                                // X collision
                                if (abs(direction.x) > abs(direction.y)) {
                                        if (direction.x < 0)
                                        {
                                                offset.x = -1 * intersection.width;
                                                collision_x = -1;
                                        }
                                        else
                                        {
                                                offset.x = 1 * intersection.width;
                                                collision_x = 1;
                                        }
                                }

                                // Y collision
                                if (abs(direction.x) < abs(direction.y))
                                {
                                        if (direction.y < 0)
                                        {
                                                offset.y = -1 * intersection.height;
                                                collision_y = -1;
                                        }
                                        else
                                        {
                                                offset.y = 1 * intersection.height;
                                                collision_y = 1;
                                        }
                                }
                                       
                                if(e.type == Category::player && gotEntity.type == Category::wall) {
                                        e.velocity -= offset;
                                }
                        }
                }
        }
}
 
I use it like HandleCollision(Player, Tiles)

:/
« Last Edit: January 05, 2015, 05:42:10 pm by MehCool »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Collision causes problem with movement
« Reply #1 on: January 05, 2015, 07:58:37 pm »
So what's the problem? Where are you stuck? With what should we help you?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MehCool

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Collision causes problem with movement
« Reply #2 on: January 05, 2015, 10:32:36 pm »
Argh... When Im moving to left with player sprite, my code(that I posted in my first post, it handles collision) detects and handles collision even tho I am not colliding/intersecting... and everything works perfect when I move right...
So my player is 'stuttering' when I move left(even tho it moves, but 3x slower then moving on the right side)... And it moves normally on right side...

I cant explain better... :/
« Last Edit: January 05, 2015, 10:34:56 pm by MehCool »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Collision causes problem with movement
« Reply #3 on: January 05, 2015, 10:34:05 pm »
This is the sort of problem that can probably be solved easily with a debugger.  Have you tried using one?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Collision causes problem with movement
« Reply #4 on: January 05, 2015, 10:49:17 pm »
                                if (e.position == gotEntity.position)
                                        e.position.x += entitySize.x;
Not sure what this code is supposed to do but it looks like it moves the entity/player to the right.

Also, what's the point of collision_x and collision_y?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

MehCool

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Collision causes problem with movement
« Reply #5 on: January 06, 2015, 09:16:45 pm »
Oh, I removed that part of code.
I used collision_x/y just for some testing and forgot to delete it.

And here is the video of what happens:

(sorry for low quality)
As you can see, when I move right, it moves at normal speed, and when I move left, it moves slower.
Should I post movement code? Althought the problem is in that HandleCollision.
Please help...

 

anything