11import math
2+ from typing import Union
23
34
45# This is a helper class for vector math. You can extend it or delete if you want.
@@ -13,8 +14,14 @@ class Vec3:
1314
1415 When in doubt visit the wiki: https://github.com/RLBot/RLBot/wiki/Useful-Game-Values
1516 """
16-
17- def __init__ (self , x : float or 'Vec3' = 0 , y : float = 0 , z : float = 0 ):
17+ # https://docs.python.org/3/reference/datamodel.html#slots
18+ __slots__ = [
19+ 'x' ,
20+ 'y' ,
21+ 'z'
22+ ]
23+
24+ def __init__ (self , x : Union [float , 'Vec3' ]= 0 , y : float = 0 , z : float = 0 ):
1825 """
1926 Create a new Vec3. The x component can alternatively be another vector with an x, y, and z component, in which
2027 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':
5865 return self * scale
5966
6067 def __str__ (self ):
61- return "Vec3(" + str (self .x ) + ", " + str (self .y ) + ", " + str (self .z ) + ")"
68+ return f"Vec3({ self .x :.2f} , { self .y :.2f} , { self .z :.2f} )"
69+
70+ def __repr__ (self ):
71+ return self .__str__ ()
6272
6373 def flat (self ):
6474 """Returns a new Vec3 that equals this Vec3 but projected onto the ground plane. I.e. where z=0."""
6575 return Vec3 (self .x , self .y , 0 )
6676
77+ @property
6778 def length (self ):
6879 """Returns the length of the vector. Also called magnitude and norm."""
6980 return math .sqrt (self .x ** 2 + self .y ** 2 + self .z ** 2 )
7081
7182 def dist (self , other : 'Vec3' ) -> float :
7283 """Returns the distance between this vector and another vector using pythagoras."""
73- return (self - other ).length ()
84+ return (self - other ).length
7485
86+ @property
7587 def normalized (self ):
7688 """Returns a vector with the same direction but a length of one."""
77- return self / self .length ()
89+ return self / self .length
7890
7991 def rescale (self , new_len : float ) -> 'Vec3' :
8092 """Returns a vector with the same direction but a different length."""
81- return new_len * self .normalized ()
93+ return new_len * self .normalized
8294
8395 def dot (self , other : 'Vec3' ) -> float :
8496 """Returns the dot product."""
@@ -94,5 +106,5 @@ def cross(self, other: 'Vec3') -> 'Vec3':
94106
95107 def ang_to (self , ideal : 'Vec3' ) -> float :
96108 """Returns the angle to the ideal vector. Angle will be between 0 and pi."""
97- cos_ang = self .dot (ideal ) / (self .length () * ideal .length () )
109+ cos_ang = self .dot (ideal ) / (self .length * ideal .length )
98110 return math .acos (cos_ang )
0 commit comments