Earth pulling Sun in 2d Physics Engine - python

I'm creating a basic physics engine in python and I've managed to create a simple "planet" class to handle the maths. I've also created a basic working scenario when a small planet orbits a larger one - if the small planet has zero mass. However, when I give the small planet any mass it slowly pulls the sun to the right in an arc shape. It's easier to see in this image:
.
I understand what's going on - the earth is pulling the sun right on one side and stopping it but not pulling it back on the other - but I have no idea how to fix it, short of giving the sun a hard-coded velocity. I've checked a few other sources but nothing helps - in fact this physics sim has the same problem (uncheck system centered).
Does this happen in the real world, or is it a glitch with this type of simulation? Either way, is there some sort of hack to prevent this from happening?
My planet class (all the methods are pretty straightforward):
import math
G = 6
class Planet():
def __init__(self, x = 0, y = 0, vx = 0, vy = 0, mass = 1, radius = 1, colour = "#ffffff"):
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.mass = mass
self.radius = radius
self.colour = colour
def draw(self, canvas):
canvas.create_oval(self.x - self.radius, self.y - self.radius, self.x + self.radius, self.y + self.radius, fill = self.colour)
def move_next(self):
self.x += self.vx
self.y += self.vy
def apply_gravity(self, other):
if other is self: #make sure you're not checking against yourself.
return 1
x = other.x - self.x
y = other.y - self.y
r = math.hypot(x, y)
if r == 0: #make sure they're not on each other.
return 1
gravity = G * (other.mass) / (r ** 2) #calculate the force that needs to be applied
self.vx += (x / r) * gravity #normalise the x component of vector and multiply it by the force
self.vy += (y / r) * gravity #normalise the y component of vector and multiply it by the force
And the "main" file:
from tkinter import *
import time
import planet
FPS = 1/60
window = Tk()
canvas = Canvas(window, width = 640, height = 400, bg = "#330033")
canvas.pack()
earth = planet.Planet(x = 320, y = 100, vx = 10, mass = 1, radius = 10, colour = "lightblue") #Because the earth's mass is so low, it won't be AS noticable, but it still pulls the sun a few pixels per rotation.
sun = planet.Planet(x = 320, y = 200, mass = 2e3, radius = 20, colour = "yellow")
planets = [earth, sun]
while True:
canvas.delete(ALL)
for planet in planets:
for other in planets:
planet.apply_gravity(other)
planet.move_next()
planet.draw(canvas)
window.update()
time.sleep(FPS)

Related

Pygame for possible trajectories planning for a car and make the car follow one of that path/trajectory

I have to create a visualization where I have to create a car (I considered to represent it as a rectangle) and develop trajectories for that car depending on its lateral & longitudinal acceleration. My problem is I am unable to create trajectories. I am doing this as a part of own project. Hence I have no idea if I am in the right direction or not. Guide me if I am wrong.
Problem1 : How do I show predicted path trajectories (beginner step to create only 8 trajectories and not many) ? Is a possible way to also create trajectories also like a curve using Pygame? If not, can I use a mix pf pygame and tkinter to create a mix of curved and straight line trajectories?
Problem2 : Make my rectangular car run along the chosen trajectory (curved line or straight line) or make it follow the path defined in trajectory.
Solutions Tried:
I have taken a rectangular sprite as a car with an idea that I can make it move around the desired trajectory. I am successful with sprite creation and moving it. But I am unable to show display any trajectory curve and make the car follow the path. I am not sure if I have to take trajectories also as Sprite or only car as Sprite is sufficient and trajectories as lines/curves?
Code of the above same explanation is as follows:
My Sprite Class :
class Vehicle(pygame.sprite.Sprite):
def __init__(self,x,y,slip_angle,length=4):
super().__init__()
width = 50
height = 50
self.image = pygame.Surface([width,height])
#Create a car
self.image.fill(RED)
self.image.set_colorkey(RED)
pygame.draw.rect(self.image, WHITE, [0, 0, width, height])
self.rect = self.image.get_rect()
#Parameters of car
self.position = position = Vector2(x, y)
self.velocity = Vector2(0.0, 0.0)
self.slip_angle = slip_angle
self.length = length
self.max_velocity = 20
self.brake_deceleration = 10
self.free_deceleration = 2
self.initial_velocity = 0
self.long_acceleration = 0.0
self.lat_acceleration = 0.0
self.acceleration = 0.0
self.steering = 0.0
def update(self,dt):
self.velocity += (self.acceleration * dt, 0)
self.velocity.x = max(-self.max_velocity, min(self.velocity.x, self.max_velocity))
if self.steering:
turning_radius = self.length / sin(radians(self.steering))
angular_velocity = self.velocity.x / turning_radius
else:
angular_velocity = 0
self.position += self.velocity.rotate(-self.angle) * dt
self.slip_angle += degrees(angular_velocity) * dt
Trajectory Class:
class trajectory(object):
def __init__(self,x,y):
self.x = x
self.y = y
def draw():
x_value = []
time = []
for acc in range(10):
for dt in range(0,10,0.05):
x = vehicle1.initial_velocity + (vehicle1.velocity*dt) + (0.5 * vehicle1.long_acceleration * dt* dt)
x_value.append(x)
time.append(dt)
coordinates = (x_value,time)
canvas_1 = Canvas(root,700,600,background='pink')
canvas_1.grid(row=0,column=1)
x1 = coordinates[acc]
y1 = coordinates[time]
canvas_1.create_line(x1,y1)
#pygame.draw.line(screen,GREEN,list(coordinates),(700, 600))
def update(self):
#self.angle = vehicle1.slip_angle*pi / 180
self.velocity_h = vehicle1.velocity*cos(angle)
self.velocity_v = vehicle1.velocity*sin(angle)
Pygame Main Logic :
#main Logic
proceed = True
#Capturing events till exit
while proceed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
proceed = False
vehicle1.update(dt)
sprites_list.update(dt)
screen.fill(WHITE)
#screen.pygame.Surface.fill(color, rect=None, special_flags=0)
pygame.draw.line(screen, BLACK, [0, 0], [700, 300], 5)
# self.screen.blit(rotated, [20,30])
sprites_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
I thank you for your advices and support. Happy to accept any guidance
A simple re-working of your trajectory.draw() should sort it out. I noticed the initial x and y passed to the constructor don't seem to be used. Is it intended that the trajectory is always from ( 0, 0 ) or suchlike?
def draw( screen, initial_vel, vel, accel ):
coordinates = []
for acc in range(10):
for dt in range( 0, 10, 0.05 ):
x = initial_vel + ( vel * dt ) + ( 0.5 * accel * dt * dt )
coordinates.append( ( x, dt ) )
if ( len( coordinates ) > 1 ):
PINK = ( 255, 192, 203 )
pygame.draw.lines( screen, PINK, False, coordinates )
Pass your Window and Vehicle Velocities as a parameters when calling trajectory.draw(), rather than relying on global variables.
my_trajectory.draw( screen, vehicle1.initial_velocity, vehicle1.velocity, vehicle1.long_acceleration )

I'm trying to create an orbital simulator using vpython but when I run it I just get a black screen

I used a youtube video for the basis of my code and adjusted it to include classes and object. The original code from the video works perfectly.
My version of the code returns a black screen and even when trying to fix it the most luck I get is the two objects displaying without moving.
I've also tried running it on glowscript IDE and winpython.
Thanks to anyone who can help!
from vpython import *
class Planet:
def __init__(self, radius, colour, mass, x, y, z, vx, vy, vz):
self.radius = int(radius)
self.colour = colour
self.mass = int(mass)
self.x = int(x)
self.y = int(y)
self.z = int(z)
self.vx = int(vx)
self.vy = int(vy)
self.vz = int(vz)
def run_planet(self):
r = self.radius
c = self.colour
px = self.x
py = self.y
pz = self.z
vx = self.vx
vy = self.vy
vz = self.vz
p = sphere(pos = vec(px, py, pz), radius = r, color = color.white, make_trail = True)
v = vec(vx, vy, vz)
for i in range(1000):
rate(100)
p.pos = p.pos + v
dist = (p.pos.x**2 + p.pos.y**2 + p.pos.z**2)**0.5
RadialVector = (p.pos - sun.pos)/dist
Fgrav = -(6.674*10**11)*self.mass*(1.989*10**30) * RadialVector/dist**2
v = v + Fgrav
p.pos += v
if dist <= sun.radius: break
###############################################################################
sun = sphere(pos = vec(0,0,0), radius = 100, color = color.orange)
p1 = Planet(10, "blue", 20, -200, 0, 0, 0, 0, 5)
p1.run_planet()
Original code from video:
sun = sphere(pos = vec(0,0,0), radius = 100, color = color.orange)
earth = sphere(pos = vec(-200,0,0), radius = 10, color = color.white, make_trail = True)
earthv = vec(0,0,5)
for i in range(10000000):
rate(100)
earth.pos = earth.pos + earthv
dist = (earth.pos.x**2 + earth.pos.y**2 + earth.pos.z**2)**0.5
RadialVector = (earth.pos - sun.pos)/dist
Fgrav = -10000 * RadialVector/dist**2
earthv = earthv + Fgrav
earth.pos += earthv
if dist<= sun.radius: break
Ps: any physics corrections would also be very appreciated!
The distance between the (center of the) sun and the (center of the) planet is only 200 meters, so the calculated force is gigantic and the new v is of the order of 10 to the 38, so immediately the planet is so far from the sun that the camera moves WAY back to try to show the whole scene, leaving the screen to appear black since the objects are now so very far away.

Adding gravity to a bouncing ball using vectors

I have a gravity vector (in the form [r, theta]) which I add to my ball's velocity vector. For some reason, the ball doesn't return to the same height after bouncing, but instead slowly loses height sporadically. I am guessing there's some rounding error or something in a calculation I'm using, but I can't isolate the issue.
Here is my code. You need both files and pygame to run it. Sorry if it's a little confusing. I can comment anything some more if you want.
I added a marker whenever the ball reaches its max height so you guys what I mean. I want the ball to return to exactly the same height every time it bounces.
I took a little bit of unnecessary code out. The full program is under the pastebin links.
https://pastebin.com/FyejMCmg - PhysicsSim
import pygame, sys, math, tools, random, time
from pygame.locals import *
clock = pygame.time.Clock()
lines = []
class Particle:
def __init__(self,screen,colour, mass, loc, vel):
self.screen = screen
self.colour = colour
self.mass = mass
self.x = loc[0]
self.y = loc[1]
self.location = self.x,self.y
self.speed = vel[0]
self.angle = vel[1]
def update(self):
global lines
# add gravity
self.speed,self.angle = tools.add_vectors2([self.speed,self.angle], tools.GRAVITY)
# update position
dt = clock.tick(60)
self.x += self.speed * tools.SCALE * math.cos(self.angle) * dt
self.y -= self.speed * tools.SCALE * math.sin(self.angle) * dt
self.location = int(self.x),int(self.y)
# border checking
do = False
n=[]
if ((self.y+self.mass) > tools.SCREEN_HEIGHT):
self.y = tools.SCREEN_HEIGHT-self.mass
n = [0,1]
do = True
# adds position to array so max height so max height can be recorded
if (self.speed==0):
lines.append([self.screen, self.location, self.mass])
# bounce
if do:
#init, convert everything to cartesian
v = tools.polarToCartesian([self.speed, self.angle])
#final -> initial minus twice the projection onto n, where n is the normal to the surface
a = tools.scalarP(2*abs(tools.dotP(v,n)),n) #vector to be added to v
v = tools.add_vectors(v,a)
self.angle = tools.cartesianToPolar(v)[1] # does not set magnitude
# drawing
pygame.draw.circle(self.screen, self.colour, self.location, self.mass, 0)
# draws max height line
def draw_line(l):
screen = l[0]
location = l[1]
radius = l[2]
pygame.draw.line(screen, tools.BLACK, [location[0] + 15, location[1]-radius],[location[0] - 15, location[1]-radius])
def main():
pygame.init()
DISPLAY = pygame.display.set_mode(tools.SCREEN_SIZE,0,32)
DISPLAY.fill(tools.WHITE)
particles = []
particles.append(Particle(DISPLAY, tools.GREEN, 10, [100,100], [0,0]))
done = False
while not done:
global lines
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
DISPLAY.fill(tools.WHITE)
for i in particles:
i.update()
for l in lines:
draw_line(l)
pygame.display.update()
main()
https://pastebin.com/Epgqka31 - tools
import math
#colours
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
BLACK = ( 0, 0, 0)
COLOURS = [WHITE,BLUE,GREEN,RED,BLACK]
#screen
SCREEN_SIZE = SCREEN_WIDTH,SCREEN_HEIGHT = 1000,700
#vectors
GRAVITY = [5.0, 3*math.pi/2] # not 9.8 because it seems too high
SCALE = 0.01
# converts polar coordinates to cartesian coordinates in R2
def polarToCartesian(v):
return [v[0]*math.cos(v[1]), v[0]*math.sin(v[1])]
# converts cartesian coordinates to polar coordinates in R2
def cartesianToPolar(v):
return [math.sqrt(v[0]**2 + v[1]**2), math.atan2(v[1],v[0])]
# dots two cartesian vectors in R2
def dotP(v1, v2):
return v1[0]*v2[0] + v1[1]*v2[1]
# multiplies cartesian vector v by scalar s in Rn
def scalarP(s,v):
v_=[]
for i in v:
v_.append(s*i)
return v_
# returns the sum of two cartesian vectors in R2
def add_vectors(v1, v2):
return [v1[0]+v2[0], v1[1]+v2[1]]
# returns the sum of two polar vectors in R2, equations from https://math.stackexchange.com/questions/1365622/adding-two-polar-vectors
def add_vectors2(v1,v2):
r1,r2,t1,t2 = v1[0],v2[0],v1[1],v2[1]
return [math.sqrt(r1**2 + r2**2 + 2*r1*r2*math.cos(t2-t1)), t1 + math.atan2(r2*math.sin(t2 - t1), r1 + r2*math.cos(t2 - t1))]
Your time interval, dt = clock.tick(60), is not a constant. If you change it to dt = 60 your program runs as expected.
Have a look a the Verlet Algorithm and implement it in your code. You are on the right track!

pygame bullet physics messed up by scrolling

the code below is the bullet class for my shooter game in pygame. as you can see if you run the full game (https://github.com/hailfire006/economy_game/blob/master/shooter_game.py) the code works great to fire bullets at the cursor as long as the player isn't moving. However, I recently added scrolling, where I change a global offsetx and offsety every time the player gets close to an edge. These offsets are then used to draw each object in their respective draw functions.
unfortunately, my bullet physics in the bullet's init function no longer work as soon as the player scrolls and the offsets are added. Why are the offsets messing up my math and how can I change the code to get the bullets to fire in the right direction?
class Bullet:
def __init__(self,mouse,player):
self.exists = True
centerx = (player.x + player.width/2)
centery = (player.y + player.height/2)
self.x = centerx
self.y = centery
self.launch_point = (self.x,self.y)
self.width = 20
self.height = 20
self.name = "bullet"
self.speed = 5
self.rect = None
self.mouse = mouse
self.dx,self.dy = self.mouse
distance = [self.dx - self.x, self.dy - self.y]
norm = math.sqrt(distance[0] ** 2 + distance[1] ** 2)
direction = [distance[0] / norm, distance[1] / norm]
self.bullet_vector = [direction[0] * self.speed, direction[1] * self.speed]
def move(self):
self.x += self.bullet_vector[0]
self.y += self.bullet_vector[1]
def draw(self):
make_bullet_trail(self,self.launch_point)
self.rect = pygame.Rect((self.x + offsetx,self.y + offsety),(self.width,self.height))
pygame.draw.rect(screen,(255,0,40),self.rect)
You don't take the offset into account when calculating the angle between the player and the mouse. You can fix this by changing the distance like this:
distance = [self.dx - self.x - offsetx, self.dy - self.y - offsety]

Lennard Jones interaction between particles. Particles moving to one point

import numpy as np
import random
import pygame
background_colour = (255,255,255)
width, height = 300, 325
eps = 1
sigma = 1
dt = 0.05
class Particle():
def __init__(self):
self.x = random.uniform(0,400)
self.y = random.uniform(0,500)
self.vx = random.uniform(-.1,.1)
self.vy = random.uniform(-.1,.1)
self.fx = 0
self.fy = 0
self.m = 1
self.size = 10
self.colour = (0, 0, 255)
self.thickness = 0
def bounce(self):
if self.x > width - self.size:
self.x = 2*(width - self.size) - self.x
elif self.x < self.size:
self.x = 2*self.size - self.x
if self.y > height - self.size:
self.y = 2*(height - self.size) - self.y
elif self.y < self.size:
self.y = 2*self.size - self.y
def getForce(self, p2):
dx = self.x - p2.x
dy = self.y - p2.y
self.fx = 500*(-8*eps*((3*sigma**6*dx/(dx**2+dy**2)**4 - 6*sigma**12*dx/(dx**2+dy**2)**7)))
self.fy = 500*(-8*eps*((3*sigma**6*dy/(dx**2+dy**2)**4 - 6*sigma**12*dy/(dx**2+dy**2)**7)))
return self.fx, self.fy
def verletUpdate(self,dt):
self.x = self.x + dt*self.vx+0.5*dt**2*self.fx/self.m
self.y = self.y + dt*self.vy+0.5*dt**2*self.fy/self.m
def display(self):
pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)
screen = pygame.display.set_mode((width, height))
screen.fill(background_colour)
partList = []
for k in range(10):
partList.append(Particle())
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(background_colour)
for k, particle in enumerate(partList):
for p2 in partList[k+1:]:
particle.getForce(p2)
particle.verletUpdate(dt)
particle.bounce()
particle.display()
pygame.display.flip()
pygame.quit()
Is my code correct? I tried to simulate particles in 2D move with Lennard Jones forces. I think calculating forces works okay but why my particles are moving to one point? Ocasionally I also get error OverflowError: Python int too large to convert to C long Any advice would be useful.
I can not comment on the physics of the simulation, but as far as the display is concerned following are my observations:
Your particles move to one point because the update condition for the x and y parameter in your code in verletUpdate are slowly moving to values beyond the display area. Also to values out of the range of the int() function which is causing your error. You can see this with the statement:
def verletUpdate(self,dt):
self.x = self.x + dt*self.vx+0.5*dt**2*self.fx/self.m
self.y = self.y + dt*self.vy+0.5*dt**2*self.fy/self.m
print self.x
print self.y
Sample Output:
290.034892392
9.98686293664
290.028208837
9.99352484332
-2.55451579742e+19
1.12437640586e+19
Also they saturate and with iterations, the update gets smaller and smaller:
def display(self):
print ' %s + %s '%(self.x,self.y)
pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)
Output:
10.0009120033 + 10.0042647307
10.0009163718 + 10.0000322065
10.0009120033 + 10.0042647307
10.0009163718 + 10.0000322065
...
10.0009163718 + 10.0000322065
10.0009120033 + 10.0042647307
10.0009163718 + 10.0000322065
This is also why your bounce functions and your limit checking is not working. And after a lot of iterations on occasion your self.x and self.y are far exceeding the limits of int().
The code seems fine, but you can get rid of the overflow error by adding some checks above the draw line. For instance I initialized them randomly again to simulate a particle going off screen and us tracking a new one. Feel free to change it.
def display(self):
if(self.x<0 or self.x>height):
self.__init__()
print "reset"
if(self.y<0 or self.y>width):
self.__init__()
print "reset"
print ' %s + %s '%(self.x,self.y)
pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)
Also at one point you adress the array as [k+1:], and addressing the zero element caused a divide by zero error. You might want to look at that.

Categories