Repulsion/attraction vectors

Reversing the order of subtraction transforms one behavior into another: a vector opposing becomes additive and a vector attracting pushes and brakes in turn. (gif)


Or maybe it’s about “frame of reference.” From the perspective of the attractor, the vector formula is consistent:

tempVec.setVec(to.x - from.x, to.y - from.y); // vector from -> to

The repulsion is a vector to itself. The attraction is a vector to the unmoving object. Given two points, we can create predictably pointing vectors.

Magnitude is a different thing. The subtraction sets the vector magnitude – i.e., x and y will be updated by dx and dy, respectively – but it will teleport the object in the next frame. You need to take the unit vector, multiply it by some scale, and then add it to the object’s original vector.