def constraints(self, particle): # adjust the effects of the limits (constraints) of the world on the partcle # it must run before acceleration and velocity are applied in impulse World.Log_Data("constraint start: {}".format(particle)) if particle.position.get_z() > self.max_z: particle.velocity = Vector3dm.zero_vector() particle.acceleration = Vector3dm.zero_vector() particle.position.set_z(self.max_z) World.Log_Data( "YES! Rock bottom post constraints:{}".format(particle)) if particle.position.get_y() > self.max_y: particle.position.set_y(self.max_y) if particle.position.get_x() > self.max_x: particle.position.set_y(self.max_x) if particle.position.get_y() < self.min_y: particle.position.set_y(self.min_y) if particle.position.get_x() < self.min_x: particle.position.set_x(self.min_x) if particle.position.get_z() < self.min_z: particle.position.set_z(self.min_z) if particle.velocity.get_z() > 20.0: particle.velocity.set_z(20.0) World.Log_Data("constraint end: {}".format(particle))
def __init__(self, pos_vect, vel_vect=Vector3dm.zero_vector(), id=""): self.position = pos_vect self.velocity = vel_vect self.acceleration = Vector3dm.zero_vector() self.id = id self.count = 0 # various accelration Attributes # aerodynamics self.aero_coef = .05 # scale factor for Aerodynamic drag which opposes velocity #colision avoidance self.col_size = 1 # collision size self.dis_size = 20 # display size self.color = (255, 255, 255) self.flash_color = (255, 50, 0) # bright red
def brain(self): # this is code that changes the acceleration based on pixie desires. result = Vector3dm.zero_vector() CLOSE_RANGE = 10.0 MAX_ACC = 20.0 if self.goal is None: self.goal = self.goals[self.goal_idx] self.goal_idx += 1 if self.goal_idx > len(self.goals): self.goal_idx = 0 World.Log_Data("brain New Goal: {} (#{})".format(self.goal,self.goal_idx)) return result range = self.position.magnitude(self.goal) if range < CLOSE_RANGE: # we've reached our goal self.goal = None World.Log_Data("brain REACHED goal") return result World.Log_Data("brain Current goal: {}".format(self.goal)) World.Log_Data("brain range: {}".format(range)) direction = self.position.point_at_that(self.goal) World.Log_Data("point at {} from {} to {}".format(direction.convert_to_cartesian(),self.position,self.goal)) if direction.get_r() > MAX_ACC: direction.set_r(MAX_ACC) speed = self.velocity.get_r() slowing = speed/MAX_ACC # Seconds to slow arrival = range/speed # seconds to get there if slowing < arrival: result = direction World.Log_Data("brain Speeding Toward Direction: {}={}".format(result,result.convert_to_cartesian())) else: if speed <= CLOSE_RANGE: return Vector3dm.zero_vector() #coast closer elif speed < CLOSE_RANGE+MAX_ACC: direction.set_r(speed - CLOSE_RANGE) #get the speed to around the close_range; fastest we can go to not overshoot result = direction.neg() World.Log_Data("brain SLOWING toward direction: {} (Direction: {}".format(result,direction)) return result
def test_zero_vector(self): zero_vector = Vector3dm.zero_vector() vx = zero_vector.get_x() vy = zero_vector.get_y() vz = zero_vector.get_z() vr = zero_vector.get_r() self.assertAlmostEqual(vx,0.0,6,"test_zero_vector bad result: is {} should be {}".format(vx,0.0)) self.assertAlmostEqual(vy,0.0,6,"test_zero_vector bad result: is {} should be {}".format(vy,0.0)) self.assertAlmostEqual(vz,0.0,6,"test_zero_vector bad result: is {} should be {}".format(vz,0.0)) self.assertAlmostEqual(vr,0.0,6,"test_zero_vector bad result: is {} should be {}".format(vr,0.0)) self.assertEqual(type(zero_vector.vals),type([]),"test changed vals type")
def impulse(self): for i in range(0, len(self.particles)): el = self.particles[i] el.acceleration = Vector3dm.zero_vector() World.Log_Data("{}: impulse old: {}".format(i, el)) brain_acc = el.brain() brain_acc.set_r(brain_acc.get_r() * float(self.impulse_duration) / World.MICROS_IN_SEC) World.Log_Data("{}: brain: {}".format( i, brain_acc.convert_to_cartesian())) aero_acc = el.aero_acc() World.Log_Data("{}: pre set aero_acc: {}".format( i, aero_acc.convert_to_cartesian())) aero_acc.set_r(brain_acc.get_r() * float(self.impulse_duration) / World.MICROS_IN_SEC) World.Log_Data("{}: post set aero_acc: {}".format( i, aero_acc.convert_to_cartesian())) gravity_acc = self.gravity(el) physics_acc = self.physics(el) World.Log_Data("{}: impulse pre:{}".format(i, el)) el.acceleration = Vector3dm.zero_vector().add(brain_acc).add( aero_acc).add(gravity_acc).add(physics_acc) el.velocity = el.velocity.add(el.acceleration) el.position = el.position.add(el.velocity) World.Log_Data("{}: impulse post:{}".format(i, el)) self.constraints( el) # change the particle based on wall/space constraints World.Log_Data("{}: impulse new: {}".format(i, el)) self.collisions() self.sim_time += self.impulse_duration World.Log_Data("time: {}".format(self.sim_time))
if x > max_x: v.set_x(max_x) elif x < min_x: v.set_x(min_x) if y > max_y: v.set_y(max_y) elif y < min_y: v.set_y(min_y) if z > max_z: v.set_z(max_z) elif z < min_z: v.set_z(min_z) #if bad_vec: # print("bad vec",dist,old_v.convert_to_cartesian(),v) return v v = Vector3dm.zero_vector() for i in range(0, 3000): print("{}\t{}".format(v.get_x(), v.get_z())) v = rand_vector(v, 90, 150) #print(rand_vector(300)) #for phi_deg in range(0,91,1): # for theta_deg in range(0,361,1): # phi = math.radians(phi_deg) # theta = math.radians(theta_deg) # v = Vector3dm(300,theta,phi,"s") # if v.convert_to_cartesian().get_z() < 0: # print(theta_deg,phi_deg,"||",v,"||",v.convert_to_cartesian())
def collision_avoidance(self): #apply the efforts of the return Vector3dm.zero_vector()
def physics(self, particle): # apply other kinds of physics (besides aerodynamics and gravity ) return Vector3dm.zero_vector()
def __init__(self,pos_vect,vel_vect=Vector3dm.zero_vector()): super().__init__(pos_vect, vel_vect) self.goals = [Vector3dm(10,-10,100,"c"),Vector3dm(400,400,100,"c"),Vector3dm(0,0,500,"c"),Vector3dm(-400,-400,800,"c")] self.goal_idx = 0 # first goal, increment as we go self.goal = None # brain will calculate first goal