Adding two 2D vectors.. ?

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Let's say my vectors are made up of a radian and length:
Code:
class vector {
	float	rad,
		len;
};

How would I go about adding two vectors to get a new one?
I've tried for a bit but I never was good at math =/
(Which says a lot about my future as a game programmer.. ><)

[EDIT]
Ehh..
Here's my solution:
Let's say I have two 2D-vectors..
Code:
//Set up my vectors
float pi = 3.141592653589;
vector a, b;

a.rad = (pi/4);
a.len = sprt(18);

a.rad = ((3 * pi) / 2);
a.len = 1;

//Then, when I want to add them..
vector result;
float x = (cos(a.rad) * a.length + cos(b.rad) * b.length);
float y = (sin(a.rad) * a.length + sin(b.rad) * b.length);

result.rad = atan2(y, x);
result.len = sqrt(x*x + y*y); //Pythagoras' theorem

The above seem to get me my solution (On paper at least, I haven't tried coding it xD) but is it efficient?
Probably not, with that expensive sqrt there.. =/
Anyone got a better way?
 

Slapshot136

Divide et impera
Reaction score
471
so the vector is a length + angle, so you draw it on a graph (X,Y) and then find the height and length of the point relative to the origin (convert it into X,Y)

so do cos(rad) * len (for the X component), and then sin(rad) * len for the Y component - then do the same for your 2nd vector, and then add x1 + x2 = xSum, y1+ y2= ySum, and then re-combine the two (to find the lenSum, you need to use the pythagorean theorem) - and then to find the rad, first check which ones are positive/negative - if you have the ySum as negative, it is in the bottom half of the graph, so rad is between 1 and 2, if it's positive it's between 0 and 1 - then check the sign of the xSum, if it's positive it's between 1.5 and .5 (remember this goes in a circle), and if it's negative, it's between .5 and 1.5). so now you need to take the inverse sin (or cos, but lets use sin to keep it simple) to get from 3 lengths to an angle (you have a triangle, a distance on the X axis, a distance on a Y axis, and the combined height/length of the two) - for inverse sin you need to use
sin^-1(Ysum/lenSum) and you get an angle, but now you need to do a reality check on the angle and put it in which quadrant it belongs in (add .5 or 1 or 1.5 to it based on the previous +/- tests)

there might be some class/function that can do this for you, but this is how I would do it if I was to do it on paper, so I think that this is the easiest way to explain it so you understand
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Thanks,
I didn't think about the xSum's and ySum's signs affecting the angle ;O

Your solution and my edit on the original post were almost the same xD
 

saw792

Is known to say things. That is all.
Reaction score
280
It's a strange thing to be representing vectors in polar form. It's much more common to use an {x, y, z} type representation as operations on the individual directional components tend to be more common. Why have you chosen a polar representation?
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
I figured I'd get started on a pong game =P
And well, the ball moves at an angle at a certain speed, right?

So..
rad //angle
speed //well, length of distance it travels per pixel

And my friend saw it and said it'd be cool if I could add some extra stuff like gravity modifiers and power ups to change the acceleration of the ball, etc.
And when it came to gravity's effect on motion, I figured I'd have to add vectors to get it done.

I use the polar representation (never knew there were names for it) because it's more natural to me than the other represenation you're suggesting.
And I wouldn't know how to represent certain angles in that format =x
Makes my head spin thinking about it.
 

saw792

Is known to say things. That is all.
Reaction score
280
Well the more common way to do something like this is to use multiple vectors for different parts of the balls movement. For example, you use one vector {x, y} for the ball's position, a vector {xvel, yvel} for the ball's velocity and (perhaps) another vector {xaccel, yaccel} for the ball's acceleration. So each tick of your program you update your position vectors based on velocity and velocity vectors based on acceleration, eg: x = x + xvel * time, xvel = xvel + xaccel * time (same for y). The velocity vector specifies a direction through coordinates. What this means is that if a ball has a velocity vector of {1, 2} and you place the origin of a coordinate system over your ball it will be moving in a straight line towards position {1, 2}. At the same time the ball may be accelerating to the left, which would mean next tick the ball might be moving towards {1, 3}. It helps a lot if you draw these things on a piece of paper first.
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Oh..
If I want to change the vectors to a new angle, I just recalculate the offsets.. right?
 

saw792

Is known to say things. That is all.
Reaction score
280
Well generally if you want to change the velocity you do so by applying an acceleration. Eg: Your ball hits a powerup which pushes boosts it to the right. Implementation would simply involve adding a constant amount to the y acceleration for a few ticks (another option is just adding to the y part of the velocity). Generally if you want the ball to behave "naturally" it will not just shift direction suddenly so you may not have to worry about using angles.
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
How about paddles.. ?
Hitting the curved paddle at different points would change the angle each hit considerably.. =/

Oh, wait-
I just have to re-calculate the vector..
And it'll use those expensive operations less ;O

That's a great idea, saw.
Thanks =)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top