Move object B with offset from A?

300 Views Asked by At

I am unsure of a proper title for this post and haven't had luck finding a proper answer. The best way I can describe this is, I have two objects $A$ and $B$ where $B$ should move relative to $A$. To be more specific:

  • The position for $A$ is always locked between $(0, 0)$ and $(W, H)$.
  • Object $B$ should move with $A$ at certain times.
    • Should also maintain its offset from $A$ from when movement began.

For example:

$A = (150, 50), B = (0, 0)$

$A$ moves freely for a while.

Event occurs that makes $B$ move with $A$.

$A = (250, 100), B = (0, 0)$

Offset is $(250, 100)$.

  • If $A$ moves to $(350, 200)$ then $B$ should move to $(100, 100)$.

  • If $A$ moves to $(150, 50)$ then $B$ should move to $(-100, -50)$.

I think the math behind this is:

$$B = A - B$$

But while testing it this causes jumping on $B$ and I'm not sure if the math I came up with is correct at this point. Is my math correct, or should I be doing something differently.


Note

If the tags are incorrect, or additional tags would be better, please edit the post.

1

There are 1 best solutions below

0
On BEST ANSWER

Your equation says that $B=\frac{1}{2}A$. What you really want is to calculate the offset $(p,q)=A_0-B_0$ where $A_0$ and $B_0$ are the initial positions.

Then you get that $B_n=A_n-(p,q)$ where $A_n$ and $B_n$ are the new positions.

Using your example with $A_0=(250,100)$ and $B_0=(0,0)$, the offset is $A_0-B_0=A_0=(250,100)$ and $B_n=A_n-(p,q)=(350,200)-(250,100)=(100,100)$.