Jump to content

Help w/ Polygon Platformer that Allows Jumping Down


UrsaMajor82
 Share

Recommended Posts

I'm experimenting with Phaser 3 and Matter JS, trying to build a polygon based platformer. All the platforms are irregular shaped polygons (tree branches), see below.

image.png.cf3bcb44d01c3f72995880ca0c407d7f.png

What are some recommended ways of allowing the player (the gray trapezoid) to jump up and down to each branch?

The best approach I came up with (which is buggy, described further down):

  • Collision categories for the tree branches and player
  • A collision mask on the tree branches ('default' and 'player')
  • Rectangle sensors on the top and bottom of the player (attached via 0 length, 0 stiffness constraints)
  • Sensors use the default collision category, so they are never ignored
  • When the player jumps up and a sensor collides with a tree branch the player is removed from that tree branch's collision mask
  • When the player jumps down, they are immediately removed from the collision mask of the tree branch they are currently on
  • The tree branch masks are reset (player re-added) when the player is falling and collides

Here's a code snippet from the Player class:

public jump() {
    if (this.isOnBranch) {
        this.scene.matter.setVelocityY(this.body, -10);
        this.isOnBranch = false;
    }
}

public jumpDown() {
    if (this.isOnBranch) {
        this.currentBranch.ignorePlayer();
        this.isOnBranch = false;
    }
}

private handleCollision(pair) {
    this.prevBranch = this.currentBranch;

    if ('treeBranchObj' in pair.bodyA.plugin) {
        this.currentBranch = pair.bodyA.plugin.treeBranchObj;
    }

    if (this.isFalling()) {
        this.prevBranch?.acknowledgePlayer();
        this.isOnBranch = 'treeBranchObj' in pair.bodyA.plugin;
    }

    if (this.isRising() && (this.prevBranch != this.currentBranch)) {
        this.currentBranch?.ignorePlayer();
    }
}

private isRising(): boolean {
    return this.body.velocity.y < 0;
}

private isFalling(): boolean {
    return this.body.velocity.y > 0;
}

"This" refers to the player. The acknowledge/ignorePlayer() methods add/remove the player from the tree branch's collision mask.

Here's another screenshot with Matter's debug enabled. You can see the rigid bodies and sensors.

image.png.effa694775c318c47d06b6b6af88d539.png

I'm running into the following bugs:

  • Quickly jumping down (tapping down arrow) causes the player to move into the tree branch and then bounce back on top (doesn't jump down through the branch)
  • Jumping up without moving left / right creates a shorter jump height (seems like the constrained sensors are weighing the player down)
  • Sometimes jumping up doesn't move the player through the branch, it collides and bounces back down (seems like multiple collides happen causing previous and current branches to be the same)
  • Sometimes the player will move through a branch by moving left / right without any jumping (seems like using the y velocity only isn't ideal)

Feedback appreciated :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...