What is Vector?
Suppose we want to move from point A to point B where point A is situated at (33.0, 35.0) and point B is at (55.0, 45.0) then Vector AB will be the different between these two points, or the x and the y distance between this two points is (x2-x1, y2-y1) or (55.0-33.0, 45.0-35.0).
Why do we need to create a vector class?
Vector module helps game developer to perform various operations, for example moves an object from point A to point B as well as find out the vector magnitude of that object, therefore it is always better if we can create a Vector module before we create our game.
Create a Vector2D class in python
Vector class is just like any other module class with methods that we can use to move an object or modify the property of that object.
import math class Vector2D(object): def __init__(self, x=0.0, y=0.0): self.x = x self.y = y def __str__(self): return "(%s, %s)"%(self.x, self.y) @classmethod def next_vector(cls, args): return cls(args-args, args-args) def get_magnitude(self): return math.sqrt( self.x**2 + self.y**2 ) def normalize(self): # find the unit vector magnitude = self.get_magnitude() self.x /= magnitude self.y /= magnitude # rhs stands for Right Hand Side def __add__(self, rhs): return Vector2D(self.x + rhs.x, self.y + rhs.y) def __sub__(self, rhs): return Vector2D(self.x - rhs.x, self.y - rhs.y) def __neg__(self): return Vector2D(–self.x, –self.y) def __mul__(self, scalar): return Vector2D(self.x * scalar, self.y * scalar) def __div__(self, scalar): return Vector2D(self.x / scalar, self.y / scalar)
A few methods above are the overload methods where they will be called when the Vector class instance performs certain operation, for example the __div__, __mul__, __sub__ and __add__ method will be called when we divide, multiply, subtract and add two vectors together. The __neg__ method will be called if we want to point a Vector in the opposite direction.
The __init__ method will be called at the moment we initialized the Vector2D’s instance and __str__ will be called when we print that object with the python print function.
The get_magnitude method will return the magnitude of the Vector and the normalize method will divide the x and the y length of the Vector with it’s magnitude.
Finally next_vector will take in the combine value of two tuples and return a new Vector2D object.
Create a separate python module with below script.
from vector2d import Vector2D if __name__ == "__main__": A = (10.0, 20.0) B = (30.0, 35.0) C = (15.0, 45.0) AB = Vector2D.next_vector(A+B) BC = Vector2D.next_vector(B+C) AC = AB+BC print(AC) AC = Vector2D.next_vector(A+C) print(AC)
If you run the above module then you can see that when you add up two vectors AC = AB + BC the overload __add__ method of vector AB will be called which will then return a new Vector2D object. AC = Vector2D.next_vector(A+C) will create the same outcome as AC = AB + BC when we print the vector out with the print function. In this example the result is (5.0, 25.0).
The above Vector2D function will get you started where you can now include more methods into the Vector2D module for the future expansion purposes.