1
1
import math
2
+ from typing import Union
2
3
3
4
4
5
# This is a helper class for vector math. You can extend it or delete if you want.
@@ -13,8 +14,14 @@ class Vec3:
13
14
14
15
When in doubt visit the wiki: https://github.com/RLBot/RLBot/wiki/Useful-Game-Values
15
16
"""
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 ):
18
25
"""
19
26
Create a new Vec3. The x component can alternatively be another vector with an x, y, and z component, in which
20
27
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':
58
65
return self * scale
59
66
60
67
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__ ()
62
72
63
73
def flat (self ):
64
74
"""Returns a new Vec3 that equals this Vec3 but projected onto the ground plane. I.e. where z=0."""
65
75
return Vec3 (self .x , self .y , 0 )
66
76
77
+ @property
67
78
def length (self ):
68
79
"""Returns the length of the vector. Also called magnitude and norm."""
69
80
return math .sqrt (self .x ** 2 + self .y ** 2 + self .z ** 2 )
70
81
71
82
def dist (self , other : 'Vec3' ) -> float :
72
83
"""Returns the distance between this vector and another vector using pythagoras."""
73
- return (self - other ).length ()
84
+ return (self - other ).length
74
85
86
+ @property
75
87
def normalized (self ):
76
88
"""Returns a vector with the same direction but a length of one."""
77
- return self / self .length ()
89
+ return self / self .length
78
90
79
91
def rescale (self , new_len : float ) -> 'Vec3' :
80
92
"""Returns a vector with the same direction but a different length."""
81
- return new_len * self .normalized ()
93
+ return new_len * self .normalized
82
94
83
95
def dot (self , other : 'Vec3' ) -> float :
84
96
"""Returns the dot product."""
@@ -94,5 +106,5 @@ def cross(self, other: 'Vec3') -> 'Vec3':
94
106
95
107
def ang_to (self , ideal : 'Vec3' ) -> float :
96
108
"""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 )
98
110
return math .acos (cos_ang )
0 commit comments