From d240084aa7e41725a0228e123080aa7fe8a241b7 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Wed, 6 Aug 2008 19:35:49 +0000 Subject: target hovering, cursors and mouse and joystick interaction --- src/client/targets.cc | 61 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 3 deletions(-) (limited to 'src/client/targets.cc') diff --git a/src/client/targets.cc b/src/client/targets.cc index b16ae13..128064e 100644 --- a/src/client/targets.cc +++ b/src/client/targets.cc @@ -197,6 +197,45 @@ void func_target_prev(std::string const &args) } } +void func_target_none(std::string const &args) +{ + current_target = 0; + current_target_id = 0; +} + +void func_target_center(std::string const &args) +{ + if (!core::localcontrol()) + return; + + // this is essentialy the hover algorithm with the cursor in the center + core::Entity *new_target = 0; + math::Vector3f center = render::Camera::eye() + render::Camera::axis().forward() * (render::Camera::frustum_front() + 0.001); + float smallest_d = -1; + + for (core::Zone::Content::iterator it=core::localcontrol()->zone()->content().begin(); it != core::localcontrol()->zone()->content().end(); it++) { + core::Entity *entity = (*it); + + math::Vector3f v(entity->state()->location() - render::Camera::eye()); + v.normalize(); + + if (is_legal_target(entity) && math::dotproduct(render::Camera::axis().forward(), v) > 0.5 ) { + + // calculate the distance from entity location to the line [eye - cursor] + float d = math::Vector3f::length(math::crossproduct( (center - render::Camera::eye()) , (render::Camera::eye() - entity->location()))) / math::Vector3f::length(center - render::Camera::eye()); + + // the entity closer to the center beam + if (smallest_d < 0 || d < smallest_d) { + new_target = entity; + smallest_d = d; + } + } + } + + if (new_target) + select_target(new_target); +} + void reset() { current_target = 0; @@ -218,6 +257,12 @@ void init() core::Func::add("target_prev", func_target_prev); func->set_info("select previous target"); + core::Func::add("target_none", func_target_none); + func->set_info("deselect current target"); + + core::Func::add("target_center", func_target_center); + func->set_info("select target near center"); + reset(); } @@ -227,6 +272,8 @@ void shutdown() core::Func::remove("target_next"); core::Func::remove("target_prev"); + core::Func::remove("target_none"); + core::Func::remove("target_center"); } void render_listener_sound() @@ -378,8 +425,16 @@ void draw() float z = -1; // mouse cursor location in 3d world space - float x = (float)(input::mouse_position_x() - video::width /2) / (float)video::width; - float y = (float)(input::mouse_position_y() - video::height /2) / (float)video::height / render::Camera::aspect(); + float x = 0; + float y = 0; + + if ((input::joystick_lastmoved_time() > input::mouse_lastmoved_time()) && (render::Camera::mode() == render::Camera::Cockpit || render::Camera::mode() == render::Camera::Track)) { + x = 0; + y = 0; + } else { + x = (float)(input::mouse_position_x() - video::width /2) / (float)video::width; + y = (float)(input::mouse_position_y() - video::height /2) / (float)video::height / render::Camera::aspect(); + } Vector3f cursor = render::Camera::eye() + render::Camera::axis().forward() * (render::Camera::frustum_front() + 0.001); cursor -= render::Camera::axis().left() * x; @@ -408,7 +463,7 @@ void draw() if (math::dotproduct(render::Camera::axis().forward(), v) > 0.75 ) { - // caculate the distance from entity location to the line [eye - cursor] + // 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 r = entity->radius() * 0.5f; -- cgit v1.2.3