Function for rotating 2d objects?
Categories:
Mastering 2D Object Rotation in Python for Game Development and Physics

Learn how to implement robust 2D object rotation functions in Python, covering both point and object rotation around arbitrary pivots, essential for game physics and simulations.
Rotating 2D objects is a fundamental operation in many applications, from game development and computer graphics to physics simulations and robotics. Understanding how to correctly implement rotation functions is crucial for creating dynamic and realistic behaviors. This article will guide you through the mathematical principles and Python implementations for rotating 2D points and entire objects around a specified pivot point.
Understanding 2D Rotation Mathematics
At its core, 2D rotation involves transforming a point's coordinates (x, y) to new coordinates (x', y') based on an angle and a pivot. The standard rotation formulas assume rotation around the origin (0,0). If you need to rotate around an arbitrary pivot point (px, py), you must first translate the point so that the pivot becomes the origin, perform the rotation, and then translate it back.
The formulas for rotating a point (x, y) around the origin by an angle theta
(in radians) are:
x' = x * cos(theta) - y * sin(theta) y' = x * sin(theta) + y * cos(theta)
When rotating around an arbitrary pivot (px, py), the steps are:
- Translate the point:
temp_x = x - px
,temp_y = y - py
- Rotate the translated point around the origin:
rotated_x = temp_x * cos(theta) - temp_y * sin(theta)
,rotated_y = temp_x * sin(theta) + temp_y * cos(theta)
- Translate the rotated point back:
x' = rotated_x + px
,y' = rotated_y + py
flowchart TD A[Original Point (x, y)] --> B{Translate to Origin (x-px, y-py)} B --> C{Rotate Around Origin (theta)} C --> D{Translate Back (x'+px, y'+py)} D --> E[New Point (x', y')]
Process for rotating a point around an arbitrary pivot.
Implementing a Point Rotation Function
Let's create a Python function that takes a point, a pivot, and an angle, and returns the new rotated point. We'll use the math
module for trigonometric functions, ensuring the angle is in radians.
import math
def rotate_point(point_x, point_y, pivot_x, pivot_y, angle_degrees):
"""
Rotates a 2D point around a specified pivot point by a given angle.
Args:
point_x (float): The x-coordinate of the point to rotate.
point_y (float): The y-coordinate of the point to rotate.
pivot_x (float): The x-coordinate of the pivot point.
pivot_y (float): The y-coordinate of the pivot point.
angle_degrees (float): The rotation angle in degrees.
Returns:
tuple: A tuple (new_x, new_y) representing the rotated point.
"""
# Convert angle from degrees to radians
angle_radians = math.radians(angle_degrees)
# Translate point so pivot is at origin
temp_x = point_x - pivot_x
temp_y = point_y - pivot_y
# Rotate point around origin
rotated_x = temp_x * math.cos(angle_radians) - temp_y * math.sin(angle_radians)
rotated_y = temp_x * math.sin(angle_radians) + temp_y * math.cos(angle_radians)
# Translate point back
new_x = rotated_x + pivot_x
new_y = rotated_y + pivot_y
return new_x, new_y
# Example Usage:
original_point = (10, 5)
pivot_point = (5, 5)
rotation_angle = 90 # degrees
rotated_p = rotate_point(original_point[0], original_point[1],
pivot_point[0], pivot_point[1],
rotation_angle)
print(f"Original Point: {original_point}")
print(f"Pivot Point: {pivot_point}")
print(f"Rotation Angle: {rotation_angle} degrees")
print(f"Rotated Point: {rotated_p}")
# Expected output for 90 degrees rotation around (5,5):
# (10,5) -> (5,5) is (5,0) relative
# Rotated (5,0) by 90 deg is (0,5) relative
# (0,5) relative to (5,5) is (5,10)
# Output: Rotated Point: (5.0, 10.0)
math.sin()
and math.cos()
. If your input is in degrees, use math.radians()
for conversion.Rotating a 2D Object (e.g., a Polygon)
To rotate an entire 2D object, such as a polygon defined by a list of vertices, you simply apply the rotate_point
function to each vertex of the object. The pivot point for the object's rotation is typically its center or a specific point on the object.
Consider a simple square defined by its four corner vertices. We can rotate this square around its geometric center.
import math
def rotate_point(point_x, point_y, pivot_x, pivot_y, angle_degrees):
# (Same rotate_point function as above)
angle_radians = math.radians(angle_degrees)
temp_x = point_x - pivot_x
temp_y = point_y - pivot_y
rotated_x = temp_x * math.cos(angle_radians) - temp_y * math.sin(angle_radians)
rotated_y = temp_x * math.sin(angle_radians) + temp_y * math.cos(angle_radians)
new_x = rotated_x + pivot_x
new_y = rotated_y + pivot_y
return new_x, new_y
def rotate_object(vertices, pivot_x, pivot_y, angle_degrees):
"""
Rotates a 2D object (defined by a list of vertices) around a specified pivot.
Args:
vertices (list of tuples): A list of (x, y) tuples representing the object's vertices.
pivot_x (float): The x-coordinate of the pivot point for the object.
pivot_y (float): The y-coordinate of the pivot point for the object.
angle_degrees (float): The rotation angle in degrees.
Returns:
list of tuples: A new list of (new_x, new_y) tuples for the rotated object.
"""
rotated_vertices = []
for point_x, point_y in vertices:
new_x, new_y = rotate_point(point_x, point_y, pivot_x, pivot_y, angle_degrees)
rotated_vertices.append((new_x, new_y))
return rotated_vertices
# Example Usage: Rotating a square
# Define a square's vertices
square_vertices = [
(0, 0),
(10, 0),
(10, 10),
(0, 10)
]
# Calculate the center of the square to use as a pivot
# For a simple square, this is (min_x + max_x)/2, (min_y + max_y)/2
pivot_x_square = (0 + 10) / 2 # 5
pivot_y_square = (0 + 10) / 2 # 5
rotation_angle_square = 45 # degrees
rotated_square = rotate_object(square_vertices,
pivot_x_square, pivot_y_square,
rotation_angle_square)
print(f"\nOriginal Square Vertices: {square_vertices}")
print(f"Pivot for Square: ({pivot_x_square}, {pivot_y_square})")
print(f"Rotation Angle for Square: {rotation_angle_square} degrees")
print(f"Rotated Square Vertices: {rotated_square}")