Vectors!

Hello, I’ve been learning how to implement vectors into game programming recently but I’m having some trouble with a couple of things. Mainly if I want an object to go from one point to another, would I create a vector where the components are as followed

V = <sprite1.x - sprite2.x, sprite1.y - sprite.y>

and then normalize, and then add the resulting x and y values onto a sprites x and y?

And would this be a Velocity vector since it’s changing the sprites position every game cycle?

Thanks in advance! :wink:

Velocity is a vector which has both “direction” and “speed”

You find the direction by normalizing the Velocity vector, and the speed is the magnitude or the length of the vector.

If you have a 2D sprite at position V1 = <x1, y1> and want to move it a 2D position with vector V2 = <x2, y2> you can find the velocity vector for movement by doing the following

velocity = V2 - V1

float speed = velocity.magnitude
Vector direction = velocity.normalize

I hope that explains things a bit =]

lets start with you having a some fully functional vector classes.

Using vectors for your game is totaly easy, you don’t need to think about such “complex” stuff as velocity vector.

Lets say we have to objects P and Q and you want P to move to Q


//you have the "position" vectors of P and Q this means you now the coordinate difference between O(origin) and P,Q
Vector OP = ...
Vector OQ = ...

//now to get from P to Q you need to now the difference between the two coordinats
//to get from P to O
Vector PO = -OP
//and the to go to Q
Vector PQ = PO + OQ

//when you now want to only move 2 units in one gamestep towards Q you do this
//first normalize PQ, after that the length is 1
Vector nPQ = normalize(PQ)
//then just multiply with the amount you want to move in one step
Vector movement = nPQ * 2

//to get your new "position" vector after this step just add it to your current position
Vector newP = P + movement

Thanks but that was pretty much what I said, I was adding the resultant direction vector components (from the vector subtraction of the two points) to the sprite that I wanted to move, I mean I tested it before and it worked I just wanted some clarification that I was going the right way about things.

Yes you are.

If you wish to know more about working with vectors then I suggest this article: http://www.wildbunny.co.uk/blog/vector-maths-a-primer-for-games-programmers/. That is by far my most linked link. It’s written by a game developer, with other game developers in mind and it’s a great tutorial IMO. Also there is matrix and some transform stuff later on, which is also extremely useful once you get to that point.

Also @Danny02: I’m not sure what you’re trying to say. Java doesn’t have operation overloads.

Yeah I just wanted to make sure, and I find vectors really interesting so I’ll be sure to check that article out, thanks!

Vectors aren’t magic sticks that do things automatically - they’re just a box with 2 values in them.

How you decide to perceive and use those 2 values makes them “different”.

There already is a thread on Vectors - use the Search function next time.

For example, instead of moving an object like this:

int x = 0;
int y = 0;
int xspeed = 1;
int yspeed = 2;

step() {
  x += xspeed;
  y += yspeed;
}

With Vectors you could do it like this:

Vector position = new Vector(0,0);
Vector speed = new Vector(1,2);

step() {
  position.addVector(speed);
}

class Vector {
int value1 = 0;
int value2 = 0;

Vector addVector(Vector v) {
  this.value1 += v.value1;
  this.value2 += v.value2;
  return this;
}

You do the same thing basically where Vectors are just a useful abstraction.

The answer to the first post is “yes.” I don’t know what question other people are answering. Do learn as much vector math as you can (including matrices.) Vectors are a type of number in the same way counting numbers, rational numbers, Booleans, and imaginary numbers are. Pursue an understanding of the mathematical operations; The source code implementation is trivial.

Lol, that made me chuckle, and that is a pretty cool way of implementing them, thanks for the help :slight_smile: