Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions arcade/draw/arc.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def draw_arc_filled(
uncentered_point_list = unrotated_point_list
else:
uncentered_point_list = [
rotate_point(point[0], point[1], 0, 0, tilt_angle) for point in unrotated_point_list
rotate_point(point, (0, 0), tilt_angle) for point in unrotated_point_list
]

point_list = [(point[0] + center_x, point[1] + center_y) for point in uncentered_point_list]
Expand Down Expand Up @@ -132,7 +132,7 @@ def draw_arc_outline(
uncentered_point_list = unrotated_point_list
else:
uncentered_point_list = [
rotate_point(point[0], point[1], 0, 0, tilt_angle) for point in unrotated_point_list
rotate_point(point, (0, 0), tilt_angle) for point in unrotated_point_list
]

point_list = [(point[0] + center_x, point[1] + center_y) for point in uncentered_point_list]
Expand Down
20 changes: 10 additions & 10 deletions arcade/draw/rect.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,16 +349,16 @@ def draw_rect_outline(
)
else:
point_list = (
rotate_point(o_left , o_top , x, y, tilt_angle),
rotate_point(i_left , i_top , x, y, tilt_angle),
rotate_point(o_right , o_top , x, y, tilt_angle),
rotate_point(i_right , i_top , x, y, tilt_angle),
rotate_point(o_right , o_bottom, x, y, tilt_angle),
rotate_point(i_right , i_bottom, x, y, tilt_angle),
rotate_point(o_left , o_bottom, x, y, tilt_angle),
rotate_point(i_left , i_bottom, x, y, tilt_angle),
rotate_point(o_left , o_top , x, y, tilt_angle),
rotate_point(i_left , i_top , x, y, tilt_angle)
rotate_point((o_left , o_top) , (x, y), tilt_angle),
rotate_point((i_left , i_top) , (x, y), tilt_angle),
rotate_point((o_right , o_top) , (x, y), tilt_angle),
rotate_point((i_right , i_top) , (x, y), tilt_angle),
rotate_point((o_right , o_bottom), (x, y), tilt_angle),
rotate_point((i_right , i_bottom), (x, y), tilt_angle),
rotate_point((o_left , o_bottom), (x, y), tilt_angle),
rotate_point((i_left , i_bottom), (x, y), tilt_angle),
rotate_point((o_left , o_top) , (x, y), tilt_angle),
rotate_point((i_left , i_top) , (x, y), tilt_angle)
)
# fmt: on
_generic_draw_line_strip(point_list, color, gl.TRIANGLE_STRIP)
Expand Down
2 changes: 1 addition & 1 deletion arcade/easing.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def ease_position(
"""
Get an easing position
"""
distance = get_distance(start_position[0], start_position[1], end_position[0], end_position[1])
distance = get_distance(start_position, end_position)

if rate is not None:
time = distance / rate
Expand Down
2 changes: 1 addition & 1 deletion arcade/examples/easing_example_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def on_key_press(self, key, modifiers):

def on_mouse_press(self, x: float, y: float, button: int, modifiers: int):
angle = arcade.math.get_angle_degrees(
x1=self.player_sprite.position[0], y1=self.player_sprite.position[1], x2=x, y2=y
self.player_sprite.position, (x, y)
)
self.player_sprite.angle = angle

Expand Down
10 changes: 5 additions & 5 deletions arcade/examples/sprite_rotate_around_tank.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ def rotate_around_point(self, point: Point, degrees: float):

# Move the sprite along a circle centered around the passed point
self.position = rotate_point(
self.center_x, self.center_y,
point[0], point[1], degrees)
(self.center_x, self.center_y),
point, degrees) # type: ignore

def face_point(self, point: Point):
self.angle = get_angle_degrees(*self.position, *point)
self.angle = get_angle_degrees(self.position, point) # type: ignore


class GameView(arcade.View):
Expand Down Expand Up @@ -209,8 +209,8 @@ def correct(self, correct: bool):
self._correct = correct
if correct:
angle = get_angle_radians(
self.tank.center_y, self.tank.center_x,
self.mouse_pos[1], self.mouse_pos[0])
(self.tank.center_y, self.tank.center_x),
(self.mouse_pos[1], self.mouse_pos[0]))

self.barrel.position = (
self.barrel.center_x + math.sin(angle) * TANK_BARREL_LENGTH_HALF,
Expand Down
2 changes: 1 addition & 1 deletion arcade/examples/turn_and_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def update(self, delta_time: float = 1 / 60):
# Do math to calculate how to get the sprite to the destination.
# Calculation the angle in radians between the start points
# and end points. This is the angle the player will travel.
target_angle = arcade.math.get_angle_degrees(start_x, start_y, dest_x, dest_y)
target_angle = arcade.math.get_angle_degrees((start_x, start_y), (dest_x, dest_y))
current_angle = self.angle - IMAGE_ROTATION

new_angle = arcade.math.lerp_angle(current_angle, target_angle, self.rot_speed)
Expand Down
58 changes: 23 additions & 35 deletions arcade/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,38 +311,32 @@ def rand_vec_magnitude(
return vel.x, vel.y


def get_distance(x1: float, y1: float, x2: float, y2: float) -> float:
def get_distance(pos1: Point2, pos2: Point2) -> float:
"""
Get the distance between two points.

Args:
x1 (float): x coordinate of the first point
y1 (float): y coordinate of the first point
x2 (float): x coordinate of the second point
y2 (float): y coordinate of the second point
pos1 (Point2): the first point
pos2 (Point2): the second point
"""
return math.hypot(x1 - x2, y1 - y2)
return math.hypot(pos1[0] - pos2[0], pos1[1] - pos2[1])


def rotate_point(
x: float,
y: float,
cx: float,
cy: float,
point: Point2,
center: Point2,
angle_degrees: float,
) -> Point2:
"""
Rotate a point around a center.

Args:
x (float): x value of the point you want to rotate
y (float): y value of the point you want to rotate
cx (float): x value of the center point you want to rotate around
cy (float): y value of the center point you want to rotate around
point (Point2): the point you want to rotate
center (Point2): the center point you want to rotate around
angle_degrees (float): Angle, in degrees, to rotate
"""
temp_x = x - cx
temp_y = y - cy
temp_x = point[0] - point[0]
temp_y = point[1] - center[1]

# now apply rotation
angle_radians = math.radians(angle_degrees)
Expand All @@ -352,8 +346,8 @@ def rotate_point(
rotated_y = -temp_x * sin_angle + temp_y * cos_angle

# translate back
x = round(rotated_x + cx, _PRECISION)
y = round(rotated_y + cy, _PRECISION)
x = round(rotated_x + center[0], _PRECISION)
y = round(rotated_y + center[1], _PRECISION)

return x, y

Expand Down Expand Up @@ -424,34 +418,28 @@ def rotate_around_point(source: Point2, target: Point2, angle: float):
return target[0] + dx, target[1] + dy


def get_angle_degrees(x1: float, y1: float, x2: float, y2: float) -> float:
def get_angle_degrees(pos1: Point2, pos2: Point2) -> float:
"""
Get the angle in degrees between two points.

Args:
x1 (float): x coordinate of the first point
y1 (float): y coordinate of the first point
x2 (float): x coordinate of the second point
y2 (float): y coordinate of the second point
pos1 (Point2): the first point
pos2 (Point2): the second point
"""
x_diff = x2 - x1
y_diff = y2 - y1
return -math.degrees(math.atan2(y_diff, x_diff))
return math.degrees(get_angle_radians(pos1, pos2))


def get_angle_radians(x1: float, y1: float, x2: float, y2: float) -> float:
def get_angle_radians(pos1: Point2, pos2: Point2) -> float:
"""
Get the angle in radians between two points.

Args:
x1 (float): x coordinate of the first point
y1 (float): y coordinate of the first point
x2 (float): x coordinate of the second point
y2 (float): y coordinate of the second point
"""
x_diff = x2 - x1
y_diff = y2 - y1
return math.atan2(x_diff, y_diff)
pos1 (Point2): the first point
pos2 (Point2): the second point
"""
x_diff = pos2[0] - pos1[0]
y_diff = pos2[1] - pos1[1]
return math.atan2(y_diff, x_diff)


def quaternion_rotation(axis: Point3, vector: Point3, angle: float) -> tuple[float, float, float]:
Expand Down
2 changes: 1 addition & 1 deletion arcade/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def has_line_of_sight(
if check_resolution <= 0:
raise ValueError("check_resolution must be greater than zero")

distance = get_distance(observer[0], observer[1], target[0], target[1])
distance = get_distance(observer, target)
if distance == 0:
return True

Expand Down
4 changes: 3 additions & 1 deletion arcade/physics_engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def _move_sprite(
# Resolve any collisions by this weird kludge
_wiggle_until_free(moving_sprite, can_collide)
if (
get_distance(original_x, original_y, moving_sprite.center_x, moving_sprite.center_y)
get_distance(
(original_x, original_y), (moving_sprite.center_x, moving_sprite.center_y)
)
> max_distance
):
# Ok, glitched trying to rotate. Reset.
Expand Down
14 changes: 7 additions & 7 deletions arcade/shape_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,10 @@ def get_rectangle_points(
y4 = -height / 2 + center_y

if tilt_angle:
x1, y1 = rotate_point(x1, y1, center_x, center_y, tilt_angle)
x2, y2 = rotate_point(x2, y2, center_x, center_y, tilt_angle)
x3, y3 = rotate_point(x3, y3, center_x, center_y, tilt_angle)
x4, y4 = rotate_point(x4, y4, center_x, center_y, tilt_angle)
x1, y1 = rotate_point((x1, y1), (center_x, center_y), tilt_angle)
x2, y2 = rotate_point((x2, y2), (center_x, center_y), tilt_angle)
x3, y3 = rotate_point((x3, y3), (center_x, center_y), tilt_angle)
x4, y4 = rotate_point((x4, y4), (center_x, center_y), tilt_angle)

return [(x1, y1), (x2, y2), (x3, y3), (x4, y4)]

Expand Down Expand Up @@ -505,7 +505,7 @@ def create_rectangle(
if tilt_angle != 0:
point_list_2: list[Point] = []
for point in data:
new_point = rotate_point(point[0], point[1], center_x, center_y, tilt_angle)
new_point = rotate_point(point, (center_x, center_y), tilt_angle) # type: ignore
point_list_2.append(new_point)
data = point_list_2

Expand Down Expand Up @@ -752,7 +752,7 @@ def create_ellipse(
y = height / 2 * math.sin(theta) + center_y

if tilt_angle:
x, y = rotate_point(x, y, center_x, center_y, tilt_angle)
x, y = rotate_point((x, y), (center_x, center_y), tilt_angle)

point_list.append((x, y))

Expand Down Expand Up @@ -812,7 +812,7 @@ def create_ellipse_filled_with_colors(
y = height * math.sin(theta) + center_y

if tilt_angle:
x, y = rotate_point(x, y, center_x, center_y, tilt_angle)
x, y = rotate_point((x, y), (center_x, center_y), tilt_angle)

point_list.append((x, y))
point_list.append(point_list[1])
Expand Down
2 changes: 1 addition & 1 deletion arcade/sprite_list/collision.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_distance_between_sprites(sprite1: SpriteType, sprite2: SpriteType) -> fl
sprite1: Sprite one
sprite2: Sprite two
"""
return get_distance(*sprite1._position, *sprite2._position)
return get_distance(sprite1._position, sprite2._position)


def get_closest_sprite(
Expand Down
6 changes: 3 additions & 3 deletions arcade/texture/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def transform_hit_box_points(
"""
Transform hit box points by rotating them 90 degrees clockwise.
"""
return tuple(rotate_point(point[0], point[1], 0, 0, 90) for point in points)
return tuple(rotate_point(point, (0, 0), 90) for point in points)


class Rotate180Transform(Transform):
Expand All @@ -135,7 +135,7 @@ def transform_hit_box_points(
"""
Transform hit box points by rotating them 180 degrees clockwise.
"""
return tuple(rotate_point(point[0], point[1], 0, 0, 180) for point in points)
return tuple(rotate_point(point, (0, 0), 180) for point in points)


class Rotate270Transform(Transform):
Expand All @@ -157,7 +157,7 @@ def transform_hit_box_points(
"""
Transform hit box points by rotating them 270 degrees clockwise.
"""
return tuple(rotate_point(point[0], point[1], 0, 0, 270) for point in points)
return tuple(rotate_point(point, (0, 0), 270) for point in points)


class FlipLeftRightTransform(Transform):
Expand Down
2 changes: 1 addition & 1 deletion arcade/tilemap/tilemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ def _process_object_layer(

angle_degrees = math.degrees(rotation)
rotated_center_x, rotated_center_y = rotate_point(
width / 2, height / 2, 0, 0, angle_degrees
(width / 2, height / 2), (0, 0), angle_degrees
)

my_sprite.position = (x + rotated_center_x, y + rotated_center_y)
Expand Down
Loading