SuperTasche
SuperTasche

Reputation: 509

Cheapest way to find Vector magnitude from a given point and angle

I am trying to determine a players depth position on a plane, which defines the walkable ground in a 2D brawler game. The problem is depictured in the following drawing:

enter image description here

C represents the players current position. I need to find the magnitude of vector V. Since I am not strong on linear algebra, the one thing I can think of is: determining the intersection point P of L1 and L2, and then take the magnitude from AP. However, I get the feeling there must be an easier way to find V, since I already know the angle the vector should have, given by vector from AB.

Any input would be appreciated, since I am looking forward to step up my linear algebra game.

Edit: As it is unclear thanks to my lack of drawing skills: the geometry depicted above is a parallelogram. The vector V I am looking for is parallel to the left and right side of the parallelogram. Depth does not mean, that I am looking for the vector perpendicular to the top side, but it refers to the fake depth of a purely 2D game. The parallelogram is therefore used as a means for creating the feeling of walking along a z axis.

Upvotes: 2

Views: 347

Answers (2)

Remy
Remy

Reputation: 5163

The depth of your player (length of V) as measured from the top line in your drawing, is just the difference between A.y and C.y. This is seperate from the slant in the parralelogram, as we're just looking at depth.

example:

float v; 

Vector2 a = new Vector2(100, 100); //The point you're measuring from
Vector2 c = new Vector2(150, 150); //Your character position

v = c.y - a.y; // This is the length of V.
//In numbers: 50 = 150 - 100 

Illustrated: image not to scalegetting the "depth" of a vector This works for any coördinate in your plane.

Now if you'd want to get the length of AC is when you'd need to apply some pythagoras, which is a² + b² = c². In the example that would mean in code:

Vector2 a = new Vector2(100, 100);
Vector2 c = new Vector2(150, 150);

float ac1 = Mathf.Sqrt(Mathf.Pow(c.x - a.x, 2) + Mathf.Pow(c.y - a.y, 2));

Now that is quite a sore to have to type out every time, and looks quite scary. But Unity has you covered! There is a Vector method called Distance

float ac2 = Vector2.Distance(a, c);

Which both return 70.71068 which is the length of AC. This works because for any point c in your area you can draw a right angled triangle from a to c.

Edit as per comment: If you want your "depth" vector to be parallel with the sides of the paralellogram we can just create a triangle in the parallelogram of which we calculate the hypotenuse.

Since we want the new hypotenuse of our triangle to be parallel to the parallelogram we can use the same angle θ as point B has in your drawing (indicated by pink in mine), of which I understood you know the value.

We also know the length of the adjacent (indicated in blue) side of this new triangle, as that is the height we calculated earlier (c.y - a.y).

Using these two values we can use cosine to find the length of hypotenuse (indicated in red) of the triangle, which is equal to the vector V, in parallel with the parallelogram. the formula for that is: hypotenuse = adjacent/cos(θ)

Now if we were to put some numbers in this, and for my example I took 55 for the angle θ. It would look like this

float v = 50/(cos(55));

image not to scale

Getitng the hypotenuse of a triange using cosine

Upvotes: 1

kefren
kefren

Reputation: 1102

Let's call the lower right vertex of the parallelogram D. If the long sides of the parallelogram are horizontal, you can find magnitude of V vector by:

V.magnitude =  (c.y - a.y) / sin(BAD)

Or if you prefer:

V.magnitude = AB.magnitude * (c.y - a.y)/(b.y - a.y)

Upvotes: 1

Related Questions