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:
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,

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
  Removing items in a list cap510 3 2,308 Nov-01-2020, 09:53 PM
Last Post: cap510
  Help with Recursive solution,list items gianniskampanakis 8 3,506 Feb-28-2020, 03:36 PM
Last Post: gianniskampanakis
  Removing items from list slackerman73 8 4,342 Dec-13-2019, 05:39 PM
Last Post: Clunk_Head
  Find 'greater than' items in list johneven 2 4,430 Apr-05-2019, 07:22 AM
Last Post: perfringo
  How to add items within a list Mrocks22 2 2,640 Nov-01-2018, 08:46 PM
Last Post: Mrocks22
  How to keep duplicates and remove all other items in list? student8 1 4,914 Oct-28-2017, 05:52 AM
Last Post: heiner55
  Help printing any items that contains a keyword from a list Liquid_Ocelot 13 64,586 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