multiple balls in a box with collision detection - python

What the problem is
I have to simulate balls in a box of 1 by 1.
i have to give the ball a random speed and a random angle it takes off in.
however when i plot the balls they go straight and have an asymptote at 0.25.
i double cheked my math but i cant find the problem?
My code
import numpy
import matplotlib.pyplot as plt
import math
import random
def rand_tuples(aantalballen): # here i make a function that randomly makes a list with lists in #
list_balls = [] # the list, every index is a ball with x_pos , y_pos , speed , rotation #
for i in range(aantalballen):
list_balls.append([0.25, 0.75,0.1* random.random(), 2* math.pi*random.random()])
return list_balls
def ball_step(list_balls, dt): # this function calculates the steps the balls takes #
x = list_balls[0]
y = list_balls[1]
vx = math.cos(list_balls[3]) * list_balls[2] # speed in the x direction #
vy = math.sin(list_balls[3]) * list_balls[2] # speed in the y direction #
dt = dt
x_pos = x + (vx * dt) # calculates the actual positions #
y_pos = y + (vy * dt)
if x_pos <= 0 or x_pos >= 1: # collision detection #
vx = -vx
if y_pos <= 0 or y_pos >= 1:
vy = -vy
return x_pos, y_pos, vx , vy # returns the new positions but the same speed so that they can be used again#
def move_ball(ballen, tijd, dt): # takes a list of balls, time they move and the time steps they take#
positions_balls = { } # hold my position in {0:{'x':[positions}, 'y':{...}}}
time_1 = 0
for i in range(len(ballen)) :
positions_balls[i] = None # make a key with empty value #
time_1 = 0
cordinates = {'x':[], 'y':[]} # make the dictionary where my values go into #
while time_1 < tijd:
bal = ball_step(ballen[i] , dt) # call bal step to calculate my x and y position #
ballen[i] = bal
cordinates['x'].append(bal[0])
cordinates['y'].append(bal[1])
time_1 += dt
if int(time_1) == tijd:
positions_balls[i] = cordinates # finally add the dictionary to my main dictionary #
print positions_balls
return positions_balls
dic = move_ball(rand_tuples(30), 3, 0.01)
plt.plot(dic[0]['x'], dic[0]['y'])
plt.show()
i do not have enough reputation to post a picture of the plot :(

ball_step() takes in (I think; comments are your friend) x, y, speed, rotation. It outputs x, y, speed x, speed y. That's assigned back to the original list.

Related

Simulating the trajectory of a particle from rest under gravity

I hope you can help! I need to show the trajectory of a particle that is under gravity of g = -9.81 m/s2 and time step of dt = 0.05 sec, where the position and velocity of the particle are:
x_1 = x_0 + v_x1 * dt
y_1 = y_0 + v_y1 * dt
v_x1 = v_x0
v_y1 = v_y0 + g * dt
This is what I should achieve:
This is what I've done so far:
import numpy as np
import matplotlib.pyplot as plt
plt.figure(1, figsize=(12,12))
ax = plt.subplot(111, aspect='equal')
ax.set_ylim(0,50)
ax.set_title('Boom --- Weeee! --- Ooof')
r = np.array([0.,0.,15.,30.])
g = -9.81
dt = 0.05
y = 0
x = 0
while y > 0:
plt.plot(x_1,y_2,':', ms=2)
x_1 = v_x1 * dt
y_1 = v_y1 * dt
v_x1 = v_x0
v_y1 = v_y0 + g * dt
This doesn't produce an image only the plt.figure stated in the beginning, I've tried to integrate the r vector into the loop but I can't figure out how.
Thank you.
Here's a modified version of your code that I believe gives you the result you desire (you may want to choose different initial velocity values):
import matplotlib.pyplot as plt
# Set up our plot surface
plt.figure(1, figsize=(12,12))
ax = plt.subplot()
# ax = plt.subplot(111, aspect='equal')
# ax.set_ylim(0,50)
ax.set_title('Boom --- Weeee! --- Ooof')
# Initial conditions
g = -9.81
dt = 0.05
y = 0
x = 0
v_x = 5
v_y = 5
# Create our lists that will store our data points
points_x = []
points_y = []
while True:
# Add the current position of our projectile to our data
points_x.append(x)
points_y.append(y)
# Move our projectile along
x += v_x * dt
y += v_y * dt
# If our projectile falls below the X axis (y < 0), end the simulation
if y < 0:
break
# Update our y velocity per gravity
v_y = v_y + g * dt
# Plot our data
ax.plot(points_x, points_y)
# Show the plot on the screen
plt.show()
I'm sorry if I could have made fewer changes. Here are the substantive ones I can think of:
You weren't using the r value you computed, so got rid of it, along with the import of numpy that was then no longer needed.
I took out calls you made explicitly size your plot. You're better off letting the plot library decide upon the bounds of the plot for you
I don't know if there's another way to do it, but I've always supplied data to the plotting library as arrays of points rather than by providing the points one at a time. So here, I collect up all of the x and y coordinates into two lists while running the simulation, and then add those arrays to the plot at the end to plot the data.
The x_0 vs x_1, etc., got confusing for me. I didn't see any reason to keep track of multiple position and velocity values, so I reduced the code down to using just one set of positions and velocities, x, y, v_x and v_y.
See the comments for more info
Result:

Plotting a graph of iterations in Python?

I've written the following piece of code using the Euler method to plot a graph of velocity against time for a free-falling object, which eventually reaches terminal velocity:
import numpy as np
import matplotlib.pyplot as plt
g = -9.81
Cd = 0.47
p0 = 1.2
A = 0.0034
k = (Cd*p0*A)/2
m = 0.058
def euler(vyn, yn): #takes the starting velocity and height as arguments
i = 0
while i < 10000:
dt = 0.01
i += 1
vyn = vyn - dt * (g + ((k/m) * abs(vyn) * vyn))
yn = yn - dt * vyn
print(vyn, yn)
if yn < 0: #stops iterations when ball hits ground
break
return vyn
NumPoints = 2000
xmin = 0
xmax = 1000
dx = (xmax - xmin) / (NumPoints - 1)
xvals = [0.0] * NumPoints
yvals = np.zeros(NumPoints)
for i in range(NumPoints):
xvals[i] = xmin + i * dx
yvals[i] = euler(0,1000)
plt.plot(xvals,yvals)
plt.show()
I put the print statement in the while loop by the way so that it prints the velocity and height of object from the ground each iteration, and the break is so that it breaks out of the loop by the time the ball hits the ground. However, when I add the graphing code at the bottom, the iterations loop back round for some reason, without breaking. A graph is also not printed.
Removing the print statement does produce a graph, however it is just a horizontal line.
How could I modify the code to produce a graph of velocity against time, and also height against time? Do I need to embed the graph code within the while loop?
Thanks

Using code to write a function that calculates values for Projectile motion

I have an assignment that asks me to use some given code to write a function which calculates the angle needed to hit a target 10 metres away.
here is the given code:
from visual import *
from visual.graph import * # For the graphing functions
#Create a graph display window (gdisplay)
win = gdisplay(xtitle="Distance [m]", ytitle="Height [m]")
#And a curve on this display
poscurve = gcurve(gdisplay=win, color=color.cyan)
#Target position (10 meters away)
target_pos = vector(10,0,0)
#Set the starting angle (in degrees)
angle = 45
#Set the magnitude of the starting velocity (in m/s)
v0 = 12.0
#Gravity vector (m/s**2)
g = vector(0, -9.8, 0)
#Create a vector for the projectile's velocity
velocity = v0 * vector(cos(anglepi/180), sin(anglepi/180), 0)
#and the position
position = vector(0,0,0)
dt = 0.01 # Time step
#Start loop. Each time taking a small step in time
while (position.y > 0) or (velocity.y > 0): # Change in position # dx = (dx/dt) * dt dx = velocity * dt
# Change in velocity
# dv = (dv/dt) * dt
dv = g * dt
# Update the position and velocity
position = position + dx
velocity = velocity + dv
# Plot the current position
poscurve.plot(pos=position)
#When loop finishes, velocity.y must be < 0, and position.y < 0
print "Landed at X position: ", position.x print "X distance to target: ", position.x - target_pos.x
How would I now write a function to calculate the required value? I have no idea where to start, any help would be greatly appreciated!
Thanks
You could use maths to work out an equation for the result.
This works out as:
range = 2v^2/g *cos(a)sin(a)
where v=initial velocity
a=angle
g=gravitational acceleration
You can use this python script to find the answer:
from math import cos
from math import sin
from math import radians
from math import fabs
a=0 # angle in degrees
target=10 # How far you want it to go in m
v=12 # initial velocity in m/s
g=9.81 #gravitational acceleration m/s/s
best_angle=None
nearest_answer=None
while a<45: # we only need to check up to 45 degrees
r = 2*v*v/g*cos(radians(a))*sin(radians(a))
if not nearest_answer or fabs(r-target)<fabs(nearest_answer-target):
nearest_answer = r
best_angle = a
print("{0} -> {1}".format(a,r))
a+=.1 # try increasing the angle a bit. The lower this is the more accurate the answer will be
print("Best angle={}".format(best_angle))

drawing a slope in random circles python

So my program is designed to manipulate certain coordinates in order to create this image:
So basically my program draw a bunch of random circles and I have to manipulate the line of equation to create the red sections. So far my image is the following:
I can't seem to figure out how to add another line equation to create the other red section. Any help would be greatly appreciated!
# using the SimpleGraphics library
from SimpleGraphics import *
# tell SimpleGraphics to only draw when I use the update() function
setAutoUpdate(False)
# use the random library to generate random numbers
import random
# size of the circles drawn
diameter = 15
resize(600, 400)
##
# returns a vaid color based on the input coordinates
#
# #param x is an x-coordinate
# #param y is a y-coordinate
# #return a colour based on the input x,y values for the given flag
##
def define_colour(x,y):
##
#add your code to this method and change the return value
slopeOne = (200 - 300)/(0-150)
b = 0 - (slopeOne * 200)
slopeTwo = (0-300)/(200 - 800)
b = 150 - (slopeTwo * 40)
lineEquationOne = (slopeOne * x) + b
lineEquationTwo = (slopeTwo * x) + b
if y > lineEquationOne:
return "red"
elif y > lineEquationTwo:
return "red"
else:
return 'white'
######################################################################
#
# Do NOT change anything below this line
#
######################################################################
# repeat until window is closed
while not closed():
for i in range(500):
# generate random x and y values
x = random.randint(0, getWidth())
y = random.randint(0, getHeight())
# set colour for current circle
setFill( define_colour(x,y) )
# draw the current circle
ellipse(x, y, diameter, diameter)
update()
You're almost there. Add back in the commented lines for your second slope and equation and make sure your variable names match up. Then you just need to add an OR condition for your if statement to set the color based on each equation.
# using the SimpleGraphics library
from SimpleGraphics import *
# tell SimpleGraphics to only draw when I use the update() function
setAutoUpdate(False)
# use the random library to generate random numbers
import random
# size of the circles drawn
diameter = 15
resize(600, 400)
##
# returns a valid color based on the input coordinates
#
# #param x is an x-coordinate
# #param y is a y-coordinate
# #return a colour based on the input x,y values for the given flag
##
def define_colour(x, y):
slopeOne = (200 - 300) / (0 - 150)
b = 0 - (slopeOne * 200)
slopeTwo = (200 - 300) / (0 - 150)
b2 = -50 - (slopeTwo * 200)
lineEquationOne = (slopeOne * x) + b
lineEquationTwo = (slopeTwo * x) + b2
if (y > lineEquationOne) | (y < lineEquationTwo):
return "white"
else:
return 'red'
######################################################################
#
# Do NOT change anything below this line
#
######################################################################
# repeat until window is closed
while not closed():
for i in range(500):
# generate random x and y values
x = random.randint(0, getWidth())
y = random.randint(0, getHeight())
# set colour for current circle
setFill(define_colour(x, y))
# draw the current circle
ellipse(x, y, diameter, diameter)
update()

Plotting particles positions over time

I have drawn one position(x,y,z) of N particles in an enclosed volume.
x[i] = random.uniform(a,b) ...
I also found the constant velocity(vx,vy,vz) of the N particles.
vx[i] = random.gauss(mean,sigma) ...
Now I want to find the position of the N(=100) particles over time. I used the Euler-Cromer method to this.
delta_t = linspace(0,2,n-1)
n = 1000
v[0] = vx;...
r[0] = x;...
for i in range(n-1):
v[i+1,:] = v[i,:]
r[i+1,:] = r[i,:] + delta_t*v[i+1,:]
t[i+1] = t[i] + delta_t
But I want to find the position over time for every particle. How can I do this? Also, how do I plot the particles position over time in 3D?
To find the position of the particles at a given time you can use the following code:
import numpy as np
# assign random positions in the box 0,0,0 to 1,1,1
x = np.random.random((100,3))
# assign random velocities in the range around 0
v = np.random.normal(size=(100,3))
# define function to project the position in time according to
# laws of motion. x(t) = x_0 + v_0 * t
def position(x_0, v_0, t):
return x_0 + v_0*t
# get new position at time = 3.2
position(x, v, 3.2)

Categories