Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » slope angle detection
- 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
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):
set [angle v] to ([atan v] of ((([y position v] of [right detector v]) - ([y position v] of [left detector v])) / (cont'd)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)
cont'd is (([x position v] of [right detector v]) - ([x position v] of [left detector v]))
set [angle v] to ([atan v] of ((([y position v] of [right detector v]) - ([y position v] of [left detector v])) / (cont'd)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):
cont'd is (([x position v] of [right detector v]) - ([x position v] of [left detector v]))
set [angle v] to (round ((angle) / [57.295]))
set [angle v] to ([atan v] of ((([y position v] of [right detector v]) - ([y position v] of [left detector v])) / (cont'd)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.
cont'd is (([x position v] of [right detector v]) - ([x position v] of [left detector v]))
set [angle v] to ((round ((angle) / [57.295])) - [90])
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
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):set [angle v] to ([atan v] of ((([y position v] of [right detector v]) - ([y position v] of [left detector v])) / (cont'd)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)
cont'd is (([x position v] of [right detector v]) - ([x position v] of [left detector v]))set [angle v] to ([atan v] of ((([y position v] of [right detector v]) - ([y position v] of [left detector v])) / (cont'd)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):
cont'd is (([x position v] of [right detector v]) - ([x position v] of [left detector v]))
set [angle v] to (round ((angle) / [57.295]))set [angle v] to ([atan v] of ((([y position v] of [right detector v]) - ([y position v] of [left detector v])) / (cont'd)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.
cont'd is (([x position v] of [right detector v]) - ([x position v] of [left detector v]))
set [angle v] to ((round ((angle) / [57.295])) - [90])
That can help me with my game too! Thank you
- Discussion Forums
- » Help with Scripts
-
» slope angle detection



