Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented basic unit rotation (bugs :( )

  • Loading branch information...
commit 5619438d867f0436dba46804defba16c31ab46d9 1 parent 845f619
@ozkriff authored
View
4 core/misc.rs
@@ -9,6 +9,10 @@ pub fn deg_to_rad(n: MFloat) -> MFloat {
n * PI / 180.0
}
+pub fn rad_to_deg(n: MFloat) -> MFloat {
+ (n * 180.0) / PI
+}
+
pub fn read_file(path: &Path) -> ~str {
if !path.exists() {
fail!("Path does not exists: {}", path.display());
View
16 visualizer/event_visualizer.rs
@@ -1,10 +1,6 @@
// See LICENSE file for copyright and license details.
-use cgmath::vector::{
- Vec3,
- Vector,
- EuclideanVector,
-};
+use cgmath::vector::{Vec3, Vector, EuclideanVector};
use visualizer::geom::Geom;
use core::types::{MBool, MInt, MapPos, UnitId};
use core::game_state::GameState;
@@ -46,8 +42,12 @@ pub struct EventMoveVisualizer {
}
impl EventVisualizer for EventMoveVisualizer {
- fn start(&mut self, geom: &Geom, _: &mut Scene, _: &GameState) {
+ fn start(&mut self, geom: &Geom, scene: &mut Scene, _: &GameState) {
self.reset_dir(geom);
+ let node_id = unit_id_to_node_id(self.unit_id);
+ let node = scene.get_mut(&node_id);
+ node.rot = geom.get_rot_angle(
+ self.current_waypoint(), self.next_waypoint());
}
fn is_finished(&self) -> MBool {
@@ -66,6 +66,8 @@ impl EventVisualizer for EventMoveVisualizer {
let _ = self.path.shift();
if self.path.len() > 1 {
self.reset_dir(geom);
+ node.rot = geom.get_rot_angle(
+ self.current_waypoint(), self.next_waypoint());
}
node.pos = self.current_waypoint();
}
@@ -161,7 +163,7 @@ impl EventVisualizer for EventCreateUnitVisualizer {
fn start(&mut self, geom: &Geom, scene: &mut Scene, state: &GameState) {
let node_id = unit_id_to_node_id(self.id);
let world_pos = unit_pos(self.id, self.pos, geom, state);
- scene.insert(node_id, SceneNode{pos: world_pos});
+ scene.insert(node_id, SceneNode{pos: world_pos, rot: 0.0});
}
fn is_finished(&self) -> MBool {
View
11 visualizer/geom.rs
@@ -1,9 +1,10 @@
// See LICENSE file for copyright and license details.
use std::f32::consts::{PI, FRAC_PI_2 };
-use std::num::{sqrt, pow, sin, cos, abs};
+use std::num::{sqrt, pow, sin, asin, cos, abs};
use cgmath::vector::{Vec2, Vec3, Vector};
use core::types::{MInt, MapPos};
+use core::misc::{rad_to_deg};
use visualizer::types::{WorldPos, MFloat, VertexCoord};
pub struct Geom {
@@ -52,6 +53,14 @@ impl Geom {
let dy = abs(b.y - a.y);
sqrt(pow(dx, 2) + pow(dy, 2))
}
+
+ pub fn get_rot_angle(&self, a: WorldPos, b: WorldPos) -> MFloat {
+ let mut angle = rad_to_deg(asin((b.x - a.x) / self.dist(a, b)));
+ if b.y - a.y > 0.0 {
+ angle = -(180.0 + angle);
+ }
+ angle
+ }
}
// vim: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab:
View
2  visualizer/types.rs
@@ -22,7 +22,7 @@ pub struct MatId(GLuint);
pub struct SceneNode {
pos: WorldPos,
- // rot: Angle,
+ rot: MFloat,
}
#[deriving(Ord, Eq, Hash)]
View
6 visualizer/visualizer.rs
@@ -18,6 +18,7 @@ use visualizer::gl_helpers::{
load_gl_funcs_with,
set_viewport,
tr,
+ rot_z,
};
use visualizer::camera::Camera;
use visualizer::geom::Geom;
@@ -211,8 +212,9 @@ impl<'a> Visualizer<'a> {
}
fn draw_units(&self) {
- for (_, unit) in self.scene().iter() {
- let m = tr(self.camera.mat(), unit.pos);
+ for (_, node) in self.scene().iter() {
+ let mut m = tr(self.camera.mat(), node.pos);
+ m = rot_z(m, node.rot);
uniform_mat4f(self.mvp_mat_id, &m);
self.unit_mesh.draw(&self.shader);
}
Please sign in to comment.
Something went wrong with that request. Please try again.