Commit 1af4351f authored by Georg Schaefer's avatar Georg Schaefer
Browse files

add proper cannon rotation and beam position / size

parent 20a6400c
......@@ -16,7 +16,7 @@ public:
void update(float dt);
glm::vec3 get_world_mouse_pos();
void render_ray(ray* r, float length, float radius, glm::vec3 color);
void render_ray(ray* r, const glm::vec3& offset, float length, float radius, bool invert, glm::vec3 color);
void process_beam_collision(ray& r, glm::vec3 &ray_unnormalized, float &beam_velocity, float &ray_length_, bool &asteroid_hit, int &portal_type, float &remaining_ray_length);
private:
void handle_key_input(float dt);
......
......@@ -72,6 +72,9 @@ private:
maploader maploader_;
float map_load_timer_;
float plane_offset_y_;
glm::vec4 cannon_socket_offset_;
glm::vec4 cannon_offset_;
bool is_level_build_;
};
......
......@@ -32,6 +32,10 @@ public:
return collision_data_manager_;
}
auto& collision_components() {
return collision_components_;
}
private:
void register_movement_component(movement_component* comp);
void unregister_movement_component(movement_component* comp);
......
#include <glm/gtx/string_cast.hpp>
#include <gameplay/game_input_manager.h>
#include <core/engine.hpp>
#include <SDL.h>
......@@ -60,7 +62,7 @@ namespace gdw {
auto plane_normal = glm::vec3(0,1,0); //Hardcoded ground plane
//HNF
float d = glm::dot(glm::vec3(0,3,15) - glm::vec3(world_pos_near), plane_normal) / glm::dot(glm::vec3(glm::normalize(-ray_dir)), plane_normal);
float d = glm::dot(glm::vec3(0.f, 0.f, 0.f) - glm::vec3(world_pos_near), plane_normal) / glm::dot(glm::vec3(glm::normalize(-ray_dir)), plane_normal);
return camera->position()-glm::vec3((glm::normalize(ray_dir))*d);
}
......@@ -145,24 +147,13 @@ namespace gdw {
auto intersection_point = get_world_mouse_pos();
auto cannon = level_manager.cannon();
auto cannon_pos = glm::vec3(cannon->position().x, 3.f, cannon->position().z);
ray r(cannon_pos, intersection_point);
//SPAWN LASER
auto v1 = glm::vec3(0,0,-1);
auto v2 = glm::normalize(r.destination-r.source);
auto angle = std::acos(glm::dot(v1, v2) / glm::length(v1) * glm::length(v2));
auto cross = glm::cross(v1, v2);
if(glm::length(cross) < 0.01f) return;
auto axis = glm::normalize(cross);
auto s = sin(angle/2.f);
auto qx = axis.x *s;
auto qy = axis.y *s;
auto qz = axis.z *s;
auto qw = cos(angle/2.f);
auto rot = glm::angleAxis(glm::radians(180.f), glm::vec3(0,1,0));
rot *= glm::quat(qw,qx,qy,qz);
if (!cannon) {
return;
}
auto cannon_pos = cannon->position();
auto cannon_rotation = glm::toMat4(cannon->rotation());
auto offset = glm::vec3(cannon_rotation * glm::vec4(0.f, 0.57352f, 1.3088f, 1.f));
ray r(cannon_pos + offset, intersection_point);
auto ray_unnormalized = glm::vec3(1.f);
if (input.isDown(inputMapping::beam_normal)) {
......@@ -183,10 +174,10 @@ namespace gdw {
if (input.isDown(inputMapping::beam_normal)) {
auto ray_length = (std::abs(ray_length_ - 0.f) < 0.001f)? glm::length(ray_unnormalized): ray_length_;
render_ray(&r, ray_length, std::max((beam_velocity/10.f)/2.f, 1.f), glm::vec3(0,0,1));
render_ray(&r, offset, ray_length, 0.18f, false, glm::vec3(0.5f, 0.6f, 0.9f));
} else {
auto ray_length = (std::abs(ray_length_ - 0.f) < 0.001f)? glm::length(ray_unnormalized): ray_length_;
render_ray(&r, ray_length, std::max((beam_velocity/10.f)/2.f, 1.f), glm::vec3(1,0,0));
render_ray(&r, offset, ray_length, 0.18f, true, glm::vec3(0.8f, 0.4f, 0.f));
}
// do {
switch(portal_type) {
......@@ -198,9 +189,9 @@ namespace gdw {
ray_length_ = remaining_length;
process_beam_collision(r2, ray_unnormalized, beam_velocity, ray_length_, asteroid_hit, portal_type, remaining_length);
if (input.isDown(inputMapping::beam_normal)) {
render_ray(&r2, remaining_length, std::max((beam_velocity/10.f)/2.f, 1.f), glm::vec3(0,0,1));
render_ray(&r2, offset, remaining_length, 0.18f, false, glm::vec3(0,0,1));
} else {
render_ray(&r2, remaining_length, std::max((beam_velocity/10.f)/2.f, 1.f), glm::vec3(1,0,0));
render_ray(&r2, offset, remaining_length, 0.18f, true, glm::vec3(1,0,0));
}
}
break;
......@@ -213,9 +204,9 @@ namespace gdw {
ray_length_ = remaining_length;
process_beam_collision(r2, ray_unnormalized, beam_velocity, ray_length_, asteroid_hit, portal_type, remaining_length);
if (input.isDown(inputMapping::beam_normal)) {
render_ray(&r2, remaining_length, std::max((beam_velocity/10.f)/2.f, 1.f), glm::vec3(0,0,1));
render_ray(&r2, offset, remaining_length, 0.18f, false, glm::vec3(0,0,1));
} else {
render_ray(&r2, remaining_length, std::max((beam_velocity/10.f)/2.f, 1.f), glm::vec3(1,0,0));
render_ray(&r2, offset, remaining_length, 0.18f, true, glm::vec3(1,0,0));
}
}
break;
......@@ -239,6 +230,16 @@ namespace gdw {
break;
}
}
auto cannon = level_manager.cannon();
if (cannon) {
const static auto cannon_dir = glm::vec3(0.f, 0.f, 1.f);
auto ray_dir = get_world_mouse_pos() - cannon->position();
auto alpha = glm::atan(ray_dir.x, ray_dir.z);
auto beta = glm::atan(cannon_dir.x, cannon_dir.z);
auto cannon_rotation = glm::angleAxis(alpha + beta, glm::vec3(0.f, 1.f, 0.f));
cannon->rotation(cannon_rotation);
}
}
void game_input_manager::process_beam_collision(ray& r, glm::vec3 &ray_unnormalized, float &beam_velocity, float &ray_length_, bool &asteroid_hit, int &portal_type, float &remaining_ray_length) {
......@@ -262,7 +263,6 @@ namespace gdw {
//BEAM X ASTEROIDS
for(auto &coll : collider) {
auto c = engine_.entity_manager().resolve(coll);
auto type_comp = c->component<object_type_component>();
if(type_comp) {
if(type_comp->get_object_type() == "ASTEROID") {
......@@ -315,27 +315,20 @@ namespace gdw {
auto distance = glm::length(r.source - c->position());
if(glm::length(r.source - c->position()) < ray_length_) {
move->add_force((attract_dir * beam_velocity) / std::max(distance/20.f, 2.f));
ray_length_ = std::min(ray_length_, glm::length(c->position() - r.source));
}
}
}
}
void game_input_manager::render_ray(ray* r, float length, float radius, glm::vec3 color) {
auto v1 = glm::vec3(0,0,-1);
auto v2 = glm::normalize(r->destination-r->source);
auto angle = std::acos(glm::dot(v1, v2) / glm::length(v1) * glm::length(v2));
auto cross = glm::cross(v1, v2);
if(glm::length(cross) < 0.01f) return;
void game_input_manager::render_ray(ray* r, const glm::vec3& offset, float length, float radius, bool invert, glm::vec3 color) {
const static auto cannon_dir = glm::vec3(0.f, 0.f, 1.f);
auto ray_dir = glm::normalize(r->destination - (r->source - offset));
auto alpha = glm::atan(ray_dir.x, ray_dir.z);
auto beta = glm::atan(cannon_dir.x, cannon_dir.z);
auto cannon_rotation = glm::angleAxis(alpha + beta, glm::vec3(0.f, 1.f, 0.f));
auto axis = glm::normalize(cross);
auto s = sin(angle/2.f);
auto qx = axis.x * s;
auto qy = axis.y * s;
auto qz = axis.z * s;
auto qw = cos(angle/2.f);
auto rot = glm::angleAxis(glm::radians(180.f), glm::vec3(0,1,0));
rot *= glm::quat(qw,qx,qy,qz);
engine_.rendering_system().render_trakor_beam(r->source, rot, length, radius, false, color);
engine_.rendering_system().render_trakor_beam(r->source, cannon_rotation, length, radius, invert, color);
}
} //Namespace gdw
......@@ -128,7 +128,7 @@ namespace gdw {
auto pos = ent->position();
ray r(pos-glm::vec3(2.5f,0,0), pos+glm::vec3(2.5f,0,0));
game_input_manager.render_ray(&r, glm::length(r.destination - r.source), 1.f, glm::vec3(0,1,0));
game_input_manager.render_ray(&r, glm::vec3(0.f), glm::length(r.destination - r.source), 1.f, false, glm::vec3(0,1,0));
a->alive_timer += dt;
if(a->alive_timer >= 20.f) {
......
#include <glm/gtx/string_cast.hpp>
#include <gameplay/level_manager.h>
#include <core/engine.hpp>
#include <ecs/entity_manager.hpp>
......@@ -44,7 +46,10 @@ namespace gdw {
player_(0),
camera_(0),
cannon_(0),
deathstar_(0){
deathstar_(0),
plane_offset_y_(0.f),
cannon_socket_offset_(0.f, 0.9163f, 3.9824f, 1.f),
cannon_offset_(0.f, 0.57352f, 1.3088f, 1.f) {
auto ptr = engine_.config_manager().load("config/settings");
map_load_timer_ = ptr->get<float>("map_load_timer", 0.f);
}
......@@ -99,7 +104,7 @@ namespace gdw {
// -------- PLAYER --------
if (player_ == 0) {
auto &player = engine_.entity_manager().emplace_back(glm::vec3(0.f, 3.f, 42.f),
auto &player = engine_.entity_manager().emplace_back(glm::vec3(0.f, plane_offset_y_ - cannon_socket_offset_.y - cannon_offset_.y, 42.f),
glm::angleAxis(glm::radians(90.f),
glm::vec3(0.f, 1.f, 0.f)));
player.emplace_back<object_type_component>("PLAYER");
......@@ -133,18 +138,18 @@ namespace gdw {
// ------- CANNON -----------
if (cannon_ == 0) {
auto &cannon = engine_.entity_manager().emplace_back(glm::vec3(0.f, 0.f, 3.f),
auto &cannon = engine_.entity_manager().emplace_back(glm::vec3(0.f, 0.f, 0.f),
glm::angleAxis(glm::radians(-90.f),
glm::vec3(0.f, 1.f, 0.f)));
cannon.emplace_back<staticmesh_component>("mesh/cannon.msh");
cannon_ = cannon.id();
}
maploader_.load({"mesh/asteroid.msh", "mesh/asteroid2.msh", "mesh/asteroid3.msh"}, 1.f, 50, 1, 40, -25, 3, -2,
maploader_.load({"mesh/asteroid.msh", "mesh/asteroid2.msh", "mesh/asteroid3.msh"}, 1.f, 50, 1, 40, -25, plane_offset_y_, -2,
static_cast<int>(time(0)), 30, 1.0f, 2.0f,
engine_.getConfig().get("navDebug", false)); //old seed: 27389
float tpos[]{20 + 15, 3, 15};
float tpos[]{20 + 15, plane_offset_y_, 15};
maploader_.set_move_target(tpos, false);
is_level_build_ = true;
......@@ -201,10 +206,8 @@ namespace gdw {
auto cannon = engine_.entity_manager().resolve(cannon_);
if (!cannon) return;
auto offset = glm::vec4(0.f, 0.9163f, 3.9824f, 1.f);
auto rotated_offset = glm::toMat4(p->rotation()) * offset;
auto rotated_offset = glm::toMat4(p->rotation()) * cannon_socket_offset_;
cannon->position(p->position() + rotated_offset.xyz());
cannon->rotation(p->rotation());
// ----- ITEMS DROPS ------
unsigned int item_drop_chance = 100; // percent
......@@ -272,7 +275,7 @@ namespace gdw {
void level_manager::create_asteroid(glm::vec3 pos, std::string mesh_name, float scale, glm::vec3 rotation) {
auto &entity_manager = engine_.entity_manager();
pos.y = plane_offset_y_;
auto &asteroid = entity_manager.emplace_back(pos, glm::angleAxis(glm::radians(0.f), glm::vec3(0.f, 0.f, 0.f)));
auto x_quat = glm::angleAxis(rotation.x, glm::vec3(1, 0, 0));
auto y_quat = glm::angleAxis(rotation.y, glm::vec3(0, 1, 0));
......@@ -282,8 +285,7 @@ namespace gdw {
auto col_data = engine_.physics_system().collision_data_manager().load("physic/asteroid.col");
auto &asteroid_coll = asteroid.emplace_back<collision_component>(
new sphere(col_data->center() + glm::vec3(0, (-pos.y) + 3.f, 0),
(scale / 4.f) * glm::length(col_data->half_extend())));
new sphere(glm::vec3(0.f), scale / 2.f * glm::length(col_data->half_extend())));
asteroid.scale(glm::vec3(scale));
asteroid.emplace_back<staticmesh_component>(mesh_name.data(), true);
......@@ -295,14 +297,14 @@ namespace gdw {
void level_manager::create_victim(float z) {
auto &entity_manager = engine_.entity_manager();
auto &victim = entity_manager.emplace_back(glm::vec3(-40.f, 3.f, z),
glm::angleAxis(glm::radians(90.f), glm::vec3(0.f, 1.f, 0.f)));
victim.scale(glm::vec3(0.6f));
auto &victim = entity_manager.emplace_back(glm::vec3(-40.f, plane_offset_y_, z),
glm::angleAxis(glm::radians(90.f), glm::vec3(0.f, 1.f, 0.f)),
glm::vec3(0.6f));
auto col_data = engine_.physics_system().collision_data_manager().load("physic/pirate_ship.col");
auto col_data = engine_.physics_system().collision_data_manager().load("physic/victim.col");
auto &victim_coll = victim.emplace_back<collision_component>(
new sphere(col_data->center(), 0.3f * glm::length(col_data->half_extend())));
new sphere(glm::vec3(0.f), 1.0f));
victim.emplace_back<object_type_component>("VICTIM");
victim_coll.set_trigger([this](unsigned long long self_id, unsigned long long coll_id) {
......@@ -334,7 +336,7 @@ namespace gdw {
void level_manager::create_item(glm::vec3 pos, int type) {
auto &entity_manager = engine_.entity_manager();
auto &item = entity_manager.emplace_back(glm::vec3(pos.x - 1.f, 3.f, pos.z),
auto &item = entity_manager.emplace_back(glm::vec3(pos.x - 1.f, plane_offset_y_, pos.z),
glm::angleAxis(glm::radians(90.f), glm::vec3(0.f, 1.f, 0.f)));
item.emplace_back<staticmesh_component>("mesh/coin.msh", true);
......@@ -365,7 +367,7 @@ namespace gdw {
}
}
auto &portal = engine_.entity_manager().emplace_back(glm::vec3(world_pos.x, 3.f, world_pos.z),
auto &portal = engine_.entity_manager().emplace_back(glm::vec3(world_pos.x, plane_offset_y_, world_pos.z),
glm::angleAxis(glm::radians(-45.f),
glm::vec3(1.f, 0.f, 0.f)));
portal.emplace_back<staticmesh_component>("mesh/coin.msh");
......@@ -386,7 +388,7 @@ namespace gdw {
}
}
auto &portal = engine_.entity_manager().emplace_back(glm::vec3(world_pos.x, 3.f, world_pos.z),
auto &portal = engine_.entity_manager().emplace_back(glm::vec3(world_pos.x, plane_offset_y_, world_pos.z),
glm::angleAxis(glm::radians(-45.f),
glm::vec3(1.f, 0.f, 0.f)));
portal.emplace_back<staticmesh_component>("mesh/coin.msh");
......
#include <glm/gtx/string_cast.hpp>
#include <physics/physics_system.h>
#include <core/engine.hpp>
#include <util/component_helper.hpp>
......
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