From 140ea71836bca67705a8643c11b5ce9efdabcc07 Mon Sep 17 00:00:00 2001
From: Stijn Buys <ingar@osirion.org>
Date: Sun, 30 Dec 2012 23:17:44 +0000
Subject: Added support for weapons mouse aiming.

---
 src/client/targets.cc | 33 +++++++++++++++++++++------------
 src/client/targets.h  |  3 +++
 2 files changed, 24 insertions(+), 12 deletions(-)

(limited to 'src/client')

diff --git a/src/client/targets.cc b/src/client/targets.cc
index 6809939..8a7f402 100644
--- a/src/client/targets.cc
+++ b/src/client/targets.cc
@@ -39,6 +39,7 @@ unsigned int current_target_id = 0;
 unsigned int current_hover = 0;
 
 const core::Entity *current_target = 0;
+math::Vector3f cursor_aim;
 
 bool is_valid_map_target(const core::Entity *entity)
 {
@@ -295,6 +296,11 @@ void shutdown()
 	core::Func::remove("target_center");
 }
 
+const math::Vector3f & aim()
+{
+	return cursor_aim;
+}
+
 // render targets and sounds (in world coordinates)
 void frame()
 {
@@ -327,14 +333,9 @@ void frame()
 		y = (float)(input::mouse_position_y() - render::State::height() / 2) / (float)render::State::height() / render::State::aspect();
 	}
 
-	Vector3f cursor = render::Camera::eye() + render::Camera::axis().forward() * (render::FRUSTUMFRONT + 0.001);
-	cursor -= render::Camera::axis().left() * x;
-	cursor -= render::Camera::axis().up() * y;
-	
-	// set aim
-	if (core::localcontrol()) {
-		core::localcontrol()->set_target_aim(cursor);
-	}
+	cursor_aim.assign(render::Camera::eye() + render::Camera::axis().forward() * (render::FRUSTUMFRONT + 0.001));
+	cursor_aim -= render::Camera::axis().left() * x;
+	cursor_aim -= render::Camera::axis().up() * y;
 
 	//math::Vector3f center = render::Camera::eye() + (render::Camera::axis().forward() * (render::FRUSTUMFRONT + 0.001f));
 	for (core::Zone::Content::iterator it = zone->content().begin(); it != zone->content().end(); it++) {
@@ -359,7 +360,7 @@ void frame()
 			if (math::dotproduct(render::Camera::axis().forward(), v) > 0.75) {
 
 				// calculate the distance from entity location to the line [eye - cursor]
-				float d = math::Vector3f::length(math::crossproduct((cursor - render::Camera::eye()) , (render::Camera::eye() - entity->location()))) / math::Vector3f::length(cursor - render::Camera::eye());
+				float d = math::Vector3f::length(math::crossproduct((cursor_aim - render::Camera::eye()) , (render::Camera::eye() - entity->location()))) / math::Vector3f::length(cursor_aim - render::Camera::eye());
 
 				float r = entity->radius() * 0.5f;
 
@@ -372,7 +373,7 @@ void frame()
 
 				// if the cursor-beam hits the entity sphere
 				if (d < r) {
-					float myz = math::distance(cursor, entity->location());
+					float myz = math::distance(cursor_aim, entity->location());
 					if (z < 0 || myz < z) {
 						current_hover = entity->id();
 						z = myz;
@@ -384,13 +385,21 @@ void frame()
 		}
 	}
 
+	float d = 64.0f;	// default aim distance
+	
 	if (!current_target) {
 		current_target_id = 0;
 	} else {
 		current_target_id  = current_target->id();
-
-
+		d = math::distance(render::Camera::eye(), current_target->location());
+	}
+	cursor_aim = render::Camera::eye() + (cursor_aim - render::Camera::eye())  / math::distance(render::Camera::eye(), cursor_aim)  * d;
+	
+	// set aim
+	if (core::localcontrol()) {		
+		core::localcontrol()->set_target_aim(cursor_aim);
 	}
+
 }
 
 }
diff --git a/src/client/targets.h b/src/client/targets.h
index e66ff97..ed681c3 100644
--- a/src/client/targets.h
+++ b/src/client/targets.h
@@ -48,6 +48,9 @@ void set_target(unsigned int id);
 /// target a specific entity
 void set_target(const core::Entity *entity);
 
+/// return current cursor aim location in world space
+const math::Vector3f &cursor_aim();
+
 }
 
 }
-- 
cgit v1.2.3