I have been having some trouble reversing my vectors in my game when a collision occurs what I need to do is flip the value to the inverse of the current value each time a marble hits an object. The vectors are directions that I need to reverse to make the bounce of look realistic.
Share
So what you’re talking about is a vector in the mathematical sense, rather than the container. Let’s still assume that you’ve implemented it with an
std::vector<fieldT>where e.g.typedef double fieldT. You seem to want to have the direction reflected by some obstacle. It’s easy when the “mirror” lies in direction of one of your basis vector, then you only need to negate that component – a “mirror” in the xy-plane (in 3D) would doi.e. flip the z-component, in the yz-plane it would be
It gets more complicated if the reflections occur in arbitrary direction. One possibility is to determine the speed component in direction of the plane – that’s done by calculating the scalar product between the speed vector and the normal vector of the plane – and then substracting twice the normal vector, weighted with that factor.
A more general approach would be modelling the reflection by matrix application: a reflection is essentially application of an orthogonal linear mapping.