def update_directional_speed(self, b_obj, speedf, balance=False): direction = self.directional_speed(b_obj.direction, speedf) if balance and tools.vector_size(direction) > 0: perpedicular_dir = (- direction[1], direction[0]) dot = (b_obj.velocities[0] * perpedicular_dir[0] + b_obj.velocities[2] * perpedicular_dir[1]) / \ (perpedicular_dir[0] * perpedicular_dir[0] + perpedicular_dir[1] * perpedicular_dir[1]) if dot < 0: dot *= -1 perpedicular_dir = (direction[1], - direction[0]) direction = (direction[0] - perpedicular_dir[0] * dot, direction[1] - perpedicular_dir[1] * dot) self.turn_to_direction(b_obj, direction[0], direction[1]) b_obj.velocities[0] += direction[0] b_obj.velocities[2] += direction[1]
def handle_water_movement(self, b_obj): is_in_water = False water_current = (0, 0, 0) bb = b_obj.aabb.expand(-0.001, -0.4010000059604645, -0.001) top_y = tools.grid_shift(bb.max_y + 1) for blk in self.world.grid.blocks_in_aabb(bb): if isinstance(blk, blocks.BlockWater): if top_y >= (blk.y + 1 - blk.height_percent): is_in_water = True water_current = blk.add_velocity_to(water_current) if tools.vector_size(water_current) > 0: water_current = tools.normalize(water_current) wconst = 0.014 water_current = (water_current[0] * wconst, water_current[ 1] * wconst, water_current[2] * wconst) b_obj.velocities = [b_obj.velocities[0] + water_current[0], b_obj.velocities[1] + water_current[1], b_obj.velocities[2] + water_current[2]] return is_in_water
def min_collision_between(self, bb1, bb2, horizontal=False, max_height=False): ubb = bb1.extend_to(dy=-1).union(bb2.extend_to(dy=-1)) blcks = self.blocks_in_aabb(ubb) dvect = bb1.vector_to(bb2) if horizontal: dvect = (dvect[0], 0, dvect[2]) col_rel_d = 1.1 col_bb = None for blk in blcks: col, rel_d, bb = blk.sweep_collision( bb1, dvect, max_height=max_height) if col and fops.eq(col_rel_d, rel_d): if max_height: if fops.lt(col_bb.max_y, bb.max_y): col_bb = bb if col and fops.lt(rel_d, col_rel_d): col_rel_d = rel_d col_bb = bb if col_bb is not None: return col_rel_d * tools.vector_size(dvect), col_bb else: return None, None