- Home /
Predicting projectile path after bouncing off Physics Material 2D
I have a projection line for my projectiles. The projection line is supposed to show where my projectiles will go even after bouncing off of colliders that have a physics material 2D.
I calculate the reflection from the incoming velocity and multiply it by the material's bounciness to get my bounce velocity.
The line works as expected as long as my material has a bounciness of 1. If I set the bounciness to anything between 0 and 1, the projection line "undershoots" the projectile. It seems that the physics material is adding a little more to the bounce.
Are you taking into account the bounceCombine variable of the physicsmaterial? Whatever it's set to, you'll also need to calculate to get the correct value.
I'm using the 2D Physics $$anonymous$$aterial which is more limited than the normal Physics$$anonymous$$aterial (no bounceCombine). I read somewhere that the physicsmaterial2d might use the multiply bounceCombine though.
$$anonymous$$y projectile's Physics $$anonymous$$aterial has 0 bounciness and 0 friction and only my platforms vary their bounciness. If my platform has a bounciness of 1 everything works correctly. It's when I changed the platform bounciness to a lower value (0 to 1) that my projectile line will incorrectly predict the projectile's path (undershooting it).
Hmmm, yeah, the docs don't really specify. But if the projectile's bounciness is always zero, then it must be maximum, since everything else would return at most 0.5.
Box2d basically used "$$anonymous$$aximum" as combine mode. $$anonymous$$ultiply wouldn't make any sense for bounce, it only makes sense for friction.
For more information on how Box2d handles friction and bounce (restitution), see the Box2d manual chapter "Fixtures" and scroll down one page to "Friction" and "Restitution".
Answer by Bunny83 · May 05, 2017 at 12:49 PM
Well, i don't know how Box2d actually takes the bounce (or "restitution" as it's called internally) into account, but i'm pretty sure that you must only multiply the reflected part by the bounce value and not the whole velocity. Imagine an almost vertical wall. If the wall as well as the ball has a bounce value of "0", no bounce should happen at all. If the ball got shoot downwards at the wall, you don't want to bounce off the wall, but you want to maintain the downward motion. If you multiply the whole velocity by "0" it would make the object to completely stop, no matter at which angle you hit the wall.
So you need to split the velocity into the normal part and the tangent part. The normal part is reflected (inversed) and multiplied by the bounce value. The tangent part should stay the same.
So you should first project the velocity onto the normal to get the "normal part". Then you can subtract the normal part from the original velocity to get the tangent part. Then just inverse the normal and multiply by bounce and finally combine the two together.
Also keep in mind that Box2d uses the larger bounce value of the two materials involved.
This should work:
Vector3 n = Vector3.Project(vIn, hitNormal);
Vector3 t = vIn - n;
return t - n * GetBounciness();
This explanation helped fix some other physics problems I was facing . Exactly what I was looking for. Thank you.
Your answer
Follow this Question
Related Questions
Shoot Projectile on the direction of where the gun is facing 1 Answer
PhysicsMaterial2D and bounciness: Getting weird angles when hitting box collider edges 2 Answers
the projectile reflects 1 time only when it hits something, pls help! 1 Answer
vector2.zero dont work in my code. 2 Answers
PhysicsMaterial2D bounces back in a strange angle when increasing bounciness 1 Answer