Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Smooth following rotation
- GWGamingYT
-
13 posts
Smooth following rotation
I want to make an object that will smoothly follow the exact rotation of the object ahead of it, rather than just statically clipping to the same rotation as the object ahead. Imagine an enemy in a game that moves slowly toward the player, however it is made up of multiple segments, and each of those segments smoothly rotate along with the head of the enemy, rather than statically rotating with it making it look like a big long stick, and instead it moves in a more string like fashion. I've tried multiple different ideas but none seem to work well.
Last edited by GWGamingYT (April 29, 2021 09:35:49)
- deck26
-
1000+ posts
Smooth following rotation
Just an idea, haven't tested it.
What if you make each object point in the average of their current direction and the new direction of the object one position ahead?
What if you make each object point in the average of their current direction and the new direction of the object one position ahead?
- GWGamingYT
-
13 posts
Smooth following rotation
Just an idea, haven't tested it.
What if you make each object point in the average of their current direction and the new direction of the object one position ahead?
This is close, it makes the rotation slightly smoother and only a bit less static, however it occasionally causes the segments to jerk for some reason.
- deck26
-
1000+ posts
Smooth following rotation
Can you share the project?Just an idea, haven't tested it.
What if you make each object point in the average of their current direction and the new direction of the object one position ahead?
This is close, it makes the rotation slightly smoother and only a bit less static, however it occasionally causes the segments to jerk for some reason.
- GWGamingYT
-
13 posts
Smooth following rotation
Can you share the project?Just an idea, haven't tested it.
What if you make each object point in the average of their current direction and the new direction of the object one position ahead?
This is close, it makes the rotation slightly smoother and only a bit less static, however it occasionally causes the segments to jerk for some reason.
I think I'll make a copy of the project for an example, it should be shared shortly-
- deck26
-
1000+ posts
Smooth following rotation
If the segments are meant to be joined together changing the direction is going to end up with the links breaking up. With short segments you might get away with it briefly but my original idea is really not going to work unless you make sure the movement is also handled correctly.
If you do this in forever loops you're not necessarily controlling things in the right order. Each clone can only be moved when the previous one has moved unless you work out all the new positions and then tell them all to move at the same time which may be possible with a custom block and may even be the best option.
Otherwise you need to move the head and then work out the rotation and position of each segment in turn using the new data for the segment one position ahead in the chain. That will depend on the change in direction and the change in position. My original idea should still work but I'd probably go for each segment having its centre at the head end (which I think you have) so it can be told to go to the position passed from the segment ahead. But it then has to work out the position for the next segment which can be done using trigonometry from the x,y position and the current direction. The next segment can only move when that is calculated so they stay connected.
That should result in the segments moving less than the head as it turns so should be OK but you do also need to manage what happens when any of them touch the edge. Remember Scratch tends to prefer objects not to go completely off screen although you can work round that (I have a project that does it).
If you do this in forever loops you're not necessarily controlling things in the right order. Each clone can only be moved when the previous one has moved unless you work out all the new positions and then tell them all to move at the same time which may be possible with a custom block and may even be the best option.
Otherwise you need to move the head and then work out the rotation and position of each segment in turn using the new data for the segment one position ahead in the chain. That will depend on the change in direction and the change in position. My original idea should still work but I'd probably go for each segment having its centre at the head end (which I think you have) so it can be told to go to the position passed from the segment ahead. But it then has to work out the position for the next segment which can be done using trigonometry from the x,y position and the current direction. The next segment can only move when that is calculated so they stay connected.
That should result in the segments moving less than the head as it turns so should be OK but you do also need to manage what happens when any of them touch the edge. Remember Scratch tends to prefer objects not to go completely off screen although you can work round that (I have a project that does it).
- deck26
-
1000+ posts
Smooth following rotation
I'm going to have a go at something myself but have other things I need to be doing.
- Discussion Forums
- » Help with Scripts
-
» Smooth following rotation