def __init__(self): pygame.init() self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode([600, 600]) self.view = View(self.screen) self.font = pygame.font.Font(None, 30) self.engine = PhysicsEngine() self.engine.contactGenerators.append(ContactGenerator(self.engine)) self.engine.gravity = Vec(0, 0) self.rb = Rigidbody() self.rb.engine = self.engine self.rb.p = Vec(random.uniform(20, 580), random.uniform(20, 580)) self.rb.v = Vec(0, 0) self.rb.w = 0 self.rb.invmass = 1 self.rb.invmoi = 0.001 self.rb.collider = Narc(self.rb, 4) self.rb.mat.setRotation(random.uniform(0, 2 * math.pi)) self.rb.collider.restitution = 1.0 color = pygame.Color(random.randint(0, 200), random.randint(0, 200), random.randint(0, 200)) self.view.objects.append(NarcObject(self.rb, color)) arena = Particle() arena.engine = self.engine arena.p = Vec(0, 0) arena.collider = Arena(arena, 600, 600) arena.collider.restitution = 1.0 self.engine.static.append(arena) arena.invmass = 0
def createArena(self, width, height): arena = Particle() arena.engine = self.engine arena.p = Vec(0,0) arena.collider = Arena(arena,width,height) arena.collider.restitution = 1.0 self.engine.static.append(arena) arena.invmass = 0
def __init__(self): pygame.init() self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode([600, 600]) self.view = View(self.screen) self.font = pygame.font.Font(None,30) self.engine = PhysicsEngine() self.engine.contactGenerators.append(ContactGenerator(self.engine)) self.engine.gravity = Vec(0,0) '''for i in range(10): rb = Rigidbody() rb.engine = self.engine rb.p = Vec(random.uniform(20,580),random.uniform(20,580)) rb.v = Vec(random.uniform(-50,50),random.uniform(-50,50)) rb.w = random.uniform(-0.25,0.25) rb.invmass = 1 rb.invmoi = 0.001 rb.collider = Polygon(rb,[Vec(random.uniform(-30,-10),random.uniform(10,30)), Vec(random.uniform(-30,-10),random.uniform(-30,-10)), Vec(random.uniform(10,30),random.uniform(-30,-10)), Vec(random.uniform(10,30),random.uniform(10,30))]) rb.collider.restitution = 1.0 self.engine.moving.append(rb) color = pygame.Color(random.randint(0,200),random.randint(0,200),random.randint(0,200)) self.view.objects.append(PolyObject(rb,color))''' for i in range(10): rb = Rigidbody() rb.engine = self.engine rb.p = Vec(random.uniform(20,580),random.uniform(20,580)) rb.v = Vec(random.uniform(-50,50),random.uniform(-50,50)) rb.w = random.uniform(-0.5,0.5) rb.invmass = 1 rb.invmoi = 0.001 rb.collider = Narc(rb, 6) rb.mat.setRotation(random.uniform(0,2*math.pi)) rb.collider.restitution = 1.0 self.engine.moving.append(rb) color = pygame.Color(random.randint(0,200),random.randint(0,200),random.randint(0,200)) self.view.objects.append(NarcObject(rb,color)) arena = Particle() arena.engine = self.engine arena.p = Vec(0,0) arena.collider = Arena(arena,600,600) arena.collider.restitution = 1.0 self.engine.static.append(arena) arena.invmass = 0
def getReadOnlyParticle(self, i): ''' Get a single particle (a view in the numpy array structures). The particle is READ ONLY, that is, it is not expected to be changed. :param int i: The particle index in the array. ''' p = Particle() p.mass = self.mass p.velocity = self.velocity[i] p.position = self.position[i] p.geometry._position = p.position return p
# As in reality, infinite equals one million :) INFINITE = 1000000.0 if __name__ == '__main__': main_window = MainWindow("Equivalent Springs Example", 400, 400) main_window.setInputListener(GameLikeInputListener(main_window)) simulator = PhysicsSimulator() #=========================================================================== k0 = 3.5 k1 = 0.5 particle1 = Particle(mass=1.0) particle2 = Particle(mass=INFINITE) simulator.addBody(particle1) simulator.addBody(particle2) particle1_painter = PaintablePolyhedron(particle1.geometry) particle2_painter = PaintablePolyhedron(particle2.geometry) connector1_2_painter = PaintableConnector(particle1, particle2) main_window.addObject(particle1_painter) main_window.addObject(particle2_painter) main_window.addObject(connector1_2_painter) particle1.setPosition(np.array([-1.0, 0.0, 5.0])) particle2.setPosition(np.array([-1.0, -1.0, 5.0])) simulator.addForceGenerator(SpringForceGenerator(particle1, particle2, 0.6, k0)) simulator.addForceGenerator(SpringForceGenerator(particle1, particle2, 0.6, k1)) equivalent_k = k0 + k1
def update(self, delta_t): self._lifetime -= delta_t if self._lifetime < 0.0: self.reset() Particle.update(self, delta_t)
def __init__(self): Particle.__init__(self, mass=0.001) self.geometry.setColor(np.array([0.1, 0.1, 0.6])) self.reset()
def __init__(self): pygame.init() self.clock = pygame.time.Clock() self.screen = pygame.display.set_mode([600, 600]) self.view = View(self.screen) self.font = pygame.font.Font(None,30) self.engine = PhysicsEngine() self.engine.contactGenerators.append(ContactGenerator(self.engine)) self.engine.gravity = Vec(0,0) #for i in range(5): # c = Particle() # c.engine = engine # c.p = Vec(random.uniform(20,580),random.uniform(20,580)) # c.v = Vec(random.uniform(-150,150),random.uniform(-150,150)) # c.m = random.uniform(40,1000) # c.collider = Circle(c,Vec(0,0),math.sqrt(c.m)) # c.collider.restitution = 0.75 # # engine.moving.append(c) # color = pygame.Color(random.randint(0,200),random.randint(0,200),random.randint(0,200)) # view.objects.append(CircleObject(c,color)) # #u = Particle() #u.engine = engine #u.p = Vec(100,100) #u.v = Vec(0,0) #u.m = 100.0 #u.collider = Union(u) #u.collider.colliders.append(Circle(u,Vec(0,-10),20)) #u.collider.colliders.append(Circle(u,Vec(0,10),20)) #u.collider.restitution = 0.75 #for i in range(25): # p = Particle(); # p.engine = engine # p.p = Vec(random.uniform(20,580),random.uniform(20,580)) # p.v = Vec(random.uniform(-50,50),random.uniform(-50,50)) # p.collider = Polygon(p,[Vec(random.uniform(-30,-10),random.uniform(10,30)), # Vec(random.uniform(-30,-10),random.uniform(-30,-10)), # Vec(random.uniform(10,30),random.uniform(-30,-10)), # Vec(random.uniform(10,30),random.uniform(10,30))]) # p.collider.restitution = 1.0 # # engine.moving.append(p) # color = pygame.Color(random.randint(0,200),random.randint(0,200),random.randint(0,200)) # view.objects.append(PolyObject(p,color)) '''for i in range(15): rb = Rigidbody() rb.engine = self.engine rb.p = Vec(random.uniform(20,580),random.uniform(20,580)) rb.v = Vec(random.uniform(-250,250),random.uniform(-250,250)) rb.w = random.uniform(-0.5,0.5) rb.invmass = 1 rb.invmoi = 0.001 rb.collider = Narc(rb) rb.mat.setRotation(random.uniform(0,2*math.pi)) rb.collider.restitution = 1.0 self.engine.moving.append(rb) color = pygame.Color(random.randint(0,200),random.randint(0,200),random.randint(0,200)) self.view.objects.append(NarcObject(rb,color))''' arena = Particle() arena.engine = self.engine arena.p = Vec(0,0) arena.collider = Arena(arena,600,600) arena.collider.restitution = 1.0 self.engine.static.append(arena) arena.invmass = 0
def Run(self): avefps = 0 numFrames = 1 dt = 0 fps = 0 physicsdt = 0.025 elapsed = 0 totElapsed = 0 running = True while running: event = pygame.event.poll() if totElapsed % 2 == 0: newrb = Rigidbody() newrb.engine = self.engine newrb.p = Vec(random.uniform(20, 580), random.uniform(20, 580)) newrb.v = Vec(0, 0) newrb.w = 0 newrb.invmass = 1 newrb.invmoi = 0.001 newrb.collider = Narc(self.rb, 4) newrb.collider.altered(self.rb.collider) newrb.mat.setRotation(random.uniform(0, 2 * math.pi)) newrb.collider.restitution = 1.0 color = pygame.Color(random.randint(0, 200), random.randint(0, 200), random.randint(0, 200)) self.view.objects.append(NarcObject(newrb, color)) self.rb = newrb arena = Particle() arena.engine = self.engine arena.p = Vec(0, 0) arena.collider = Arena(arena, 600, 600) arena.collider.restitution = 1.0 self.engine.static.append(arena) arena.invmass = 0 if event.type == pygame.QUIT or totElapsed >= 20: running = False self.screen.fill([245, 240, 230]) # blank the screen. # Save time by only calling this once dt = self.clock.tick() * 0.001 elapsed += dt while elapsed > 0: elapsed -= physicsdt self.engine.step(physicsdt) self.view.update() totElapsed += dt newfps = 1.0 / dt avefps *= numFrames avefps += newfps numFrames += 1 avefps /= numFrames fpstext = "Current: %.1f | Average: %.1f | Frames: %d" % (newfps, avefps, numFrames) fpsSurface = self.font.render(fpstext, False, Color(0, 0, 0)) self.screen.blit(fpsSurface, (0, 0)) pygame.display.update()
def createParticle(mass, color): particle = Particle(mass) particle.setGeometry(readPly('../objects/sphere.ply')) particle.geometry.scale(np.repeat(mass / 2.0, 3)) particle.geometry.setColor(color) return particle