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 - MehCool

Pages: [1]
1
General / Re: Collision causes problem with movement
« 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...

2
General / Re: Collision causes problem with movement
« 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... :/

3
General / 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)

:/

Pages: [1]
anything