Screen Saver math - TurboWarp - 2


Here I need to basically flip the sign, that if you are at negative, go to positive, and if you are at positive, go to negative. There is a shortcut for this in math which is known as multiplicative inversion. Remember when negatives and positives are multiplied, the ones of opposite signs result in a negative, and a negative is a number less than zero. So two multiplicative inverses make negative number, x1 * x2 < 0 And here we have our way of check! This is how I used it:

Here I am checking if target is inverse to current position or not, hence, is in the opposite triangle or not. This check prevents the sprite from getting tied down to one corner.

And here my function is complete. It is intended to be invoked whenever the sprite hits the edge, and it basically sends the destination point in 2 variables.

First it picks a random destination, then it keeps checking for whether it is outside the box and in opposite side to current side, and keeps regenerating a random point until it finds a valid one.

Now we move the sprite - I used glide for certain seconds to destination at first. But because the destination is outside the box, and I want the sprite to move within the box and bounce from edges, I need to intercept it at edge. And this is officially where Scratch capabilities end, or so it seems. I had to use More Motion extension in TurboWarp. This extension has a block for moving a number of steps towards a destination. This is perfect for this scenario, as I can move incremental steps now (bonus: it has constant speed!) and check every now and then when its at edge.

Now there is similar math in how to check for whether sprite is at edge. This is same to how we are checking how destination is outside of edge. I have applied same check here:

Basically it moves 3 steps at every frame and then checks whether it has crossed the border yet. 3 are small so its not actually visible that the sprite has crossed the border a little bit, not just bumped into it.

I have also noticed this is the second time I could have done better with a do..loop until instead of repeat until.

Now this is just code for moving to next destination, after this it will set destination again and start again:

Get Screen Saver

Leave a comment

Log in with itch.io to leave a comment.