britzl / platypus

Defold platformer engine
MIT License
53 stars 10 forks source link

High gravity/jump launch sends player through platforms #13

Closed stefankendall closed 5 years ago

stefankendall commented 5 years ago

When setting a high launch speed or gravity, a game object is able to move through a platform going up (jumping), or through a platform going down (falling).

https://github.com/stefankendall/platform

https://github.com/stefankendall/platform/blob/master/high_gravity.mp4?raw=true

britzl commented 5 years ago

Thank you for reporting a problem. I took a look at your project and was able to reproduce the problem. This is what I did to debug:

Screenshot 2019-08-02 at 11 26 05

    this.platypus = platypus.create({
        collisions = {
            separation = platypus.SEPARATION_SHAPES,
            groups = {
                [hash("ground")] = platypus.DIR_ALL,
            },
            left = 7, right = 7, top = 10, bottom = 10,
        },
        max_velocity = 2000,
        gravity = kGravity,
        allow_double_jump = false,
        allow_wall_jump = false,
        debug = true,
    })

Results in:

Screenshot 2019-08-02 at 11 26 31

Screenshot 2019-08-02 at 11 30 00

The size corresponds to left, right, top and bottom values passed into the config. The values should match the size of the collision shape. This is especially important since you have set collision resolution to happen using the collision shape (you could also resolve collisions based just on the result of the ray casts).

            left = 54, right = 54, top = 75, bottom = 75,
Screenshot 2019-08-02 at 11 33 54

This is better but obviously not right. It highlights the fact that I have assumed that the collision shape and sprites are centred around 0x0 while in your case the sprite is offset. If we change the left, right, top, bottom values to match we get this:

            left = 54, right = 54, top = 108, bottom = 1,

But it doesn't work. The player falls through the ground. This is because the ray casts pointing down, down left and down right aren't angled properly and the one pointing straight down is almost 0 in length. Increasing bottom gives a better result:

            left = 54, right = 54, top = 108, bottom = 10,
Screenshot 2019-08-02 at 11 39 24

But the player is hovering above the ground. Not good. There are two alternatives now:

  1. I fix this by adding a new config value called offset (vector3) which is used when doing ray casts. This value would offset the raycast start position so that it is possible to have sprites and shapes offset from 0x0 on the game object.
  2. Move your sprite and shape and set new values for ray casts to solve the problem:

Screenshot 2019-08-02 at 11 43 50

Screenshot 2019-08-02 at 11 44 07

left = 54, right = 54, top = 75, bottom = 75,
britzl commented 5 years ago

Released 3.2.1 with this new config option.

https://github.com/britzl/platypus/releases/tag/3.2.1

See grottoescape game in project for example of use.