Discuss Scratch

mip67
Scratcher
68 posts

slope angle detection

is it possible to detect the exact angle of a slope the player is touching? (say in a platformer-like game)

Last edited by mip67 (Aug. 6, 2022 15:37:09)

yavuz61035
Scratcher
500+ posts

slope angle detection

you could find the normal vector (that's basically the angle): in griffpatch's ball physics tutorial, there is a part dedicated to finding the vector of a slope, so maybe that will help a little?
CodeNinja329
Scratcher
1000+ posts

slope angle detection

mip67 wrote:

is it possible to detect the exact angle of a slope the player is touching? (say in a platformer-like game)
I'm going to assume this slope is a straight line. If you can find 2 points on the line (ex. with two sprites a set number apart on the x-axis that fall onto the surface of the slope), you can use slope-intercept form to calculate the slope of the line. From there, according to the internet, you take the inverse tangent of the decimal slope you just calculated. This sounds confusing, but here's a script to do this (slightly modified due to forum limitations):
setangletoatanofy positionofright detector-y positionofleft detector/cont'dcont'disx positionofright detector-x positionofleft detector
Now, this gets us the radians, so for degrees, we need to also divide by 57.295, and you also may need to round to the nearest degree, getting this: (you can do this all in one line, but it's too long to display here- also, you don't have to round if you don't mind decimal numbers)
setangletoatanofy positionofright detector-y positionofleft detector/cont'dcont'disx positionofright detector-x positionofleft detectorsetangletoroundangle/57.295
We're not done quite yet- this formula considers a left-right line to be 0 degrees, but Scratch considers it to be 90 degrees, so we have to subtract 90 from the result. Here's the final code (which can also be done in one block rather than two):
setangletoatanofy positionofright detector-y positionofleft detector/cont'dcont'disx positionofright detector-x positionofleft detectorsetangletoroundangle/57.295-90
Now, there is a quirk of continuous lines that you must take into account- they go two ways, so they can be interpreted as having degrees of a negative number or a positive number, as a degree line normally goes one way, like a clock, giving it only one degree value. Therefore, the degrees of any line will only be calculated as a value from 0 to 180, even though Scratch degrees can go from -179 to 180. Let me know if you need a way to switch to negative degrees when, say, the player is facing left- it should be fairly easy. The other quirk you need to know is that a fully vertical line has a slope of undefined, which can't be used to calculate degrees without extra code, even though it does translate to a degree value- let me know if you need help with that code, too.

Last edited by CodeNinja329 (Aug. 6, 2022 19:19:18)

mip67
Scratcher
68 posts

slope angle detection

thank you, this should work, I'll update you if it does.
Jereb-co7
Scratcher
100+ posts

slope angle detection

CodeNinja329 wrote:

mip67 wrote:

is it possible to detect the exact angle of a slope the player is touching? (say in a platformer-like game)
I'm going to assume this slope is a straight line. If you can find 2 points on the line (ex. with two sprites a set number apart on the x-axis that fall onto the surface of the slope), you can use slope-intercept form to calculate the slope of the line. From there, according to the internet, you take the inverse tangent of the decimal slope you just calculated. This sounds confusing, but here's a script to do this (slightly modified due to forum limitations):
setangletoatanofy positionofright detector-y positionofleft detector/cont'dcont'disx positionofright detector-x positionofleft detector
Now, this gets us the radians, so for degrees, we need to also divide by 57.295, and you also may need to round to the nearest degree, getting this: (you can do this all in one line, but it's too long to display here- also, you don't have to round if you don't mind decimal numbers)
setangletoatanofy positionofright detector-y positionofleft detector/cont'dcont'disx positionofright detector-x positionofleft detectorsetangletoroundangle/57.295
We're not done quite yet- this formula considers a left-right line to be 0 degrees, but Scratch considers it to be 90 degrees, so we have to subtract 90 from the result. Here's the final code (which can also be done in one block rather than two):
setangletoatanofy positionofright detector-y positionofleft detector/cont'dcont'disx positionofright detector-x positionofleft detectorsetangletoroundangle/57.295-90
Now, there is a quirk of continuous lines that you must take into account- they go two ways, so they can be interpreted as having degrees of a negative number or a positive number, as a degree line normally goes one way, like a clock, giving it only one degree value. Therefore, the degrees of any line will only be calculated as a value from 0 to 180, even though Scratch degrees can go from -179 to 180. Let me know if you need a way to switch to negative degrees when, say, the player is facing left- it should be fairly easy. The other quirk you need to know is that a fully vertical line has a slope of undefined, which can't be used to calculate degrees without extra code, even though it does translate to a degree value- let me know if you need help with that code, too.

That can help me with my game too! Thank you

Powered by DjangoBB