1
\$\begingroup\$

So the problem i am working with here is about rays piercing objects. Basically, a ray is created, it hits a piece of armor, and then some math happens, if it is able to pierce the armor, then I need it to create a ray just past it, moving in the same direction. Ive already figured out the math for if it can pierce the armor, I think i can figure out the direction part, i just need to know how to offset the position. I dont think i can just add an x or y constant variable for this reason: grey = armor, red = damageable component The green is the initial shot, and the blue is the second ray that needs to be created. If its hitting like this, its pretty easy, you would just offset the starting position of the ray by a little bit on the x axis. But it might not always hit like that. What if it hits like one of these: same thing I accidentally inverted the colors of the rays but my point still stands.

I cant just add an x value to the starting point of both of those secondary rays. For one, it would work alright, but the other, that would put it back outside of the armor and just hit it twice. And this project is in 3d, so there's a z coordinate to deal with as well. What i would need to do is add a value to the origin of the secondary ray that is a few units forward of the first ray's eventual impact point. And that is what I can't figure out how to do.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

What's industry standard is to take the surface normal vector of the surface that you've hit/intersected, and add a small bit of that vector to the starting pos of the next ray. How small is a complete can of worms into robustness of numerical methods and computational geometry that I'm not qualified to open, but something like 0.0001*normvec should do it. Of course that assumes your geometry is much larger than 0.0001.

\$\endgroup\$
4
  • \$\begingroup\$ It took me some time to figure out how to take the surface normal vector, but i got it figured out eventually. It seems to work just fine now. \$\endgroup\$
    – Chuck
    Jul 8 at 15:52
  • \$\begingroup\$ Yes, you take 2 edges of your face and take the cross product to get the normal vector. For a triangle made of vertices A B C this would look like to_unit_vector(cross(A-B, C-B)) \$\endgroup\$
    – AnnoyinC
    Jul 8 at 16:35
  • \$\begingroup\$ well, thats not what i did. i just used this line of code: if(Physics.Raycast(projectileHitPoint + (new Vector3(hitInfo.normal.x, hitInfo.normal.y, hitInfo.normal.z)*0.001f), projectileSpawnPoint.forward, out secondaryProjectile, 100f)) ProjectileHitPoint is just where the last ray hit. This way works just fine to me \$\endgroup\$
    – Chuck
    Jul 8 at 16:53
  • \$\begingroup\$ Right, if you're using an engine or library which already handles this for you do that :) \$\endgroup\$
    – AnnoyinC
    Jul 8 at 18:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .