I’ve created a class, whose object falcon_vectors
holds each vector I add to it and the class is also responsible for updating the vectors coordinates i.e. in main update function I just call falcon_vectors.update()
. They each start at the same origin point, but their end points (x2, y2) differ depending on what kind of vector they are (e. g. horizontal velocity vector, vertical velocity vector, both).
My problem is: when I pass a list into the function (f9.v_m_s … the first vector consisting of both the horizontal and the vertical component), it displays the vector properly, since (I believe) instead of passing the initial coordinates 0;0, python passes the entire list and later in the function, when the function looks into the list, it finds out, that the coordinates are no longer 0;0, but for example 100,100 so the vector displays properly
But as soon as I want to create just the horizontal component of that vector i.e. I input just the horizontal x value of that vector and leave the vertical value as 0, (I believe) that python passes just the initial coordinates 0;0, meaning, that the update function always updates x2 and y2 to 0;0 instead of the current, updated coordinates, so the vector doesn’t display at all, since the line’s end point matches its staring point
so how can I tell the update function, to look into the first index of f9.v_m_s list every time it updates the vector’s coordinates?
class FalconVectors:
def __init__(self) -> None:
self.vectors = []
def new_vector(self, coordinates, color, visual_enlargement_factor):
vector = {
"pyglet_component": pyglet.shapes.Line(0,0,0,0,3,color,sw_batch),
"coordinates": coordinates,
"visual_enlargement_factor": visual_enlargement_factor
}
self.vectors.append(vector)
def update(self):
for vector in self.vectors:
vector["pyglet_component"].x = falcon_screen_location_point_px.x
vector["pyglet_component"].y = falcon_screen_location_point_px.y
vector["pyglet_component"].x2 = falcon_screen_location_point_px.x + m_into_px(vector["coordinates"][0]) * vector["visual_enlargement_factor"]
vector["pyglet_component"].y2 = falcon_screen_location_point_px.y + m_into_px(vector["coordinates"][1]) * vector["visual_enlargement_factor"]
falcon_vectors = FalconVectors()
#f9.v_m_s = [0, 0] <-- later gets updated
falcon_vectors.new_vector(f9.v_m_s, RED, 500) #velocity vector
falcon_vectors.new_vector((f9.v_m_s[0], 0), BLUE, 500) #horizontal velocity vector
btw, m_into_px
just transforms meters into pixels
…
I’ve managed to solve it by adding two booleans to each vector, which determine, which component (horizontal, vertical or both) should be added to the vector’s origin point.
But the question remains: how can I pass the variable itself into the function instead of the number the variable is pointing at?
class FalconVectors:
def __init__(self) -> None:
self.vectors = []
def new_vector(self, coordinates, access_0, access_1, color, visual_enlargement_factor):
vector = {
"pyglet_component": pyglet.shapes.Line(0,0,0,0,3,color,sw_batch),
"coordinates": coordinates,
"access_0": access_0,
"access_1": access_1,
"visual_enlargement_factor": visual_enlargement_factor
}
self.vectors.append(vector)
def update(self):
for vector in self.vectors:
vector["pyglet_component"].position = falcon_screen_location_point_px.position
vector["pyglet_component"].x2 = falcon_screen_location_point_px.x
vector["pyglet_component"].y2 = falcon_screen_location_point_px.y
if vector["access_0"]:
vector["pyglet_component"].x2 += m_into_px(vector["coordinates"][0]) * vector["visual_enlargement_factor"]
if vector["access_1"]:
vector["pyglet_component"].y2 += m_into_px(vector["coordinates"][1]) * vector["visual_enlargement_factor"]
falcon_vectors = FalconVectors()
#f9.v_m_s2 = [0, 0] <-- later gets updated
falcon_vectors.new_vector(f9.v_m_s2, True, True, RED, 500) #velocity vector
falcon_vectors.new_vector(f9.v_m_s2, True, False, BLUE, 500) #horizontal velocity vector
Tofu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1