def spherical_billboarding(cam_pos, obj_pos): look_at = Vector3d(0., 0., 0.) obj_to_cam_proj = Vector3d(0., 0., 0.) up_aux = Vector3d(0., 0., 0.) angle_cosine = 0. obj_to_cam_proj.x = cam_pos.x - obj_pos.x obj_to_cam_proj.z = cam_pos.z - obj_pos.z look_at.z = 1. obj_to_cam_proj = obj_to_cam_proj.normalizing() up_aux = look_at.cross_product(obj_to_cam_proj) angle_cosine = look_at * obj_to_cam_proj if (angle_cosine < 0.99990 and angle_cosine > -0.9999): glRotatef(degrees(acos(angle_cosine)), *up_aux) obj_to_cam = (cam_pos - obj_pos).normalizing() angle_cosine = obj_to_cam_proj * obj_to_cam if (angle_cosine < 0.99990 and angle_cosine > -0.9999): if (obj_to_cam[1] < 0): glRotatef(degrees(angle_cosine), 1., 0., 0.) else: glRotatef(degrees(angle_cosine), -1., 0., 0.)
def __init__(self, mass = 0.0, pos = Vector3d(0.,0.,0.)): self.mass = mass self.position = pos self.velocity = Vector3d(0., 0., 0.) self.aceleration = Vector3d(0., 0., 0.) # angular velocity ( degree/sec ) self.velocity_angular_x = 0.0 self.velocity_angular_y = 0.0 self.velocity_angular_z = 0.0 self.rotation_center = Vector3d(pos.x, pos.y, pos.z) self.rotation_radius = None self.rot_xy = 0. self.rot_z = 0. self.rot_vel_xy = 0. self.rot_vel_z = 0. # angles ( degree ) self.angle_x = 0.0 self.angle_y = 0.0 self.angle_z = 0.0 # forces # vector list self.forces = [] # temporary force or impulse # vector list self.forces_tmp = [] # resistence force # positive real values list self.forces_res = [] self.forces_res_tmp = []
def update_spinning(self, time_elapsed): for dir in self.spinning: if (self.spinning[dir]): axis, sign = self.vectors[dir] angle = self.spin_velocity * time_elapsed * sign r = Quaternion.from_axis_angle(axis, angle) self.rotation = self.rotation * r self.ship_dir = self.rotation * Vector3d(0., 0., -1.) self.up_dir = self.rotation * Vector3d(0., 1., 0.)
def __init__(self, model, shape, element, level): Object.__init__(self, model, shape, element) cfg = Config('physics','Ship') self.move_force_sz = cfg.get('move_force') self.spin_velocity = cfg.get('spin_velocity') self.strafe_force = cfg.get('strafe_force') self.shape.forces_res.append(cfg.get('vacuum_resistance')) self.breake_rate = cfg.get('breake_rate') self.mouse_sensivity = Config('game','Mouse').get('sensivity') self.level = level self.rotation = Quaternion.from_axis_rotations(0.,0.,0.) self.ship_dir = None self.up_dir = None self.spinning = { 'up' : False, 'down' : False, 'left' : False, 'right' : False } self.vectors = { 'up' : (Vector3d.x_axis(), 1.), 'down' : (Vector3d.x_axis(), -1.), 'left' : (Vector3d.y_axis(), 1.), 'right' : (Vector3d.y_axis(), -1.) } self.strafe = { 'forward': False, 'left' : False, 'right' : False, 'breake' : False } self.strafe_vectors = { 'forward':Vector3d(0.,0.,-0.7), 'left' : Vector3d(-0.9,0.,0.), 'right' : Vector3d(0.9,0.,0.), 'breake' : Vector3d(0.,0.,1.) } self.angles = [0.,0.] self.mouse_target = [0.,0.] self.collision_set = set() self.keep_colliding = set()
def __mul__(self, q): self.normalize() if (isinstance(q, Quaternion)): q.normalize() r = Quaternion() r.w = self.w*q.w - self.x*q.x - self.y*q.y - self.z*q.z r.x = self.w*q.x + self.x*q.w + self.y*q.z - self.z*q.y r.y = self.w*q.y + self.y*q.w + self.z*q.x - self.x*q.z r.z = self.w*q.z + self.z*q.w + self.x*q.y - self.y*q.x return r elif (isinstance(q, Vector3d)): v = q.normalizing() vec_quat = Quaternion() res_quat = Quaternion() vec_quat.x = v.x vec_quat.y = v.y vec_quat.z = v.z vec_quat.w = 0. res_quat = vec_quat * self.conjugate() res_quat = self * res_quat return Vector3d(res_quat.x, res_quat.y, res_quat.z)
def __init__(self, model, shape, element, lvl, damage, target_obj, attract_v): Object.__init__(self, model, shape, element) self.lvl = lvl self.damage = damage self.target = target_obj self.velocity = attract_v self.dir = Vector3d(0.,0.,0.)
def __init__(self, model, shape, element, level): Object.__init__(self, model, shape, element) cfg = Config('physics', 'Ship') self.move_force_sz = cfg.get('move_force') self.spin_velocity = cfg.get('spin_velocity') self.strafe_force = cfg.get('strafe_force') self.shape.forces_res.append(cfg.get('vacuum_resistance')) self.breake_rate = cfg.get('breake_rate') self.mouse_sensivity = Config('game', 'Mouse').get('sensivity') self.level = level self.rotation = Quaternion.from_axis_rotations(0., 0., 0.) self.ship_dir = None self.up_dir = None self.spinning = { 'up': False, 'down': False, 'left': False, 'right': False } self.vectors = { 'up': (Vector3d.x_axis(), 1.), 'down': (Vector3d.x_axis(), -1.), 'left': (Vector3d.y_axis(), 1.), 'right': (Vector3d.y_axis(), -1.) } self.strafe = { 'forward': False, 'left': False, 'right': False, 'breake': False } self.strafe_vectors = { 'forward': Vector3d(0., 0., -0.7), 'left': Vector3d(-0.9, 0., 0.), 'right': Vector3d(0.9, 0., 0.), 'breake': Vector3d(0., 0., 1.) } self.angles = [0., 0.] self.mouse_target = [0., 0.] self.collision_set = set() self.keep_colliding = set()
def get_axis_angle(self): try: x, y, z, w = self.x, self.y, self.z, self.w s = sqrt(x**2 + y**2 + z**2) a = Vector3d(x / s, y / s, z / s) return (a, degrees(acos(self.w) * 2.)) except ZeroDivisionError: return (v3d(1.,0.,0.), 0.)
def calculate_aceleration(self): self.aceleration = sum(chain(self.forces, self.forces_tmp), Vector3d(0.,0.,0.)) f_res = sum(chain(self.forces_res, self.forces_res_tmp), 0.) if (f_res >= 1.0): f_res = 1.0 # clear temporary forces self.forces_tmp = [] self.forces_res_tmp = [] v = self.velocity.get_mod() res_mod = v * f_res self.aceleration = self.aceleration.scalar(1.0 / self.mass) self.aceleration = self.aceleration + self.velocity.normalizing().scalar(-res_mod) if (self.aceleration.get_mod() <= _eps): self.aceleration = Vector3d(0.,0.,0.)
def __init__(self, model, ship, level): self.model = model self.ship = ship self.level = level cfg = Config('game','SimpleMissile') self.pos = Vector3d(*cfg.get('pos')) self.initial_velocity = cfg.get('initial_velocity') self.attraction_vel = cfg.get('attraction_velocity') self.damage = cfg.get('damage') self.num_rockets = cfg.get('num_rockets')
def __init__(self, model, ship, level): self.model = model self.ship = ship self.level = level cfg = Config('game', 'SimpleGun') self.duration = cfg.get('duration') self.pos = Vector3d(*cfg.get('pos')) self.shoot_period = 1. / cfg.get('shoot_rate') self.shoot_velocity_sz = cfg.get('shoot_velocity') self.damage = cfg.get('damage') self.shooting = False self.since_last_shoot = 0.0
def update_mouse_track(self, time_elapsed): eps = 0.1 ax, ay = self.angles tx, ty = self.mouse_target if (fabs(ax - tx) <= eps): self.angles[0] = 0.0 self.mouse_target[0] = 0.0 else: before = sign(tx - ax) d = self.spin_velocity * time_elapsed * sign(tx - ax) self.angles[0] += d after = sign(tx - self.angles[0]) if (before != after): self.angles[0] = 0.0 self.mouse_target[0] = 0.0 r = Quaternion.from_axis_angle(Vector3d(0., 1., 0.), d) self.rotation = self.rotation * r if (fabs(ay - ty) <= eps): self.angles[1] = 0.0 self.mouse_target[1] = 0.0 else: before = sign(ty - ay) d = self.spin_velocity * time_elapsed * sign(ty - ay) self.angles[1] += d after = sign(ty - self.angles[1]) if (before != after): self.angles[1] = 0.0 self.mouse_target[1] = 0.0 r = Quaternion.from_axis_angle(Vector3d(1., 0., 0.), d) self.rotation = self.rotation * r
def calculate_velocity(self, delta): self.velocity = self.velocity + self.aceleration.scalar(delta) if (self.velocity.get_mod() <= _eps): self.velocity = Vector3d(0.,0.,0.)
def load_file(self, level_name): lvl = yaml.load(open('resources/levels/' + level_name + '.lvl')) self.title = lvl['name'] self.dimensions = lvl['scene']['dimensions'] self.time = float(lvl['time']) * 60 #models = {} for element in lvl['elements']: file = open('resources/models/' + element['model']['file'], 'r') name = element['name'] subtitle = element['subtitle'] translate = element['model'].get('translate', (0., 0., 0.)) rotate = element['model'].get('rotate', (0., 0., 0.)) scale = element['model'].get('scale', 1.) rc = element['model'].get('radius_correction', 0.) gl_model = GLModel(file, translate, rotate, scale, rc) self.models[name] = (gl_model, element) file.close() self.dimensions = Vector3d(*lvl['scene']['dimensions']) type_class = { 'planet': Planet, 'asteroid': Asteroid, 'start_portal': Portal, 'end_portal': Portal, 'enemyship': EnemyShip } id_table = {} obj_set = set() for object in lvl['scene']['objects']: element_name = object['element'] shape = None model, element = self.models[element_name] mass = float(object['mass']) rvel = object['rotation_velocity'] movement = object['movement']['type'] if (movement == 'static'): pos = Vector3d(*object['pos']) shape = Shape(mass, pos) elif (movement == 'orbit'): center_id = object['movement']['center_planet_id'] center_obj = id_table[center_id] center_pos = Vector3d(*center_obj.shape.position) shape = Shape(mass, center_pos) shape.rotation_radius = object['movement']['radius'] shape.rot_vel_xy = object['movement']['rot_velocity_xy'] shape.rot_vel_z = object['movement']['rot_velocity_z'] shape.rot_xy = object['movement']['rot_xy'] shape.rot_z = object['movement']['rot_z'] elif (movement == 'dynamic'): pos = Vector3d(*object['pos']) shape = Shape(mass, pos) shape.velocity = Vector3d(*object['movement']['velocity']) shape.velocity_angular_x = rvel[0] shape.velocity_angular_y = rvel[1] shape.velocity_angular_z = rvel[2] type = element['type'] _object = type_class[type](model, shape, element) if (type == 'asteroid'): _object.asteroid_type = object['movement']['asteroid_type'] elif (type == 'enemyship'): _object.level = self _object.gun_pos = Vector3d(*object['gun_position']) obj_set.add(_object) if ('id' in object): id_table[object['id']] = _object for obj in obj_set: self.add_object(obj) Player.get_instance().beginning_level(self) self.make_spaceship(lvl, self.models) self.make_guns(self.models)
from physics.vector3d import Vector3d from physics.shape import Shape sh = Shape(10.0) sh.forces.append(Vector3d(1., 2., 3.)) #sh.forces_res.append(0.5) sh.calculate_aceleration() print sh.aceleration.x