Search code examples
mathluagarrys-mod

Garry's mod lua, moving a vector closer


In garry's mod we have a vector metatable: Vector
This is more a kind of a mathematical question.
I have 2 vectors: The players position (1) and one fixed on the map (2).
I want to draw text on the fixed position but because it will be inside things i can't. So how can i move vector 2 towards the player (1). I can make use of the Angle type: Angle. I can just rescale the text.
Thanks in advance!


Solution

  • I guess what you are talking is about points, rather than vectors in the traditional sense. Let's call the point where the player is V1, and the fixed point V2, then the vector representing the distance from the fixed point to the player, D, is:

    D = V1 - V2

    Now, we move V2 to a new position closer to the player by, say some fraction c:

    V2 = V2 + c * D

    So if you wanted to move the position of the text a tenth of the way closer to the player, you would set c= 1/10.

    For example, using your libraries we can just directly modify the fixed point vector (2):

    d = v1
    d:Sub(v2)
    d:Mul(0.1)
    v2:Add(d)
    

    The above code will move the fixed point, v2, a tenth of the way closer. If you can check whether the text is obstructed by something or not, you can move it step by step until it is unobstructed.