# Need Help Making Projectiles Ricochet for a Game

Alright, I have an idea for a 2D top-down game that involves the player firing projectiles that can ricochet off of walls. But the walls will be sprites. I tried this block:

But that seems to only work for making it ricochet in the vertical direction.

So, how can I make the projectile's direction change to reflect off of both vertical and horizontal walls/sprites, especially having it communicate with the wall it's reflecting off of so that it knows which side it's hitting?

To keep it concise, I want an algorithm that works like the "if on edge, bounce" block that's built in to Snap*!*, but it works on things other than the end of the screen. Think of it like the Wii Play "Tanks!" game, if you will (and you know what that is).

Thanks!

Just letting you know, it is pretty complicated, which is why it's not built into snap. Don't worry though, I want to figure this out too. I did find this Bouncing Algorithm | 101 Computing which may help a little. Although, I am thinking of much simpler way. I'll see if I can make it, then share it (I just don't know how to explain it)

edit: can you give me the map you want? I just can't decide how to make one.

If it is vertical, horizontal, or circular, these will work. (both sprites must be circles for that option)

huh, I didn't think of that.

works like a charm Snap! Build Your Own Blocks

@earthrulerr

It works, but touching color is slow, and it sometimes gets stuck on corners. I don't have time now, but when I make it I would use one sprite for horizontal, one for vertical, and one sprite and clones for corners.

Something like this? Snap! Build Your Own Blocks (yes it's the same project, I just didn't want to duplicate it)

You can also do if touching sprite.

I think you should read replies, because I did that (after touching color).

Maybe you could do something like this

where Sprite(2) is whatever you're richochet-ing off of.

Project this is used in.

When a light beam bounces off a mirror (specular reflection), the rule is that the angle of incidence equals the angle of reflection. The same is true for a hard object bouncing off another hard object. The angles are measured relative to a line perpendicular to the mirror. (It could also be done relative to the mirror itself, if the mirror is flat, but the perpendicular is well defined even if the mirror is curvy.)

So your sprites have to know the slope of their perpendiculars. In Snap! this turns out to be the usual thing to measure, since mostly-vertical shapes such as people, animals, and buildings start out facing to the right, which is perpendicular to the vertical shape. If you have only horizontal and vertical lines, you can use the special case formulas given above; if you also have diagonal lines, each sprite has to know its DIRECTION, which it does. Then the new DIRECTION of the bouncing sprite is

This is very useful! Now I just need to figure out a way for this to target the one it's touching.

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