 # Math - Vector Addition - Challenge

In this lecture we look at how to add vectors together, which is one of the more common things you’ll be doing with two vectors.

For your challenge, I want you to find the current position of a camera that’s following the player at a fixed distance.

The player starts at (5,0) and the camera starts at (2,1).

First find the offset between the player and the camera.
Then work out the camera position when the player has moved to (11,0).

Once you’ve found your answers, post them below to let us know how you got on (remember to use spoiler tags).

If you need some extra help, there are some extra hints at the end of the video.

new camera pos is at (8,1)

1 Like

The offset is (-3,0)
When you put in the values you get (8,1) as the new camera position.

Here is the math:

CameraPos - PlayerPos = offset

(8,1) - (11,1) = (-3,0)

Now to check

PlayerPos + offset = CameraPos

(11,1) + (-3,0) = (8,1)

I’m not going to lie I was confused but I think I figured it out. I was just unsure how we used the hints to help get the offset. Instead I just counted the starting point where we had the player at (5,1) and the camera at (2,1) and did the same when it was at (11,1), but I felt like I didn’t really do anything special.
@garypettie I think i’m just overthinking it.

Hey @Kevin-Brandon_Joseph ,
When you draw the problem out like this, it’s pretty easy to get the answer just from looking at it and thinking it through.

If you were just starting with the two variables and no picture, it’s a little more abstract.
Say you just had the two variables in code:
`playerPos = (5,0)`
`cameraPos = (2,1)`

This is where finding the offset and storing it in a variable can be useful.
So you can do a vector subtraction to get;

`offset = cameraPos - PlayerPos`
`offset = (2, 1) - (5, 0)`
`offset = (2-5, 1-0)`
`offset = (-3, 1)`

And now you can just add that offset to the player, where ever they may end up.

So at (11,0) it would be;

`cameraPos = playerPos + offset`
`cameraPos = (11, 0) + (-3, 1)`
`cameraPos = = (11-3, 0+1)`
`cameraPos = = (8, 1)`

or if the player was at say (-5, 3), you’d get;

`cameraPos = playerPos + offset`
`cameraPos = (-5, 3) + (-3, 1)`
`cameraPos = = (-5-3, 3+1)`
`cameraPos = = (-8, 4)`

Hopefully that helps clear things up. If not, do let me know.

1 Like

This is exactly what I needed. Thank you Gary! Now I understand completely. My least favorite thing in math is getting the right answer but not completely understanding how.

1 Like

Calculating the camera offset always gave me a hard time when I’m trying to get the camera to follow the player because I keep forgetting which Vector should be subtracted from the other 1 Like

I did screw this up a bit at first and subtracted camera from player (which gave the inverse of the vector I was looking for. The answer was obviously wrong (-3, 1), but figuring out which vector to subtract from is definitely something I need to wrap my head around. I’m going to watch the hints next and read through previous posts to see if it’s already come up! I just wanted to post my answer before I read any more.

Camera − Player = ((2−5),(1−0))
Camera −Player=(−3, 1)=CameraOffset

〖Player〗_1+CameraOffset=((11+(−3)), (0+1))
〖Player〗_1+CameraOffset=(8, 1)

1 Like

@Dendrolis, you’re spot on!

The offset will be (-3, 1), which is camera position (2, 1) - player position (5, 0). The camera position after player has moved will be (8, 1), which is either new player position (11, 0) + offset (-3, 1) or old camera position (2, 1) + movement of player (6, 0). (