-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdql_orbit_util.py
61 lines (47 loc) · 1.74 KB
/
dql_orbit_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'''
This code for 2D gravity physics was generated by GPT-3.5 ("ChatGPT").
Prompt given: "Given two objects in 2D space, write a Python script which simulates gravity between them based on their masses and distances."
'''
import math
class Object2D:
def __init__(self, mass, x, y, vx=0, vy=0):
self.mass = mass
self.x = x
self.y = y
self.vx = vx
self.vy = vy
def calculate_gravitational_force(obj1, obj2, G=6.67430e-11):
dx = obj2.x - obj1.x
dy = obj2.y - obj1.y
distance_squared = dx ** 2 + dy ** 2
distance = math.sqrt(distance_squared)
# Prevent division by zero
if distance_squared == 0:
return 0, 0
force_magnitude = G * obj1.mass * obj2.mass / distance_squared
force_x = force_magnitude * dx / distance
force_y = force_magnitude * dy / distance
return force_x, force_y
def update_position(obj, force_x, force_y, dt):
acceleration_x = force_x / obj.mass
acceleration_y = force_y / obj.mass
obj.vx += acceleration_x * dt
obj.vy += acceleration_y * dt
obj.x += obj.vx * dt
obj.y += obj.vy * dt
def simulate_gravity(obj1, obj2, num_steps, time_step, G=6.67430e-11):
for _ in range(num_steps):
force_x, force_y = calculate_gravitational_force(obj1, obj2, G=G)
update_position(obj1, force_x, force_y, time_step)
update_position(obj2, -force_x, -force_y, time_step)
if __name__ == "__main__":
# Define objects
obj1 = Object2D(mass=1000, x=0, y=0)
obj2 = Object2D(mass=500, x=50, y=0)
# Simulate gravity
num_steps = 1000
time_step = 0.1
simulate_gravity(obj1, obj2, num_steps, time_step)
print("Final positions:")
print("Object 1:", obj1.x, obj1.y)
print("Object 2:", obj2.x, obj2.y)