Legacy GM Math problem involving 2 objects moving together

U

Undead Hero

Guest
Hey guys. Math is the big thing I have trouble with, which I realize makes me a pretty silly programmer.

Nonetheless. I'm trying to learn and I could use some help with a problem. I have 2 objects that I need to move simultaneously, in opposite directions.

Object A moves on the x axis in a range of -2120 to 3080, with 960 being in the center.
Object B moves in a range of 720 to 1200, again with 960 being the center.

I need Object B to move opposite to Object A, ie, when Object A.x = -2120, Object B.x = 1200, and vice versa, while they will both land on 960 together. Math comes in because they will need to match up accordingly along the entire spectrum.

Can anyone more talented with maths help me out?
 

jo-thijs

Member
960 isn't the center of -2120 and 3080, 480 is.
This makes that Azure Spectre's code has to become:
Code:
objectB.x = 960 - (objectA.x - 480) / 2600 * 240;
 
U

Undead Hero

Guest
Yeah, I screwed up. -1160 is what I meant, not -2120. Object A moves 2120 pixels in either direction. I did warn you that I'm not good at math.
 
U

Undead Hero

Guest
Okay, thank you very much. I swapped in 2120 for 2600 and it worked. I'll have to take some more time to analyze it to understand why it works, though. o_O
 

jo-thijs

Member
You normalize your range from (-1160, 3080) to (-1, 1) in 2 steps:
1) Match the center of A (960) to 0. Do this by subtracting 960 from A.
2) Scale A to fit the (-1, 1) range. Since the moved A has the range (-2120, 2120), you just simply divide this by 2120.
This so far leaves us with the expression: (objectA.x - 960) / 2120.

Now, we reverse the previous and get this normalized range (-1, 1) to (720, 1200) by:
1) Scaling this to fit the range (-240, 240) (and mirroring it). Do this by multiplying the previous with -240.
2) Moving the center to match B. Do this by adding 960 to the last result.
This now gives you the final result of: objectB.x = (objectA.x - 960) / 2120 * (-240) + 960.

Alternatively, you could have also used linear interpolation between 2 points.
Let c, d and e be 3 constants we seek.
Let us say that A and B correlate with each other as follows:
c * A + d * B + e = 0

If we now take 2 points of your data (A = -1160, B = 1200 and A = 3080, B = 720), we get the following conditions for our constants:
-1160 * c + 1200 * d + 1 * e = 0
3080 * c + 720 * d + 1 * e = 0

Using gauss elimination (https://en.wikipedia.org/wiki/Gaussian_elimination),
we can reduce these conditions to the ones below:
c + e / 9440 = 0
d + e * 53 / 56640 = 0

Now, we'll want d = -1, so that our equation:
c * A + d * B + e = 0
becomes:
B = c * A + e

This makes:
e = 56640 / 53
c = -(56640 / 53) / 9440 = - 6 / 53

And this gives us the final result of: objectB.x = (56640 - objectA.x * 6) / 53.
This corresponds to the previous final result found.
 
U

Undead Hero

Guest
Thanks a lot, this is a great community here. I think more than anything, I just need to get back to Khan academy's math program.
 
Top