# How does the code in the if on edge, bounce block work?

I know it's not simply just turn 180 degrees, and I can't quite wrap my head around the javascript
Could someone please show me how it works in Snap! blocks? (The bounce part, not the if on edge part.)

Look in src/objects.js.

I already know what the code is, but

Also, github is blocked by my school admin.

Well here's how I would make the block:

But looking at the snippet of code provided by programmer_user, it seems like it calculates the dirX and dirY of the sprite using sin and cos. So it probably flips the dirN based on which edge it hit, then recalculates the direction using atan.

So like this?

That looks like a no-op; the atan2 undoes the sin and cos. If there were a stage in the middle that did the reflection, then maybe.

I use this:

I use it to bounce off sprites and it works for me.

All this JS stuff and idk how to do JS so I am confused if your talking about the if on edge, bounce. Or something else cause you just have to loop that block and it prevents certain sprites from going out of view.

Thanks! I was in fact trying to write a script to bounce off a sprite.

I was asking how the code works internally, not how to use the block

Wait, it should work only if the sprite's walls are horizontal or vertical.

What direction should the blue sprite turn to?

The stage walls are horizontal and vertical, so that code will work for walls.

L8zy hitboxes

How to bounce off an arbitrary-slope line:

LogoWorks, page 90 of this PDF, page 347 of the whole book.

What you need is this:

As long as you know the direction of the line you can bounce off of it.

If you don't, find it out. (you can also take out the block out and use "bounce direction" as an input instead of "line direction")

he said:

Yeah but I'm a teacher, and as you know, teachers never answer questions directly. :~)

Snap forums is a classroom now

just translate it to snap code.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.