1
votes

I have a moving platform at constant speed and I need to update my player position when my olayer jump ont that platform. But I have problems with collision detection. Here how I try to detect collision between my player's bottom Rectangle and platform Rectangle.

    for(GameObject t2 : list){
                    if(t2 instanceof Platform){

                        platform = (Platform) t2;
                        render.rect(platform.getHitBox().getX(), 
                                    platform.getHitBox().getY(),
                                    platform.getHitBox().getWidth(),
                                    platform.getHitBox().getHeight());

                        platform.update(Gdx.graphics.getDeltaTime(), 0,
                                        player1.getHitBox().getX(), 
                                        Gdx.graphics.getWidth());

                        if(platform.getHitBox().overlaps(player1.getHitBox())
                           ||player1.getHitBox().overlaps(platform.getHitBox())){

                            System.out.println("true");

                            platformPositonX = player1.getHitBox().getX() 
                                              + platform.getUpdateSpeed();

                            player1.action2(1, platformPositonX, 
                                           (platform.getHitBox().y +
                                           platform.getHitBox().height));

                        }else{
                            System.out.println("false");
                        }
                    }
}

Here's my print screen and my result printed in concole enter image description here As you can see my collision don't work correctly. When my character is not on the platform my result are correct I get false but when my character is on the platform i got true, false, false, true, ......

2
Why are you checking for collision twice? player1.getHitBox().overlaps(platform.getHitBox() and platform.getHitBox().overlaps(player1.getHitBox() are both same and would return the same result. Overlaps means they are colliding with "each other".Zohaib Amir
Now to your problem, are you sure you are updating the platform hitbox as the platform moves? Can you post the code of your Platform class?Zohaib Amir

2 Answers

1
votes

Finally I found out what's wrong. In this line I need to substract a number

 player1.action2(1, platformPositonX,(platform.getHitBox().y + platform.getHitBox().height - 5 ));

Now all works!

0
votes

into your sample code, just enter "false" println, if not overlaping, and touched is true, I do not know what is "touched" in this case, but if you want to enter only if, ovelaping is false, your pudes provar this:

in your code says:

..// other code

    }else if(touched){
            System.out.println("false");
    }
}

change for this:

..// other code

    }else{
            System.out.println("false");
    }
}

You might also be interested to look at this:

Libgdx - Get Intersection Rectangle from Rectangle.overlap(Rectangle)