def _draw_lsystem(self) -> None: not_drawn_lines = 0 x, y = self.start_coords[0], self.start_coords[1] angle = self.start_angle stack = [] for char in self.string: if char == "F": new_x, new_y = point_on_circle((x, y), self.step_length, angle) if check_coords((x, y), (new_x, new_y), self.size): self.draw.line(((x, y), (new_x, new_y)), fill=self.line_color, width=1) not_drawn_lines = 0 else: not_drawn_lines += 1 x, y = new_x, new_y elif char == "f": x, y = point_on_circle((x, y), self.step_length, angle) elif char == "+": angle -= self.rot_angle elif char == "-": angle += self.rot_angle elif char == "|": angle += pi elif char == "[": stack.append((x, y, angle)) elif char == "]": x, y, angle = stack.pop() if not_drawn_lines >= 30000: break
def find_normal_lines(self, points, radius, current_pass): working_line = [] i = 0 for point in points: if point == points[0] or point == points[-1]: # start and end points self.scene.debug_batch.add(2, GL_LINES, self.scene.ordered_group4, ('v2f', (point[0],point[1],point[0],point[1]-radius)), ('c3B', (55,255,85)*2)) working_line.append((point[0],point[1]-radius)) else: angle1 = math.radians(utils.angle_between_points(point, points[i-1])+90) angle2 = math.radians(utils.angle_between_points(point, points[i+1])+90) delta = angle1-angle2-math.radians(180) # disable this and see what happens # yeah, i don't get it either... if delta < -3: delta += math.radians(360) new_step_point = utils.point_on_circle(radius, angle1-delta/2, point) working_line.append(new_step_point) # actual appending normal = (point[0], point[1], new_step_point[0], new_step_point[1]) self.scene.debug_batch.add(2, GL_LINES, self.scene.ordered_group4, ('v2f', normal), ('c3B', (25,255,55)*2)) self.scene.debug_batch.add(1, GL_POINTS, self.scene.ordered_group4, ('v2f', (new_step_point[0],new_step_point[1])), ('c3B', (95,95,255))) self.scene.debug_batch.add(1, GL_POINTS, self.scene.ordered_group4, ('v2f', (point[0],point[1])), ('c3B', (255,255,255))) i += 1 l = [] for point in working_line: l.append(point[0]) l.append(point[1]) ''' #self.scene.debug_batch.add_indexed(len(l)/2, GL_LINES, self.scene.ordered_group5, # pyglet_util.calc_line_index(len(l)/2), # ('v2f', l), # ('c3B', (25,255,255)*(len(l)/2))) ''' print working_line return working_line
def find_normals(point_dict, scene, radius=120): new_dict = {} points = point_dict.values() for i, point in enumerate(points): if point == points[0] or point == points[-1]: # start and end points scene.debug_batch.add(2, GL_LINES, scene.ordered_group4, ('v2f', (point[0],point[1],point[0],point[1]-radius)), ('c3B', (55,255,85)*2)) new_dict[i] = point[0], point[1]-radius else: angle1 = math.radians(utils.angle_between_points(point, points[i-1])+90) angle2 = math.radians(utils.angle_between_points(point, points[i+1])+90) delta = angle1-angle2-math.radians(180) # disable this and see what happens # yeah, i don't get it either... if delta < -3: delta += math.radians(360) normal_point = utils.point_on_circle(radius, angle1-delta/2, point) if normal_point[0] > points[0][0]: new_dict[i] = normal_point normal = (point[0], point[1], normal_point[0], normal_point[1]) scene.debug_batch.add(2, GL_LINES, scene.ordered_group4, ('v2f', normal), ('c3B', (25,255,55)*2)) scene.debug_batch.add(1, GL_POINTS, scene.ordered_group4, ('v2f', (normal_point[0],normal_point[1])), ('c3B', (95,95,255))) scene.debug_batch.add(1, GL_POINTS, scene.ordered_group4, ('v2f', (point[0],point[1])), ('c3B', (255,255,255))) return new_dict
def step(self, tick): # if self.target == self.master.ball.location: # self.master.ball.step(tick) # self.target = self.master.ball.location # if self.agent.team == 0: print(utils.steer(self.target, self.car), utils.angle2D(self.target, self.car), self.target.tuple, self.car.location.tuple) # self.controls.yaw = utils.steer(self.target, self.car) self.controls.throttle, self.controls.boost = utils.throttle( self.car, self.speed) turn_radius = utils.turn_radius(self.car, self.target) angle_to_target = utils.angle2D(self.target, self.car) point = utils.point_on_circle(self.car, utils.sign(angle_to_target) * math.pi / 2, turn_radius) #, self.car.rotation) circle = [] for i in range(0, 366, 6): n = utils.point_on_circle(point, math.radians(i), turn_radius) #, point.rotation) circle.append(n.tuple) self.agent.renderer.draw_polyline_3d(circle, self.agent.renderer.red()) # ball_prediction = self.agent.get_ball_prediction_struct() # if ball_prediction is not None: # # for i in range(0, ball_prediction.num_slices): # if self.target == self.master.ball.location: # self.target = structs.Vector3(ball_prediction.slices[10].physics.location) # turn_radius = utils.turn_radius(self.car, self.target) # goal_to_ball_uv = (self.agent.game_info.their_goal.location - self.agent.game_info.ball.location).normalize() # yaw = math.atan2(goal_to_ball_uv.x, goal_to_ball_uv.z) #math.atan((goal_to_ball_uv.x/(-goal_to_ball_uv.y)))# # pitch = 0#math.atan(math.hypot(goal_to_ball_uv.x, goal_to_ball_uv.y)/goal_to_ball_uv.z) # target_ang = structs.Rotator(pitch, yaw, 0) # circle_point = utils.point_on_circle(self.target, math.pi/2, turn_radius, target_ang) # circle = [] # for i in range(0, 366, 6): # n = utils.point_on_circle(circle_point, math.radians(i), turn_radius) # circle.append(n.tuple) # self.agent.renderer.draw_polyline_3d(circle, self.agent.renderer.black()) # x1 = point.x # y1 = point.y # x2 = circle_point.x # y2 = circle_point.y # gamma = -math.atan((y2-y1)/(x2-x1)) # beta = math.asin((turn_radius-turn_radius)/math.sqrt((x2-x1)**2 + (y2-y1)**2)) # alpha = gamma-beta # x3 = x1 - turn_radius*math.cos((math.pi/2)-alpha) # y3 = y1 - turn_radius*math.sin((math.pi/2)-alpha) # x4 = x2 + turn_radius*math.cos((math.pi/2)-alpha) # y4 = y2 + turn_radius*math.sin((math.pi/2)-alpha) # tang1 = structs.Vector3(x3, y3, 100) # tang2 = structs.Vector3(x4, y4, 100) # if self.last_target_ang != target_ang: # print('ye') # self.cur_hit = 0 # self.last_target_ang = target_ang # self.to_hit = [tang1, tang2, self.agent.game_info.ball.location] # self.agent.renderer.draw_rect_3d(self.to_hit[0].tuple, 20, 20, True, self.agent.renderer.black()) # self.agent.renderer.draw_rect_3d(self.to_hit[1].tuple, 20, 20, True, self.agent.renderer.black()) # point_to_target = utils.distance2D(self.target, point) # if point_to_target < turn_radius: # #inside circle # if abs(utils.angle2D(self.target, self.car)) < math.pi/2: # to_target = utils.localize(self.target, self.car).normalize(turn_radius) # self.target = self.car.location - to_target # self.controls.boost = 0 # self.controls.throttle = 0 # else: # to_target = utils.localize(self.target, self.car).normalize(turn_radius) # self.target = self.car.location - to_target # self.controls.boost = 0 # self.controls.handbrake = 1 # point2 = structs.Vector3(x,y,z) # self.agent.renderer.draw_rect_3d(point.tuple, 10, 10, True, self.agent.renderer.blue()) if isinstance(self.master, states.quickShoot): if self.path: if not self.path.on_path(self.car.location): # self.path = None print('yes') else: curvetarget = self.path.evaluate(0.1) curvetarget = structs.Vector3(curvetarget[0], curvetarget[1], 0) self.controls.steer = utils.steer(curvetarget, self.car) self.agent.renderer.draw_rect_3d( curvetarget.tuple, 10, 10, True, self.agent.renderer.blue()) if not self.path: b_prediction = self.agent.get_ball_prediction_struct() target = utils.find_point_on_line( self.agent.game_info.their_goal.location, self.target, -.05) self.path = structs.Path(self.agent, [ self.car.location, utils.point_on_circle(self.car, 0, 500), 'TP', target, self.target ], utils.sign(angle_to_target), self.car.rotation, self.car) angletotarget = utils.map( abs(utils.angle2D(self.target, self.car)), 0, math.pi, 0, math.pi) curvetarget = self.path.evaluate(0.1) curvetarget = structs.Vector3(curvetarget[0], curvetarget[1], 0) self.agent.renderer.draw_rect_3d(curvetarget.tuple, 10, 10, True, self.agent.renderer.blue()) self.controls.steer = utils.steer(curvetarget, self.car) elif utils.distance2D(point, self.target) < turn_radius: to_target = utils.localize(self.target, self.car).normalize(turn_radius) self.target = self.car.location - to_target self.controls.boost = False self.controls.steer = utils.steer(self.target, self.car) else: self.controls.steer = utils.steer(self.target, self.car) if self.path: # print(self.path.on_path(self.car.location)) self.agent.renderer.draw_polyline_3d( self.path.get_path_points(np.linspace(0, 1.0, 100)), self.agent.renderer.green()) self.agent.renderer.draw_rect_3d(self.target.tuple, 10, 10, True, self.agent.renderer.red()) self.agent.renderer.draw_line_3d(self.car.location.flatten().tuple, self.target.flatten().tuple, self.agent.renderer.green()) self.expired = True return self.controls
def __init__(self, space, position, radius = 50, poly = 6, seg_width = 8, mass = .001, friction = .1, elasticity = .5, group = 3, stiff = .2, damp = .02): # divisible by 360 a = 360/poly s = 360/a t = 0 points = [] for i in range(s): point = utils.point_on_circle(radius, radians(t), position) points.append(point) t += a shape_height = utils.distance(points[0], points[1]) width = seg_width size = (width,shape_height) box_moment = pymunk.moment_for_box(mass, size[0], size[1]) angle = 0 shapes = [] for point in points: box_body = pymunk.Body(mass, box_moment) box_body.position = point box_body.angle = radians(angle) box_shape = pymunk.Poly.create_box(box_body, size=size) box_shape.friction = friction box_shape.elasticity = elasticity box_shape.group = group space.add(box_body, box_shape) box_shape.size = size shapes.append(box_shape) angle += a rest_ln = radius*2 idx = (len(shapes)-1)/2 i = 0 for shape in shapes: if shape == shapes[0]: pin_const = pymunk.constraint.PivotJoint(shape.body, shapes[-1].body, Vec2d(0,-shape.size[1]/2), Vec2d(0,shape.size[1]/2)) space.add(pin_const) else: pin_const = pymunk.constraint.PivotJoint(shape.body, shapes[i-1].body, Vec2d(0,-shape.size[1]/2), Vec2d(0,shape.size[1]/2)) space.add(pin_const) if idx == len(shapes)-1: idx = 0 spr_const = pymunk.constraint.DampedSpring(shape.body, shapes[idx].body, Vec2d(0,0), Vec2d(0,0), rest_ln, stiff, damp) space.add(spr_const) idx += 1 i += 1