Skip to content

Commit a7fdfca

Browse files
authored
Vec3 improvements (#37)
1 parent 0cbea23 commit a7fdfca

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,6 @@ ENV/
110110
# Gradle files
111111
/.gradle
112112

113+
# VSCode
113114
.vscode/
115+
*.code-workspace

src/bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def get_output(self, packet: GameTickPacket) -> SimpleControllerState:
4747

4848
# Example of using a sequence
4949
# This will do a front flip if the car's velocity is between 550 and 600
50-
if 550 < car_velocity.length() < 600:
50+
if 550 < car_velocity.length < 600:
5151
self.active_sequence = Sequence([
5252
ControlStep(0.05, SimpleControllerState(jump=True)),
5353
ControlStep(0.05, SimpleControllerState(jump=False)),

src/util/vec.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 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

Comments
 (0)