ScratchData LogoScratchData
Back to manan68's profile

If on edge, bounce recreated remix

MAmanan68•Created November 23, 2022
If on edge, bounce recreated remix
0
0
5 views
View on Scratch

Instructions

I have recreated the functionality of the "if on edge, bounce" block, but this time, you get to CHOOSE WHERE the edges are! I tried to make the most practical recreation possible, so if you're looking for a way to bounce your sprites off of your own walls, floors, and ceilings, this is the way to go. ◆◆◆◆◆◆ How it works ◆◆◆◆◆◆ When a sprite is bouncing off any edge, it isn't as simple as rotating it 180 degrees. That would work for straight edge approaches, but it will bounce off at an awkward angle when coming in at an angle. That's why we need to take our time to do some math. First, we break up the sprite's direction into a sine and a cosine. These are trigonometry functions that are sort of like the horizontal and vertical positions of clock hand tips. Sine is horizontal, and cosine is vertical. Next, we flip one of the values when the sprite hits the edge. In the case of the left and right edges, we flip the sine, and if it hits the top or bottom edges, we flip the cosine. Finally, we turn that back into a direction by making the sprite "look at the clock hand's tip" in a way that I can't concisely explain here. These three steps are represented as custom blocks inside the Demo sprite's code, so feel free to have a look. If you're having trouble reverse engineering anything, you can try reading trigonometry guides, or if you're confused with the above description, just understand that there's math involved in making a sprite realistically bounce off walls. To use this code as-is, though, you don't need any trigonometry knowledge, just keep the custom blocks intact and it will serve you well. ◆◆◆◆◆◆ Changelog ◆◆◆◆◆◆ November 7, 2022: Version 1.0.0 - Ready for use!

Project Details

Project ID766433841
CreatedNovember 23, 2022
Last ModifiedNovember 23, 2022
SharedNovember 23, 2022
Visibilityvisible
CommentsAllowed

Remix Information

Parent ProjectView Parent
Root ProjectView Root