Aiming laser that redirects when it hits an object?

Hi guys.

I’m creating an aiming system that displays a laser emitting from the player mesh that shows the projected direction a projectile would take show it hit an object and bounce off it. I have created a beam particle who’s target will become any object it hits, however I don’t want it to end there and instead reflect off in the direction the projectile would bounce. I believe this could potentially be achieved through debug lines, splines and/or beam particle tangents however I don’t have much experience with any of the above. Bare in mind I’m creating a networked multiplayer game so the less taxing it is the better, thanks guys :slight_smile:

TLDR: Want to create aiming laser that bounces off objects. Think of how grenades are thrown in Gears of War.

95381-download.jpg

This is what I currently have:

I’m not sue if I got this right but can’t you just spawn another actor of the beam class where your previous beam hits the wall?.

Potentially, however my only issue is performance as it’s a networked multiplayer game that has quite a few particles and intensive shaders to begin with, so I’m trying to improve performance as much as possible.

Would this display a bounce effect so the line trace pictured would hit a wall and bounce as though it were a physics object and not just travel though/stop at the wall? Baring in mind I’m displaying the aim laser thing - the line trace in your picture - using a particle beam so it needs an end point and start point so wouldn’t this just be useless for me as it would just produce the same effect as I already have? Thanks :slight_smile:

sorry, that was an old image from a project i made a long time ago, to answer a similar question on here, but i didn’t realize it doesn’t show the bounce part. i don’t have that project anymore, and i guess i implemented bouncing after making that image, and i should have looked more carefully before posting that answer.

the idea is that you can use something similar to make a list of locations, and then you can loop through that to set points on your particle beams. the bounce part would be added right after the line trace, if it hits something, you can reflect the vector based on the surface normal of the hit with a formula like this:

ReflectionVector = Direction - 2 * (Direction Dot SurfaceNormal) * SurfaceNormal

you just use that to set the new velocity whenever it hits something. maybe ill throw together a working example, instead of posting some old half finished image from years ago.

The formula in Scott’s answer is a very efficient way to re-direct a projectile hitting a surface in a new direction, that part is true. But there’s a lot of other stuff that’s got to happen in the “you just need to make another beam” part of his answer that isnt explained and that formula isnt in blueprint format either.

So let’s expand a bit. In order to maintain a total combined beam length of 1000 units per your blueprint, you’ll need a short list of things to go get. You’ll need:

  • A “new start point” and “new end point” to construct beam #2.
  • The “old start point” and the “impact point” of beam #1.

Using his formula, let’s turn the velocity on the left side into “new direction” and the velocities on the right side into “old direction”. Using the impact point as “new start point”, we can use the new direction to figure out the “new end point” if we also can figure out how much beam is left over. [Note: You should probably test that there is beam length left - meaning the impact happens within 1000 units, otherwise skip all this calculation stuff].

https://s31.postimg.org/53g8lo1rf/Junk.jpg

Starting with the old stuff, “old start point” is the tip of your gun. Use a socket on your gun or whatever was fed into the start point for beam #1 here. (Not shown in image). The “old direction” is the direction beam 1 was shot in - again you can use a forward vector on the projectile or gun socket to feed there before the trace. You could also subtract the Trace End from Trace Start too. Just make sure you Normalize that before feeding it to that Dot product node so the pointing direction is a length of 1.

Right, so onto the new stuff. “New start point” is easy - that’s the impact point included in the Hit result. “New end point” is a little more complicated. We gotta know which way that beam is going to go, and how much beam is left to extend in that direction, yes?

Let’s do the length first because that’s based on old things we already know. The Vector Length from your gun to the impact point tells you how much was used in Beam #1. Subtract that from 1000 and you have your “new length” of Beam #2. This is also a handy point to do a branch test because if that length is negative, there’s no Beam #2 to be made.

To get “new direction”, let’s re-write Scott’s formula as: NewDir = OldDir - 2 * ( OldDir dot ImpactNormal ) * ImpactNormal. Note that for a line trace, normal and impact normal give the same thing.

Last step, now you have a unit vector pointing down the path the beam will reflect in, you just need to multiply that by whatever beam is left over and add that result to the impact point in order to determine “new end point”. I didn’t actually test the BP in the picture so might have some things mixed up there…

yeah, i was gonna expand my answer with a blueprint example, but i got lazy. i have made a similar laser aiming system before, but i just used debug lines, instead of beams, but i don’t have that project on my computer anymore, so i couldn’t find an image of the finished blueprint.

since his image showed he could make beams, and i have no idea what kind of physics his projectiles will use, i just answered the unsolved part of his question, providing the math for bouncing off walls.

Velocity = Velocity - 2 * ( Velocity Dot SurfaceNormal ) * SurfaceNormal

i suggest using more than 2 beams, and making an array of locations, where each beam is made out of 2 of those locations. i would calculate this array by looping through some projectile physics math, to calculate a line trace, to detect walls for bouncing. every physics iteration, i would add another point, and every hit detection i would add an extra point, and change the velocity in the physics math using that reflection vector formula.

its not just hitting a wall that might change the direction, gravity on a projectile will make an arc, which should be made out of many points. each point in the list should be the end of one beam and the beginning of another, and this point data should be calculated for the whole laser in a separate step before updating the visuals for the beams.

this is kinda an expensive thing to make in blueprints, if you can calculate the list of points in C++, it will be far more efficient. then in blueprint, you can set dress those points with whatever you want, using beam particles, or scaled instance static meshes, or project the locations to render icons on the hud, etc… but this kind of code really belongs in c++, probably in a custom projectile movement component, or in a C++ blueprint function library.

Cheers guys, I’ll test the stuff out as soon as I get to my computer. In regards to physics. They are restricted to movement along the xy plane and are not effected by gravity.

Thanks guys, it has nearly worked. It calculates the length to perfection, it however doesn’t give the correct rotation and just makes it face upward/forward - depending on perspective, any idea why? Here’s a link to what is currently happening:

This is the blueprint: