hexus / phaser-arcade-slopes

:triangular_ruler: A Phaser CE plugin that brings sloped tile collision handling to the Arcade Physics engine
MIT License
126 stars 16 forks source link

Question: How to prevent slipping or sliding on slopes #51

Closed colinvella closed 6 years ago

colinvella commented 6 years ago

Note: Question / answer session transcribed on to an issue to help out others with a similar problem.

============================================

Hi,

What parameters do I need to set to prevent the player sprite from sliding down a slope. In my code, even the slightest of slops causes the player to slide down to flat ground. In your demo code, the player sprite slides of a bit but eventually stops, even on 45 degree slopes. I have tried to use the same physics params as per your demo but I still get the slipping effect.

What should I do to prevent slipping? There may be a answer but I'm new to using Phaser and my last game programming stint was with XNA.

Thanks & Regards Colin

============================================

Hey Colin,

The solution in the demo is to use X-axis drag on the body, as well as the "minimum Y axis" feature that's mentioned in here in the readme.

This feature separates collision bodies on the Y axis only where appropriate, so that the sliding does not occur.

The sliding is actually caused by the separating axis theorem, which looks for the shortest possible way for one shape to no longer intersect another. I looked into a solution for this for early versions of the plugin and settled on this "minimum Y axis" approach that I found somewhere buried in a forum.

A second solution that I've been using in a game I'm developing is to simply turn off gravity for the player body whenever it is touching anything beneath itself (player.body.touching.down).

I hope this helps, please feel free to get in touch if you have any more trouble with this. It pestered me for a while and it was important, for me, that the plugin could at least attempt to help with this problem.

Cheers, Chris

============================================

Hi Chris,

Thanks for your response. I had already set the preferY for SAT testing and also had body drag set as per the demo's setting. What eventually helped was setting the friction coefficients to 0.2 on the sprite's body. The 0.2 setting was just right to have the player slow down to a stop on 0.5 tangent slopes but still slide down gradually on 1.0 tangent (45 degree) slopes.

I also had a problem with the player loosing momentum when walking on the slopes, but I solved this using high acceleration values (3000) coupled with a relatively ow max velocity (250) on the horizontal axis. This resulted in a responsive sprite that can climb slopes steadily with only a slight slowdown.

Once again, thanks a lot! You've been very helpful.

Cheers! :) Colin

============================================

Hey Colin,

I'm glad to hear it! That's very similar to what I ended up doing in the demo too, tweaking values until I found the behaviour I needed and landing around there.

Balancing friction, drag, acceleration and max velocity against the gravity applied to the character is the way to go for quite a natural look and feel.

I'm still looking into the best ways for the plugin to provide no struggle when ascending slopes, or explicitly controlling said struggle, but for my current project I've simply been adjusting gravity with quite some success. And, to be fair, such collisions could end up very game specific, so for now I've left it to the user until I can think up something magical (if ever).

Cheers, Chris

hexus commented 6 years ago

Thanks Colin :)