SFML community forums

Help => General => Topic started by: Me-Myself-And-I on July 12, 2023, 07:15:26 pm

Title: A problem with platformer side collisions.
Post by: Me-Myself-And-I on July 12, 2023, 07:15:26 pm
Hello.I'm having some trouble getting the collisions correct for a platformer.I have tried many different ways but have had none success with this.This particular attempt causes the player,"shape",to move far to the left of the blocks once it comes in contact.I'm sure that the hitboxes and the sides are correct.The tiles are all placed in the world relative to their array index. My intended method is to move the player to the top of the tile when the top is collided then to check for side collisions and reverse the effect of setting at the top so that the player doesn't teleport to the top when he hits the side of another tile.Does anyone see what mistakes there are?Is this method even plausible.If there is a simpler method please mention how to do so.Thanks!



                update(RenderWindow &window)
                {
               
                        //update collision sides of player
                        left=shape.getPosition().x;
                        right=shape.getPosition().x+hitbox.x;
                        top=shape.getPosition().y;
                        bottom=shape.getPosition().y+hitbox.y;
                        Vector2f over;
                        bool exitloop=false;
                        bool sidehit=false;
                        for(int x=0;x<gmap.mapsize.x;x++)
                        {
                               
                                if(exitloop==true)
                                {
                                        break;
                                }
                               
                               
                               
                               
                                for(int y=0;y<gmap.mapsize.y;y++)
                                {
                       
                                        if(exitloop==true)
                                                break;
                                       

                               
                       
                       
                                                               
                                        if(gmap.tile[x][y].active==true)
                                        {
                                               
                                                if(gmap.tile[x][y].right<left||gmap.tile[x][y].left>right||gmap.tile[x][y].bottom<top||gmap.tile[x][y].top>bottom)
                                                {
                                               
                                                        velocity.y+=Gravity;
                                                }
                                                else
                                                {
                       
                                                       
                                                        if(bottom>=gmap.tile[x][y].top-velocity.y)
                                                        {                                                              

                                                                shape.setPosition(shape.getPosition().x,gmap.tile[x][y].top);
                                                                velocity.y=0;
                                                                if(Keyboard::isKeyPressed(Keyboard::Up))
                                                                {
                                                                        velocity.y=-2;
                                                                }
                                                        }
                                                       
                                               
                                                        exitloop=true;
                                                       
                                                }
                                        }
                                       
                                        if(gmap.tile[x+1][y-1].active==true)
                                        {
                                               
                                                if(right>gmap.tile[x+1][y-1].left&&bottom>gmap.tile[x+1][y-1].top)
                                                {
                                                                velocity.x=0;
                                               
                                                                shape.setPosition(shape.getPosition().x-1,shape.getPosition().y+gmap.tile[x][y].hitbox.y);//set x to left and set bottom to original standingfloor to make up for the change made by top collision
                                                       
                                                }
                                       
                                                       
                                        }              
                       

                       
                       
                                }
                       
                        }
                       
                       
                       
                       
                       
                       
                       
                        if(Keyboard::isKeyPressed(Keyboard::Right))
                        {
                                velocity.x=2;
                        }
                        if(Keyboard::isKeyPressed(Keyboard::Left))
                        {
                                velocity.x=-2;
                        }      
                        else if(!Keyboard::isKeyPressed(Keyboard::Right))
                        {
                                if(velocity.x>0.05)
                                        velocity.x-=0.05;
                                       
                                if(velocity.x<-0.05)
                                        velocity.x+=0.05;
                        }
                       
                        shape.setPosition(shape.getPosition().x+velocity.x,shape.getPosition().y+velocity.y);
                        window.draw(shape);
                       
                }
 
Title: Re: A problem with platformer side collisions.
Post by: fallahn on July 13, 2023, 10:35:00 am
You need to calculate from which direction the collision occurs - ie the 'collision normal', then correct the position along it. I wrote a post about it here:
https://trederia.blogspot.com/2016/02/2d-physics-101-pong.html

 ;D
Title: Re: A problem with platformer side collisions.
Post by: Me-Myself-And-I on July 14, 2023, 05:20:54 am
Thankyou!