def get_vertices(self): vertices = [ Vec2d(self.pos.x, self.pos.y), Vec2d(self.pos.x + self.w, self.pos.y), Vec2d(self.pos.x + self.w, self.pos.y + self.h), Vec2d(self.pos.x, self.pos.y + self.h) ] return vertices
def __init__(self, context, x, y, r): Circle.__init__(self, context, x, y, r, 1, (100, 200, 0)) self.mass = 1 self.force = Vec2d(0, 0) self.acc = Vec2d(0, 0) self.vel = Vec2d(0, 0) self.power = 12 self.keys = {}
def get_vertices(self, rot): vertices = [ Vec2d(self.mid.x - self.w / 2, self.mid.y - self.h / 2), Vec2d(self.mid.x + self.w / 2, self.mid.y - self.h / 2), Vec2d(self.mid.x + self.w / 2, self.mid.y + self.h / 2), Vec2d(self.mid.x - self.w / 2, self.mid.y + self.h / 2), Vec2d(self.mid.x - self.w / 2, self.mid.y - self.h / 2) ] return [ self.mid + (vertex - self.mid).rotate(rot) for vertex in vertices ]
def __init__(self, context, x, y, immovable=False, imrotatable=False): self.immovable = immovable self.imrotatable = imrotatable Entity.__init__(self, context, x, y) self.vel = Vec2d(0, 0) self.acc = Vec2d(0, 0) self.force = Vec2d(0, 0) self.rot = 0 self.rot_vel = 0 self.rot_acc = 0 self.tourqe = 0
def __init__(self, context, x, y): self.context = context self.px2m = self.context.px2m_func self.px2m_tuple = self.context.px2m_tuple self.screen_w = self.context.screen_size_m.w self.screen_h = self.context.screen_size_m.h self.pos = Vec2d(x, y)
def circle_is_on_line(circle_mid, circle_rad, line_start, line_end): collision_point, length_to_line = length_from_point_to_line( circle_mid, line_start, line_end) if length_to_line < circle_rad: line_min = Vec2d(min(line_start.x, line_end.x), min(line_start.y, line_end.y)) line_max = Vec2d(max(line_start.x, line_end.x), max(line_start.y, line_end.y)) if line_start.x != line_end.x: if collision_point.x > line_min.x and collision_point.x < line_max.x: return True, collision_point, circle_rad - length_to_line else: if collision_point.y > line_min.y and collision_point.y < line_max.y: return True, collision_point, circle_rad - length_to_line return circle_is_on_corner(circle_mid, circle_rad, line_start, line_end) return False, None, None
def __init__(self, context, x, y, vx, vy): Entity.__init__(self, context, x, y) self.vec = Vec2d(vx, vy) self.Surface = pygame.Surface(self.px2m_tuple(*abs(self.vec))) self.Surface.fill((255, 255, 255)) self.Surface.set_colorkey((255, 255, 255)) pygame.draw.lines(self.Surface, (0, 0, 255), False, [(0, 0), self.px2m_tuple(*self.vec)], 1) self.Surface = self.Surface.convert()
def __init__(self, vertices): self.vertices = vertices if self.vertices[0].x != self.vertices[-1].x or self.vertices[ 0].y != self.vertices[-1].y: self.vertices.append(Vec2d(self.vertices[0].x, self.vertices[0].y)) self.mid = self.get_mid() self.vertices = [vertex - self.mid for vertex in self.vertices] self.edges = self.get_edges() self.normals = self.get_normals()
def get_hitbox(self): top = self.points[0].y bottom = self.points[0].y left = self.points[0].x right = self.points[0].x for point in self.points[1:]: top = min(top, point.y) bottom = max(bottom, point.y) left = min(left, point.x) right = max(right, point.x) return Hitbox(Vec2d(left, top), right - left, bottom - top)
def physics_update(self, dt): if not self.immovable: self.acc = self.force/self.mass self.vel += self.acc*dt self.pos += self.vel*dt self.force = Vec2d(0, 0) if not self.imrotatable: self.rot_acc = self.tourqe/self.moofin self.rot_vel += self.rot_acc*dt self.rot += self.rot_vel*dt self.tourqe = 0
def get_normals(self): normals = [] for edge in self.edges: edge_vector = edge[1] - edge[0] new_normal = Vec2d(-edge_vector.y, edge_vector.x) / edge_vector.length for normal in normals: if abs(new_normal.dot(normal)) == 1: break else: normals.append(new_normal) return normals
def update_points(self, point_list): self.points = point_list # close points list if self.points[0].x != self.points[-1].x or self.points[ 0].y != self.points[-1].y: self.points.append(Vec2d(self.points[0].x, self.points[0].y)) self.mid = self.get_mid() self.edges = self.get_edges() self.hitbox = self.get_hitbox() self.pos = self.hitbox.pos self.surface_dirty = True
def __init__(self, context, x, y, w, h, density, rot=0): self.w = w self.h = h self.mid = Vec2d(x + self.w / 2, y + self.h / 2) mass = density * w * h moofin = (w**2 + h**2) / 12 * mass Polygon.__init__(self, context, self.get_vertices(rot), mass, moofin, immovable=True) self.rot = rot
def __init__(self, x, y, w, h): self.w = w self.h = h self.pos = Vec2d(x, y) Polygon.__init__(self, self.get_vertices())
from vector2 import Vec2d w = 3.2 h = 1 mass = 5 mid = Vec2d(w/2, h/2) vertices = [ Vec2d(0, 0), Vec2d(w, 0), Vec2d(w, h), Vec2d(0, h), Vec2d(0, 0) ] qmoofin = mass*(w**2 + h**2)/12 print("square moof:", qmoofin) def get_moofin(vertices, mass): # vertices must be a list with vertices[-1] == vertices[0] n = len(vertices) - 1 moofin = 0 total_area = 0 for i in range(0, n): area = abs(vertices[i+1].cross(vertices[i])) mid_corector = 3*mid.dot(mid - vertices[i] - vertices[i+1])
def __init__(self, pos, w, h): self.pos = pos self.type = type self.w = w self.h = h self.mid = Vec2d(self.pos.x + self.w / 2, self.pos.y + self.h / 2)