diff --git a/.gitignore b/.gitignore index e7803d8..89563b0 100644 --- a/.gitignore +++ b/.gitignore @@ -110,4 +110,6 @@ ENV/ # Gradle files /.gradle +# VSCode .vscode/ +*.code-workspace diff --git a/src/bot.py b/src/bot.py index 99bfb64..9ac2d85 100644 --- a/src/bot.py +++ b/src/bot.py @@ -47,7 +47,7 @@ def get_output(self, packet: GameTickPacket) -> SimpleControllerState: # Example of using a sequence # This will do a front flip if the car's velocity is between 550 and 600 - if 550 < car_velocity.length() < 600: + if 550 < car_velocity.length < 600: self.active_sequence = Sequence([ ControlStep(0.05, SimpleControllerState(jump=True)), ControlStep(0.05, SimpleControllerState(jump=False)), diff --git a/src/util/vec.py b/src/util/vec.py index 6850e4f..055bc93 100644 --- a/src/util/vec.py +++ b/src/util/vec.py @@ -1,4 +1,5 @@ import math +from typing import Union # This is a helper class for vector math. You can extend it or delete if you want. @@ -13,8 +14,14 @@ class Vec3: When in doubt visit the wiki: https://github.com/RLBot/RLBot/wiki/Useful-Game-Values """ - - def __init__(self, x: float or 'Vec3'=0, y: float=0, z: float=0): + # https://docs.python.org/3/reference/datamodel.html#slots + __slots__ = [ + 'x', + 'y', + 'z' + ] + + def __init__(self, x: Union[float, 'Vec3']=0, y: float=0, z: float=0): """ Create a new Vec3. The x component can alternatively be another vector with an x, y, and z component, in which case the created vector is a copy of the given vector and the y and z parameter is ignored. Examples: @@ -58,27 +65,32 @@ def __truediv__(self, scale: float) -> 'Vec3': return self * scale def __str__(self): - return "Vec3(" + str(self.x) + ", " + str(self.y) + ", " + str(self.z) + ")" + return f"Vec3({self.x:.2f}, {self.y:.2f}, {self.z:.2f})" + + def __repr__(self): + return self.__str__() def flat(self): """Returns a new Vec3 that equals this Vec3 but projected onto the ground plane. I.e. where z=0.""" return Vec3(self.x, self.y, 0) + @property def length(self): """Returns the length of the vector. Also called magnitude and norm.""" return math.sqrt(self.x**2 + self.y**2 + self.z**2) def dist(self, other: 'Vec3') -> float: """Returns the distance between this vector and another vector using pythagoras.""" - return (self - other).length() + return (self - other).length + @property def normalized(self): """Returns a vector with the same direction but a length of one.""" - return self / self.length() + return self / self.length def rescale(self, new_len: float) -> 'Vec3': """Returns a vector with the same direction but a different length.""" - return new_len * self.normalized() + return new_len * self.normalized def dot(self, other: 'Vec3') -> float: """Returns the dot product.""" @@ -94,5 +106,5 @@ def cross(self, other: 'Vec3') -> 'Vec3': def ang_to(self, ideal: 'Vec3') -> float: """Returns the angle to the ideal vector. Angle will be between 0 and pi.""" - cos_ang = self.dot(ideal) / (self.length() * ideal.length()) + cos_ang = self.dot(ideal) / (self.length * ideal.length) return math.acos(cos_ang)