I've been struggling a lot to learn how to use vectors in programming. Now I've successfully made my own vector class but I still can't use vectors for movement.
This is my class :
class MyVector2(object):
def __init__(self, x, y):
self.x, self.y = x, y
self.tuple = (self.x, self.y)
def __str__(self):
return f"({self.x}, {self.y})"
@property
def magnitude(self):
return math.sqrt(self.x**2 + self.y**2)
def normalize(self):
self.x /= self.magnitude
self.y /= self.magnitude
@staticmethod
def create_path(destination, pos):
return MyVector2(destination.x - pos.x, destination.y - pos.y)
@staticmethod
def dot(v1, v2):
return v1.x * v2.x + v1.y * v2.y
(there are also other methods for the operations (+/*-) that I don't need to copy here)
My problem with movement is that the object I want to move doesn't stop at the destination but continues moving infinitely :
class Obj(object):
def __init__(self):
self.x, self.y = 100, 80
self.speed = 0.1
self.pos = MyVector2(self.x, self.y)
self.img = img
self.moving = True
def blit(self):
screen.blit(self.img, (self.pos.tuple))
obj = Obj()
dest = MyVector2(150, 130)
path = MyVector2.create_path(dest, obj.pos)
path.normalize()
while True:
obj.blit()
if obj.moving:
obj.pos += path * obj.speed
if dest == obj.pos:
obj.moving = False
I also think that my way of using vectors to move objects is bad, so please provide me with a professional one.
path * obj.speed
brings you further away from your destination, and if it does, stop at your destination... \$\endgroup\$dest == obj.pos
) is rarely useful/relevant in game programming because of how data is stored internally by the computer. You should use something like anequivalent
orapproximately
function which lets you decide if they're "close enough" for your needs (some implementations have three parameters: the two values to compare and the "range" (epsilon), which is often set to a default value). \$\endgroup\$equivalent(a, b, epsilon = 0.00001) : return abs(a - b) < epsilon
, you can add an equivalent function to your vector class such as this (pseudocode):equivalent(self, other, epsilon = 0.00001) : return equivalent(self.x, other.x, epsilon) and equivalent(self.y, other.y, epsilon)
. \$\endgroup\$