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

Curved slopes #52

Open colinvella opened 6 years ago

colinvella commented 6 years ago

I'd like to suggest the introduction of additional curved slopes like the ones below:

image

I understand that collision bodies for the sloped tiles are generated from polygons which are made of straight edges, but I think with some interpolation, curved slopes can also be generated programmatically.

The formula for the curved part of the 4 tiles above are:

1) y = w - c(x) 2) y = w - c(x + w) 3) y = c(2w - x) 4) y = c(w-x)

where c(x) = x^2 / 4w and w is the width of the tile (e.g. 16, 32, 64, 128 etc.) Note: I'm assuming that the Y axis points downwards, as per normal display convention.

When generating the curved slope, x would be sweeped from 0 to w by a step of 1 and y would be computed accordingly. For practical purposes, x can be incremented by a larger step, e.g. by w / 4 to reduce the number of sides.

An additional point for tile 1 and 4, or two points for 2 and 3, would need to be generated for the lower corner(s).

I can give you the formulas for the 4 tiles going down right, but as you might imagine, they are simple variants of tiles 1 to 4.

Notice how 2 and 3 have 45 degree slopes at one end, which makes it possible to combine them with straight 45 degree slopes.

Flatter versions (1/2 height) can be generated for transitioning on to straight 22.5 degree slopes as well, since their edge would end at halfway up the second tile with a tangent corresponding to the 22.5 slopes, so the second 22.5 straight slope can be placed next to it for a smooth transition.

These could be also extended to curved ceiling tiles, but they're not really that useful.

I could try extending your library myself, but I'm not sure I understand the rest of your code!

What are your thoughts on this?

colinvella commented 6 years ago

Now that I think about it, tiles 1 and 2 are concave and hence not amenable to collision detection by SAT. Also, tiles 3 and 4, albeit convex, consist of many segmented sides constituting the curve, which would give the SAT solver a lot more to do.

hexus commented 6 years ago

Yes, this wouldn't use proper SAT, but there is a solution for it.

http://www.metanetsoftware.com/technique/tutorialA.html#section2

See the "round shapes" subheading.

I didn't dive into this for the plugin because I didn't feel I was ready to at the time.

Hopefully this is something I could look into in the future, because then this would be the complete alternative solution to the deprecated Ninja Physics system.

colinvella commented 6 years ago

The method described in that tutorial is quite interesting. I was surprised how it worked with the convex curve as well. It seems to be a hybrid that applies SAT on the horizontal and vertical axes but computes a an overlap projection along the normal to the curve in a different way.

Perhaps it's because a circular arc has certain properties. I don't know if the same assumptions can be applied to a polynomial curve as in my tiles above.

Anyhow, keep up the good work! This is really a great plugin!

hexus commented 6 years ago

Thank you! I look forward to getting more time for it next year.