def rotate_matrix(cls, mat: hg.Matrix3, axe: hg.Vector3, angle): axeX = mat.GetX() axeY = mat.GetY() # axeZ=mat.GetZ() axeXr = cls.rotate_vector(axeX, axe, angle) axeYr = cls.rotate_vector(axeY, axe, angle) axeZr = hg.Cross(axeXr, axeYr) # cls.rotate_vector(axeZ,axe,angle) return hg.Matrix3(axeXr, axeYr, axeZr)
def update_kinetics(self, position: hg.Vector3, direction: hg.Vector3, v0: hg.Vector3, axisY: hg.Vector3, dts): self.num_new = 0 if not self.end: self.particles_cnt_f += dts * self.flow self.num_new = int(self.particles_cnt_f) - self.particles_cnt if self.num_new > 0: for i in range(self.num_new): if not self.loop and self.particles_cnt+i>=self.num_particles:break particle = self.particles[(self.particles_cnt + i) % self.num_particles] particle.age = 0 particle.delay = uniform(self.delay_range.x, self.delay_range.y) particle.scale = uniform(self.scale_range.x,self.scale_range.y) mat = particle.node.GetTransform() dir = self.get_direction(direction) rot_mat = hg.Matrix3(hg.Cross(axisY, dir), axisY, dir) mat.SetPosition(position + dir * self.start_offset) mat.SetRotationMatrix(rot_mat) mat.SetScale(self.start_scale) particle.rot_speed = hg.Vector3(uniform(self.rot_range_x.x, self.rot_range_x.y), uniform(self.rot_range_y.x, self.rot_range_y.y), uniform(self.rot_range_z.x, self.rot_range_z.y)) particle.v_move = v0 + dir * uniform(self.start_speed_range.x, self.start_speed_range.y) particle.node.SetEnabled(False) self.particles_cnt += self.num_new n=0 for particle in self.particles: if particle.age > particle.delay: particle.kill() elif particle.age == 0: particle.age += dts n+=1 elif particle.age > 0: n+=1 if not particle.node.GetEnabled(): particle.node.SetEnabled(True) t = particle.age / particle.delay mat = particle.node.GetTransform() pos = mat.GetPosition() rot = mat.GetRotation() particle.v_move += self.gravity * dts spd = particle.v_move.Len() particle.v_move -= particle.v_move.Normalized()*spd*self.linear_damping*dts pos += particle.v_move * dts rot += particle.rot_speed * dts pos.y=max(0,pos.y) mat.SetPosition(pos) mat.SetRotation(rot) mat.SetScale((self.start_scale * (1 - t) + self.end_scale * t)*particle.scale) # material = particle.node.GetObject().GetGeometry().GetMaterial(0) # material.SetFloat4("self_color",1.,1.,0.,1-t) self.update_color(particle) # particle.node.GetObject().GetGeometry().GetMaterial(0).SetFloat4("teint", 1,1,1,1) particle.age += dts if n==0 and not self.loop: self.end=True
# - Python tutorial - # Camera follow # =========================================================== import harfang as hg from math import radians, sqrt from MathsSupp import * # ================================================================================================== # Camera follow # ================================================================================================== track_position = hg.Vector3(0, 4, -20) track_orientation = hg.Matrix3(hg.Vector3(1, 0, 0), hg.Vector3(0, 1, 0), hg.Vector3(0, 0, 1)) pos_inertia = 0.2 rot_inertia = 0.07 follow_inertia = 0.01 follow_distance = 200 target_point = hg.Vector3(0, 0, 0) target_matrix = hg.Matrix3() target_node = None satellite_camera = None satellite_view_size = 100 satellite_view_size_inertia = 0.7
def update_radar(Main, plus, aircraft, targets): value = Main.render_to_texture.value t = hg.time_to_sec_f(plus.GetClock()) rx, ry = 150 / 1600 * Main.resolution.x, 150 / 900 * Main.resolution.y rs = 200 / 1600 * Main.resolution.x rm = 6 / 1600 * Main.resolution.x radar_scale = 4000 plot_size = 12 / 1600 * Main.resolution.x / 2 plus.Sprite2D(rx, ry, rs, "assets/sprites/radar.png", hg.Color(1, 1, 1, value)) mat = aircraft.get_parent_node().GetTransform().GetWorld() aZ = mat.GetZ() aZ.y = 0 aZ.Normalize() aY = mat.GetY() if aY.y < 0: aY = hg.Vector3(0, -1, 0) else: aY = hg.Vector3(0, 1, 0) aX = hg.Cross(aY, aZ) mat_trans = hg.Matrix4.TransformationMatrix(mat.GetTranslation(), hg.Matrix3(aX, aY, aZ)).InversedFast() for target in targets: if not target.wreck and target.activated: t_mat = target.get_parent_node().GetTransform().GetWorld() aZ = t_mat.GetZ() aZ.y = 0 aZ.Normalize() aY = hg.Vector3(0, 1, 0) aX = hg.Cross(aY, aZ) t_mat_trans = mat_trans * hg.Matrix4.TransformationMatrix( t_mat.GetTranslation(), hg.Matrix3(aX, aY, aZ)) pos = t_mat_trans.GetTranslation() v2D = hg.Vector2(pos.x, pos.z) / radar_scale * rs / 2 if abs(v2D.x) < rs / 2 - rm and abs(v2D.y) < rs / 2 - rm: if target.type == Destroyable_Machine.TYPE_MISSILE: plot = Main.texture_hud_plot_missile elif target.type == Destroyable_Machine.TYPE_AIRCRAFT: plot = Main.texture_hud_plot_aircraft elif target.type == Destroyable_Machine.TYPE_SHIP: plot = Main.texture_hud_plot_ship t_mat_rot = t_mat_trans.GetRotationMatrix() ps = plot_size a = 0.5 + 0.5 * abs(sin(t * uniform(1, 500))) else: if target.type == Destroyable_Machine.TYPE_MISSILE: continue dir = v2D.Normalized() v2D = dir * (rs / 2 - rm) ps = plot_size plot = Main.texture_hud_plot_dir aZ = hg.Vector3(dir.x, 0, dir.y) aX = hg.Cross(hg.Vector3.Up, aZ) t_mat_rot = hg.Matrix3(aX, hg.Vector3.Up, aZ) a = 0.5 + 0.5 * abs(sin(t * uniform(1, 500))) cx, cy = rx + v2D.x, ry + v2D.y if target.nationality == 1: c = hg.Color(0.25, 1., 0.25, a) elif target.nationality == 2: c = hg.Color(1., 0.5, 0.5, a) c.a *= value p1 = t_mat_rot * hg.Vector3(-plot_size, 0, -ps) p2 = t_mat_rot * hg.Vector3(-plot_size, 0, ps) p3 = t_mat_rot * hg.Vector3(plot_size, 0, ps) p4 = t_mat_rot * hg.Vector3(plot_size, 0, -ps) plus.Quad2D(cx + p1.x, cy + p1.z, cx + p2.x, cy + p2.z, cx + p3.x, cy + p3.z, cx + p4.x, cy + p4.z, c, c, c, c, plot) c = hg.Color(1, 1, 1, value * max(pow(1 - aircraft.health_level, 2), 0.05)) plus.Quad2D(rx - rs / 2, ry - rs / 2, rx - rs / 2, ry + rs / 2, rx + rs / 2, ry + rs / 2, rx + rs / 2, ry - rs / 2, c, c, c, c, Main.texture_noise, 0.25 + 0.25 * sin(t * 103), (0.65 + 0.35 * sin(t * 83)), 0.75 + 0.25 * sin(t * 538), 0.75 + 0.25 * cos(t * 120)) c = hg.Color(1, 1, 1, value) plus.Sprite2D(rx, ry, rs, "assets/sprites/radar_light.png", hg.Color(1, 1, 1, 0.3 * value)) plus.Sprite2D(rx, ry, rs, "assets/sprites/radar_box.png", c)