Walking uphill and downhill

Recommended Posts

I'm somewhat extending the platformer tutorial and trying to write a new game. My problem is that if I draw platform objects in the collision layer that are not completely flat but a diagonal slope for example, the character moves very slowly uphill and slides fast downhill.
Can you suggest how to implement such a movement that the character would just walk up and down with normal speed, and if he stops he is not sliding down like it's an icy surface? Just like a person would simply stop in the middle of a slope? (Given that the slope is not too steep of course).
Many thanks.

Share on other sites

I managed to make the character walk uphill with normal speed by setting a low body mass. But I still have problem with sliding down on slopes. The character is now sliding down very slowly (I guess because of the low mass) but I can't figure out how I could prevent it from sliding.

Share on other sites

58 minutes ago, PLAYERKILLERS said:

If I set friction to (1, 1) then the body stops sliding but the character can't walk at all ? Any value lower than (1, 1) makes the body slide down (although very slowly).
I think I will have to adjust the physics parameters dynamically. Detect if there's collision with a slope and add some extra force to keep the body in position. However calculating that force will not be easy.

Share on other sites

I managed to solve this. The reason why things slide down on slopes is that on every collision they're moved outside the collision bounds, then they fall down a little bit, then again collide, etc... in a loop... which is I guess OK for most of the games but I wanted it to be different. So I solved it by bypassing the default respondToCollision function call and implemented a slightly different variant:

```onCollision : function (response, other) {

// Slopes are marked "Slope" type in Tiled
if(other.type === "Slope") {
var overlap = response.overlapV;
// Move out of collision bounds, but only on the Y axis
overlapCopy = overlap.clone();
overlapCopy.x = 0;
this.pos.sub(overlapCopy);
if (overlap.x !== 0) {
this.body.vel.x = ~~(0.5 + this.body.vel.x - overlap.x) || 0;
// Removed bouncing because I don't need it
}
if (overlap.y !== 0) {
this.body.vel.y = ~~(0.5 + this.body.vel.y - overlap.y) || 0;
// cancel the falling an jumping flags if necessary
var dir = Math.sign(me.game.world.gravity.y * this.body.gravityScale) || 1;
this.body.falling = overlap.y >= dir;
this.body.jumping = overlap.y <= -dir;
}
return false;
}

return true;
}```

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.