UrsaMajor82 Posted October 1, 2020 Share Posted October 1, 2020 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. 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. 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 More sharing options...
Recommended Posts