def __init__(self, pos, collidable, world): self.pos = pos self.collidable = collidable self.mech = Physics(pos, 10, (0, 0)) self.contact = Distance_Contact() self.world = world
class Actor: def __init__(self, pos, collidable, world): self.pos = pos self.collidable = collidable self.mech = Physics(pos, 10, (0, 0)) self.contact = Distance_Contact() self.world = world def move_to(self, x, y): self.pos = x, y self.collidable.set_pos(x, y) def move_mech_to(self, x, y): self.mech.pos.x = x self.mech.pos.y = y def move(self, dx, dy): self.pos = self.pos[0] + dx, self.pos[1] + dy self.collidable.move(dx, dy) def move_mech(self, dx, dy): self.mech.pos.x += dx self.mech.pos.y += dy def nearby_collidables(self): return self.world def update(self): self.mech.update() if self.contact.incontact: u = unit_vector(self.contact.edge) d = dot_product(self.mech.velocity, u) self.mech.pos -= self.mech.velocity self.mech.velocity = u * d self.mech.pos += self.mech.velocity n = self.contact.normal self.collidable.move(-n.x, -n.y) status, poly_pts = collision(self.collidable, self.contact.collidable) if not status: self.contact.break_contact() else: pass # self.mech.add_impulse(0, -9.81) self.collidable.move(n.x, n.y) v = self.mech.velocity.__tuple__() p = self.mech.pos.__tuple__() self.move_to(*p) for c in self.nearby_collidables(): status, polygon_points = collision(self.collidable, c) if status == True: self.resolve_collision(polygon_points, p, v, c, "slide") def resolve_collision(self, polygon_points, p, v, c, mechanic): pen_vector = penetration_vector(polygon_points, (0, 0)) d, v = collision_info(polygon_points, v) self.move(*pen_vector) self.move_mech(*pen_vector) if mechanic == "slide": self.contact.new_contact(v, pen_vector, c) elif mechanic == "bounce": self.mech.velocity = mirror_vector(self.mech.velocity, v) def jump(self): self.contact.break_contact() u = unit_vector(self.contact.edge) normal = u.get_normal() avg = u * 0.5 + normal * 0.5 jvect = avg * -u.y + normal * -u.x print(jvect) # if self.mech.velocity = jvect * GRAVITY * 0.5 + self.mech.velocity * 0.5