neils Posted March 3, 2021 Share Posted March 3, 2021 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. Quote Link to comment Share on other sites More sharing options...
neils Posted March 10, 2021 Author Share Posted March 10, 2021 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. Quote Link to comment Share on other sites More sharing options...
PLAYERKILLERS Posted March 12, 2021 Share Posted March 12, 2021 Try adjusting the friction on your body. Quote Link to comment Share on other sites More sharing options...
neils Posted March 12, 2021 Author Share Posted March 12, 2021 58 minutes ago, PLAYERKILLERS said: Try adjusting the friction on your body. 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. Quote Link to comment Share on other sites More sharing options...
neils Posted March 15, 2021 Author Share Posted March 15, 2021 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); // adjust velocity 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; } PLAYERKILLERS 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.