I’m working on developing a game, and I need to solve a bit of an interesting equation/scenario for this one mechanic involved in the game. I already have a solution in mind, but I’m not 100% confident that I haven’t made any mistake or if there’s a better/easier way of accomplishing this task, so for all of you math-people out there, I need your brainpower . I want to make sure I’m doing this correctly so I don’t waste my time writing a bunch of useless code.
Let me lay out the scenario that needs to be solved. You have two parts that can be travelling in ANY given direction on a 2D-Plane, ignoring the Y axis as the parts will always be bound to the same height on the Y axis. As stated in the title, I need to figure out how much of part #2’s velocity is perpendicular to the velocity of part #1. That statement may be a bit confusing, and may not be the best way of describing the problem, so I’ve illustrated the problem below with an image to help.
Black lines = travel direction of parts
Light blue numbers = the velocity vector
Green lines = perpendicular travel direction
Pink line = travel direction of first part shown in relation to second part
Red highlight on green line = the distance value I’m trying to figure out
Blue = Axis View (This is a top-down view, Y axis is towards you)
Here’s the steps for my current approach to solve this problem:
- If either part has a velocity vector with a negative X value such as part#2, flip the signs of both X and Z so that we can create a proper line equation using the traditional rules of the X axis only moving from negative to positive. In the example above, the second part’s velocity vector would change from (-2, 2) to (2, -2)
- Assume point A = (0, 0)
- Create equation for line AB (Z = -1X)
- Create equation for line AC (Z = -2/3X)
- Create equation for line BC (Z = 3/2X - 5)
- Find intersection of line AC and line BC, steps for example below
- -2/3X = 3/2X - 5
- 2/3X + 3/2X = 5
- 13/6X = 5
- X = 5 / (13/6)
- X ~= 2.308
- Z = -2/3(2.308)
- Z ~= -1.538
- Intersection = (2.308, -1.538)
- Finally, either use another equation that calculates distance between two points, or just use roblox’s vector magnitude property to calculate the distance between point B (2, -2 in example) and the intersection point that was just calculation (point C in example)
I also need to know the direction on the X axis of this final distance value, positive or negative, and I’m assuming I can just grab the sign of X from the original velocity vector or part #2, before any potential sign flips in step 1.
Thoughts?