Skip to content

Commit 368cabc

Browse files
committed
path: Change goal with right mouse button.
1 parent 810822b commit 368cabc

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

libopenage/pathfinding/demo/demo_0.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "demo_0.h"
44

55
#include <QKeyEvent>
6+
#include <QMouseEvent>
67

78
#include "pathfinding/cost_field.h"
89
#include "pathfinding/flow_field.h"
@@ -596,6 +597,27 @@ void path_demo_0(const util::Path &path) {
596597
auto flow_field = std::make_shared<FlowField>(10);
597598
flow_field->build(integration_field);
598599

600+
window.add_mouse_button_callback([&](const QMouseEvent &ev) {
601+
if (ev.type() == QEvent::MouseButtonRelease) {
602+
if (ev.button() == Qt::RightButton) {
603+
auto grid_plane_normal = Eigen::Vector3f{0, 1, 0};
604+
auto grid_plane_point = Eigen::Vector3f{0, 0, 0};
605+
auto camera_direction = renderer::camera::cam_direction;
606+
auto camera_position = camera->get_input_pos(
607+
coord::input{ev.position().x(), ev.position().y()});
608+
609+
Eigen::Vector3f intersect = camera_position + camera_direction * (grid_plane_point - camera_position).dot(grid_plane_normal) / camera_direction.dot(grid_plane_normal);
610+
auto grid_x = static_cast<int>(-1 * intersect[2]);
611+
auto grid_y = static_cast<int>(intersect[0]);
612+
613+
if (grid_x >= 0 && grid_x < 10 && grid_y >= 0 && grid_y < 10) {
614+
integration_field->integrate(cost_field, grid_x, grid_y);
615+
flow_field->build(integration_field);
616+
}
617+
}
618+
}
619+
});
620+
599621
// Create object for the cost field
600622
// this will be shown at the start
601623
Eigen::Matrix4f model = Eigen::Matrix4f::Identity();

0 commit comments

Comments
 (0)