forked from virtualeiro/pygame-imfvj2-2023-24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaula9_buoyancy.py
91 lines (70 loc) · 2.75 KB
/
aula9_buoyancy.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import pygame
import random
from pygame.math import Vector2
# Initialize Pygame
pygame.init()
# Set up the display
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Buoyancy Simulation")
# Colors
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Rectangle parameters
rectangle_width = 200
rectangle_height = 100
rectangle_x = width // 2 - rectangle_width // 2
rectangle_y = 0 # Starts at the top
# Initial position and velocity
position = Vector2(rectangle_x, rectangle_y)
velocity = Vector2(0, 0)
# Buoyancy parameters
water_level = height * 2 // 3 # Initial position of the water level
water_density = 4.5 # Density of water (higher value for denser water)
gravity = 9.8
# Mass of the red rectangle
rectangle_mass = 700 # Smaller mass
FPS=60
# Game loop
running = True
clock = pygame.time.Clock()
buoyant_force=0
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if position.y + rectangle_height < water_level: # Rectangle is in the free-fall phase
# Calculate the net force
net_force = Vector2(0, rectangle_mass * gravity)
# Apply the net force to the rectangle's position
acceleration = net_force / rectangle_mass
position += acceleration
else: # Rectangle is in water => in the buoyancy phase
# Calculate the submerged depth
submerged_depth = ((position.y + rectangle_height) - water_level)
# Calculate the buoyant force
#----submerged_volume=submerged_depth/rectangle_height
submerged_volume=submerged_depth*rectangle_width*0.01# Convert pixels^2 to m^2
#Version 1
buoyant_force = water_density * submerged_volume * gravity
# Calculate the net force
net_force = Vector2(0, rectangle_mass * gravity - buoyant_force)
# Apply the net force to the rectangle's position
acceleration = net_force / rectangle_mass
velocity += acceleration/FPS
# Limit the buoyant force so the rectangle floats correctly
if position.y + rectangle_height > water_level + rectangle_height * 0.9:
position.y = water_level + rectangle_height * 0.9 - rectangle_height
velocity.y = 0
position+=velocity
# Draw the scene
screen.fill((0, 0, 0)) # Fill the screen with black color
pygame.draw.rect(screen, RED, (position.x, position.y, rectangle_width, rectangle_height))
pygame.draw.line(screen, BLUE, (0, water_level), (width, water_level), 3)
# Update the display
pygame.display.flip()
# Limit the frame rate
clock.tick(FPS)
# Quit the game
pygame.quit()