Commit ccee4646 authored by Elias Broschin's avatar Elias Broschin
Browse files

click in world works

parent 7afd4a57
......@@ -22,6 +22,7 @@
#include <input/input.hpp>
#include <time.h>
#include <gameplay/level_manager.h>
#include <glm/gtx/intersect.hpp>
namespace gdw {
......@@ -60,11 +61,33 @@ void play_state::handle_player_input() {
}
if(input.isMouseButtonDown(1)) {
float x = (input.mousePos().x/800.0f) * 2.0f -1;
float y = (input.mousePos().y/600.0f) * 2.0f -1;
auto cam = level_manager_->camera()->component<camera_component>();
auto world_pos = glm::inverse(cam->projection_view()) * glm::vec4(input.mousePos(),0.f,1.f);
world_pos /= world_pos.w;
level_manager_->create_asteroid(glm::vec2(world_pos));
std::cout << world_pos.x << "/" << world_pos.y << std::endl;
auto camera = level_manager_->camera();
auto mouse_pos = glm::vec2(x, -y);
auto inv_proj_view = glm::inverse(cam->projection_view());
auto world_pos_near = inv_proj_view * glm::vec4(mouse_pos,0.1f,1.f);
auto world_pos_far = inv_proj_view * glm::vec4(mouse_pos,0.9f,1.f);
world_pos_near /= world_pos_near.w;
world_pos_far /= world_pos_far.w;
auto ray_dir = world_pos_far - world_pos_near;
auto final_pos = world_pos_near + ray_dir;
auto& entity_manager = engine_.entity_manager();
//auto plane_normal = glm::normalize(glm::angleAxis(glm::radians(90.f), glm::vec3(1,0,0)) * glm::vec3(0,0,1));
auto plane_normal = glm::vec3(0,0,1);
float d = glm::dot(glm::vec3(0,15,0) - glm::vec3(world_pos_near), plane_normal) / glm::dot(glm::vec3(glm::normalize(-ray_dir)), plane_normal);
auto intersection_point = camera->position()-glm::vec3((glm::normalize(ray_dir))*d);
auto &asteroid = entity_manager.emplace_back(intersection_point, glm::angleAxis(glm::radians(0.f), glm::vec3(0.f, 0.f, 0.f)));
asteroid.emplace_back<staticmesh_component>("mesh/cube.msh");
asteroid.scale(glm::vec3(1.f));
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment