Skip to content
VirxEC edited this page Apr 24, 2021 · 10 revisions

util/utils.py

cap(x, low, high)

Returns x but capped between low and high.

  • x - A number.
  • low - A number.
  • high - A number.

cap_in_field(agent, target)

Caps target so the bot won't drive through walls when going to it.

  • agent - An instance of VirxERLU. If you're running this from an instance of VirxERLU, pass in self.
  • target - A Vector.

defaultDrive(agent, target_speed, local_target)

Using the given parameters, this method makes the bot drive towards local_target with a speed of target_speed.

This util understands going backwards (negative target speed) as well as how to use handbrake.

This is the recommended util for driving to a target.

Returns the target angles and the car's forward velocity.

  • agent - An instance of VirxERLU. If you're running this from an instance of VirxERLU, pass in self.
  • local_target - The local location of the target to face. See local_location under car_object in util/agent.py.
  • target_speed - A number between -1410 and 2300.

defaultPD(agent, local_target, upside_down=False, up=None)

Alters the agent's controller to face a given target. Works for backwards and forwards driving.

  • agent - An instance of VirxERLU. If you're running this from an instance of VirxERLU, pass in self.
  • local_target - The local location of the target to face. See local_location under car_object in util/agent.py.
  • upside_down - If True, then this will have the top of the car face the ground. Only works when airborne.
  • up - Which direction to face via rolling. Only works when airborne. Defaults to actually facing upwards.

defaultThrottle(agent, target_speed)

Alters the agent's controller to accelerate to a given speed using throttle and boost.

  • agent - An instance of VirxERLU. If you're running this from an instance of VirxERLU, pass in self.
  • target_speed - A number between -1410 and 2300.

curvature_to_velocity(curve)

Returns the velocity of the car from the car's turning curvature.

  • curve - A number that represents the turning curvature of the car.

Example:

turn_rad = turn_radius(abs(agent.me.local_velocity().x))
velocity_from_turn_rad = curvature_to_velocity(1 / turn_rad)

throttle_acceleration(car_velocity_x)

Returns the acceleration that the throttle can give when set to 1 when the car is traveling at car_velocity_x speed.

  • car_velocity_x - A number that represents the forward velocity of the car.

is_inside_turn_radius(turn_rad, local_target, steer_direction)

Returns whether or not the car needs to use handbrake to reach local_target.

turn_radius(v)

Returns the turning radius of the car.

  • v - A number that represents the forward velocity of the car.

curvature(v)

Returns the curvature of the turning radius of the car.

  • v - A number that represents the forward velocity of the car.

in_field(point, radius)

Returns whether or not point with a radius of radius is in the field or not.

  • point - A Vector.
  • radius - A number.

find_slope(shot_vector, car_to_target)

Returns the slope of the bot's position relative to the shot vector.

10 = The bot is on the axis and the ball is between you and the direction to shoot in.

-10 = The bot is on the wrong side.

1 = The bot is about 45 degrees offcenter.

  • shot_vector - A Vector.
  • car_to_target - A Vector that is the target's location minus the bot's location.

quadratic(a, b, c)

Returns the 2 roots of a quadratic.

  • a - A number.
  • b - A number.
  • c - A number.

shot_valid(agent, shot, target=None)

Returns whether or not the ball is still where the shot anticipates it to be.

  • agent - An instance of VirxERLU. If you're running this from an instance of VirxERLU, pass in self.
  • shot - An instance of a shot routine. See util/routines.py.
  • target - A Vector. If undefined, it will default to shot.ball_location.

side(x)

Returns 1 for orange team and -1 for blue team

  • x - A team. If you're running this from an instance of VirxERLU, pass in self.team (for current team) or not self.team (for the enemy team).

sign(x)

Returns the sign of x - either -1, 0, or 1. Python doesn't have built-in method to do this like it should, so it's here instead.

  • x - A number.

steerPD(angle, rate)

A Proportional-Derivative control loop used for defaultPD.

  • angle - Angle in radians.
  • rate - A number.

lerp(a, b, t)

Returns the linear interpolation from a to b using t.

For instance, when t == 0, a is returned, and when t is 1, b is returned, and when t is 0.5, the midpoint from a to b it returned.

invlerp(a, b, v)

Returns the inverse linear interpolation from a to b with value v.

For instance, it returns 0 if v is a, and returns 1 if v is b, and returns 0.5 if v is exactly between a and b

send_comm(agent, msg)

Sends the object msg to the match communication server after attaching the bot's index and team.

  • agent - An instance of VirxERLU. If you're running this from an instance of VirxERLU, pass in self.
  • msg - A Python object.

peek_generator(generator)

Returns the next value in a generator. If there isn't another value, it will return None.

  • generator - A Python generator.

almost_equals(x, y, threshold)

Returns if x is equals to y within the range of threshold.

  • x - Some value that supports basic math.
  • y - Some value that supports basic math.
  • threshold - Some value that supports basic math.

point_inside_quadrilateral_2d(point, quadrilateral)

Return whether or not a 2d point is inside a 2d quadrilateral

  • point - A Vector. The z coordinate will be ignored.
  • quadrilateral - A tuple of 4 Vectors, in either a clockwise or counter-clockwise order. The z coordinate on each will be ignored.

perimeter_of_ellipse(a, b)

Returns the approximate perimeter of an ellipse.

  • a - A number representing 1 of the 2 radii of the ellipse.
  • b - A number representing the other radius of the ellipse.

dodge_impluse(agent)

Returns the maximum speed gain from a flip or wave dash.

  • agent - An instance of VirxERLU. If you're running this from an instance of VirxERLU, pass in self.

ray_intersects_with_line(origin, direction, point1, point2)

Returns whether or not a ray intersects with a line.

  • origin - The origin of the ray on the field, represented by a Vector.
  • direction - The direction of the ray, represented by a Vector.
  • point1 - The first point of the line, represented by a Vector.
  • point2 - The second point of the line, represented by a Vector.

ray_intersects_with_circle(origin, direction, center, radius)

Returns whether or not a ray intersects with a circle.

  • origin - A Vector representing the origin of the ray on the field.
  • direction - A Vector representing the direction of the ray.
  • center - A Vector representing the center of the circle on the field.
  • radius - A number representing the radius of the circle.

min_non_neg(x, y)

Returns the smallest non-negative value.

  • x - A number.
  • y - A number.

vertex_quadratic_solve_for_x_min_non_neg(a, h, k, y)

Plugs a, h, k and y into a quadratic's vertex form (y = a(x - h)^2 + k) and return the lowest non-negative value of x.

  • a - A number.
  • h - A number.
  • k - A number.
  • y - A number.

get_landing_time(fall_distance, falling_time_until_terminal_velocity, falling_distance_until_terminal_velocity, terminal_velocity, k, h, g)

Returns the time that the object will land on some plane when accounting for gravity and terminal velocity.

  • fall_distance - A number. The distance to some plane.
  • falling_time_until_terminal_velocity - A number. The time until the object reaches terminal velocity.
  • falling_distance_until_terminal_velocity - A number. The distance until the object reaches terminal velocity.
  • terminal_velocity - A number. The terminal velocity of the object.
  • k - A number. The k value in a quadratic's vertex form (y = a(x - h)^2 + k).
  • h - A number. The h value in a quadratic's vertex form (y = a(x - h)^2 + k).
  • g - A number. The current world gravity in the z axis.

find_landing_plane(l, v, g)

Find the plane (side wall, back wall, ceiling, or floor) that the car will collide with first

Return a number. 0 and 1 for the side walls, 2 and 3 for the back walls, 4 for the ceiling and 5 for the floor.