Python Forum
Collisions for items in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Collisions for items in a list
#1
Hi,
I am attempting to make a simple ideal gas simulation.
I have created particles in a list as such, where the user will input n:
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
import random
import math
import turtle as tt
no_particles=int(n)
ball=[]
for i in range(no_particles):
    ball.append(tt.Turtle())
    ball[i]= tt.Turtle()
    ball[i].speed(0)
    ball[i].shape("circle")
    ball[i].color("White")
    ball[i].penup()
    ball[i].goto(((right_edge*random.random())-(right_edge/2)),(top_edge*random.random())-(top_edge/2))
        ball[i].dx = random.random()*3
        ball[i].dy = random.random()*3
 
However I am struggling to detect collisions of these particles, I have attempted using the code below
 
while True:
    for i in range(no_particles):
        ball[i].setx(ball[i].xcor() + ball[i].dx)
        ball[i].sety(ball[i].ycor() + ball[i].dy)
 
        if distance < 25:
            collision_counter +=1
            counter.clear()
            counter.write("Collison counter: {}".format(collision_counter), align="left",
                font=("Courier", vhalf, "normal"))
            ball[0].dx *= -1
            ball[0].dy *= -1
            ball[1].dy *= -1
            ball[1].dx *= -1
However no collisions will be detected, does anyone have any thoughts on how I could achieve this?
Larz60+ write Apr-06-2021, 03:29 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
Please wrap code with Python tags to retain indenting.

I don't see where you calculate distance.
Reply
#3
(Apr-06-2021, 02:40 PM)deanhystad Wrote: Please wrap code with Python tags to retain indenting.

I don't see where you calculate distance.

Sorry about the code, this is the whole lot,

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import random
import math
R=8.3145
V=float(input("What is the volume of your vessel in metres cubed?: "))
T=float(input("What is the temperature of the gas in Kelvin?: "))
P=float(input("What is the pressure of the gas in Pascals?: "))
n=(P*V)/(R*T)
 
 
#variaball
vessel_height=V*30
vessel_width=V*40
import turtle as tt
wn=tt.Screen()
 
wn.title("Ideal Gas Simulator")
wn.bgcolor("Black")
wn.setup(width=vessel_width, height=vessel_height)
 
wn.tracer(0)
 
#Create a vessel
pen=tt.Turtle()
pen.shape("square")
pen.color("white")
pen.speed(0)
pen.penup()
pen.hideturtle()
pen.goto((-0.5*vessel_width)+(2*V),(-0.5*vessel_height)+(2*V))
pen.pendown()
pen.setheading(0)
 
for i in range (2):
    pen.forward(vessel_width-(4*V))
    pen.left(90)
    pen.forward(vessel_height-(4*V))
    pen.left(90)
v=int(V)
pen.penup()
pen.goto(0, (vessel_height/2)-(2*V))
pen.write("Simulation of an ideal gas", align="center",
          font=("arial", v, "normal"))
 
 
#Screen size variables
left_edge=(-0.5*vessel_width)+(2*V)
right_edge=(0.5*vessel_width)-(2*V)
top_edge=(0.5*vessel_height)-(2*V)
bottom_edge=(-0.5*vessel_height)+(2*V)
 
#collison counter
vhalf=int(v/2)
collision_counter = 0
counter=tt.Turtle()
counter.speed(0)
counter.shape("square")
counter.color("white")
counter.penup()
counter.hideturtle()
counter.goto(left_edge, top_edge)
counter.write("Collision counter: 0", align="left",
           font=("arial", vhalf, "normal"))
#mole counter
moles=tt.Turtle()
moles.speed(0)
moles.shape("square")
moles.color("white")
moles.penup()
moles.hideturtle()
moles.goto(right_edge, top_edge)
moles.write("Number of moles: {}".format(n), align="right",
           font=("arial", vhalf, "normal"))
 
if n<2:
    n=2
     
no_particles=int(n)
ball=[]
for i in range(no_particles):
    ball.append(tt.Turtle())
    ball[i]= tt.Turtle()
    ball[i].speed(0)
    ball[i].shape("circle")
    ball[i].color("White")
    ball[i].penup()
    ball[i].goto(((right_edge*random.random())-(right_edge/2)),(top_edge*random.random())-(top_edge/2))
    if T > 600:
        ball[i].dx = random.random()*3
        ball[i].dy = random.random()*3
 
    elif T < 250:
        ball[i].dx=random.random()*1
        ball[i].dy=random.random()*1
     
    else:
        ball[i].dx = random.random()*2
        ball[i].dy = random.random()*2
 
distance = math.sqrt((ball[0].xcor()*ball[0].xcor())+(ball[1].ycor()*ball[1].ycor()))
             
while True:
    for i in range(no_particles):
        ball[i].setx(ball[i].xcor() + ball[i].dx)
        ball[i].sety(ball[i].ycor() + ball[i].dy)
 
        if ball[i].ycor()> (top_edge-10):
            collision_counter +=1
            counter.clear()
            counter.write("Collison counter: {}".format(collision_counter), align="left",
                font=("Courier", vhalf, "normal"))
            ball[i].sety(top_edge-10)
            ball[i].dy *= -1
 
        if ball[i].ycor()< (bottom_edge+10):
            collision_counter +=1
            counter.clear()
            counter.write("Collison counter: {}".format(collision_counter), align="left",
                font=("Courier", vhalf, "normal"))
            ball[i].sety(bottom_edge+10)
            ball[i].dy *= -1
 
        if ball[i].xcor() > (right_edge-10):
            collision_counter +=1
            counter.clear()
            counter.write("Collison counter: {}".format(collision_counter), align="left",
                font=("Courier", vhalf, "normal"))
            ball[i].setx(right_edge-10)
            ball[i].dx *= -1
 
        if ball[i].xcor() < (left_edge+10):
            collision_counter +=1
            counter.clear()
            counter.write("Collison counter: {}".format(collision_counter), align="left",
                font=("Courier", vhalf, "normal"))
            ball[i].setx(left_edge+10)
            ball[i].dx *= -1
 
        if distance < 50:
            collision_counter +=1
            counter.clear()
            counter.write("Collison counter: {}".format(collision_counter), align="left",
                font=("Courier", vhalf, "normal"))
            ball[0].dx *= -1
            ball[0].dy *= -1
            ball[1].dy *= -1
            ball[1].dx *= -1
 
         
 
 
 
    wn.update()
Reply
#4
You need to calculate distance each time you move the particles.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 2 2,163 Jul-26-2024, 06:59 AM
Last Post: AlinaHabba24
  Removing items in a list cap510 3 3,297 Nov-01-2020, 09:53 PM
Last Post: cap510
  Help with Recursive solution,list items gianniskampanakis 8 5,105 Feb-28-2020, 03:36 PM
Last Post: gianniskampanakis
  Removing items from list slackerman73 8 6,132 Dec-13-2019, 05:39 PM
Last Post: Clunk_Head
  Find 'greater than' items in list johneven 2 5,474 Apr-05-2019, 07:22 AM
Last Post: perfringo
  How to add items within a list Mrocks22 2 3,532 Nov-01-2018, 08:46 PM
Last Post: Mrocks22
  How to keep duplicates and remove all other items in list? student8 1 5,699 Oct-28-2017, 05:52 AM
Last Post: heiner55
  Help printing any items that contains a keyword from a list Liquid_Ocelot 13 96,319 May-06-2017, 10:41 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020