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: ...@@ -16,7 +16,7 @@ public:
void update(float dt); void update(float dt);
glm::vec3 get_world_mouse_pos(); 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); 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: private:
void handle_key_input(float dt); void handle_key_input(float dt);
......
...@@ -72,6 +72,9 @@ private: ...@@ -72,6 +72,9 @@ private:
maploader maploader_; maploader maploader_;
float map_load_timer_; float map_load_timer_;
float plane_offset_y_;
glm::vec4 cannon_socket_offset_;
glm::vec4 cannon_offset_;
bool is_level_build_; bool is_level_build_;
}; };
......
...@@ -17,77 +17,77 @@ ...@@ -17,77 +17,77 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
namespace gdw { namespace gdw {
class engine; class engine;
class entity; class entity;
class maploader{ class maploader{
enum SamplePolyAreas enum SamplePolyAreas
{ {
SAMPLE_POLYAREA_GROUND, SAMPLE_POLYAREA_GROUND,
SAMPLE_POLYAREA_WATER, SAMPLE_POLYAREA_WATER,
SAMPLE_POLYAREA_ROAD, SAMPLE_POLYAREA_ROAD,
SAMPLE_POLYAREA_DOOR, SAMPLE_POLYAREA_DOOR,
SAMPLE_POLYAREA_GRASS, SAMPLE_POLYAREA_GRASS,
SAMPLE_POLYAREA_JUMP, SAMPLE_POLYAREA_JUMP,
}; };
enum SamplePolyFlags enum SamplePolyFlags
{ {
SAMPLE_POLYFLAGS_WALK = 0x01, // Ability to walk (ground, grass, road) SAMPLE_POLYFLAGS_WALK = 0x01, // Ability to walk (ground, grass, road)
SAMPLE_POLYFLAGS_SWIM = 0x02, // Ability to swim (water). SAMPLE_POLYFLAGS_SWIM = 0x02, // Ability to swim (water).
SAMPLE_POLYFLAGS_DOOR = 0x04, // Ability to move through doors. SAMPLE_POLYFLAGS_DOOR = 0x04, // Ability to move through doors.
SAMPLE_POLYFLAGS_JUMP = 0x08, // Ability to jump. SAMPLE_POLYFLAGS_JUMP = 0x08, // Ability to jump.
SAMPLE_POLYFLAGS_DISABLED = 0x10, // Disabled polygon SAMPLE_POLYFLAGS_DISABLED = 0x10, // Disabled polygon
SAMPLE_POLYFLAGS_ALL = 0xffff // All abilities. SAMPLE_POLYFLAGS_ALL = 0xffff // All abilities.
}; };
private: private:
engine &engine_; engine &engine_;
unsigned char* triareas_; unsigned char* triareas_;
rcHeightfield* solid_; rcHeightfield* solid_;
rcCompactHeightfield* chf_; rcCompactHeightfield* chf_;
rcContourSet* cset_; rcContourSet* cset_;
rcPolyMesh* pmesh_; rcPolyMesh* pmesh_;
rcConfig cfg_; rcConfig cfg_;
rcPolyMeshDetail* dmesh_; rcPolyMeshDetail* dmesh_;
dtNavMesh* navMesh_; dtNavMesh* navMesh_;
dtNavMeshQuery* navQuery_; dtNavMeshQuery* navQuery_;
dtCrowd* crowd_; dtCrowd* crowd_;
BuildContext ctx_; BuildContext ctx_;
float m_targetPos[3]; float m_targetPos[3];
dtPolyRef m_targetRef; dtPolyRef m_targetRef;
std::unordered_map<int,unsigned long long> entityAgent; std::unordered_map<int,unsigned long long> entityAgent;
static constexpr float agent_size_ = 2.25f; static constexpr float agent_size_ = 2.25f;
public: public:
maploader(engine &engine); maploader(engine &engine);
virtual ~maploader(); virtual ~maploader();
std::unique_ptr<map> load(const std::vector<std::string>& meshnames, std::unique_ptr<map> load(const std::vector<std::string>& meshnames,
const float& modelscale, const float& modelscale,
const int& mapWidth,const int& mapHeight,const int& mapDeep, const int& mapWidth,const int& mapHeight,const int& mapDeep,
const int& startX,const int& startY,const int& startZ, const int& startX,const int& startY,const int& startZ,
const int& seed = 27389,const int& scatter= 17, const int& seed = 27389,const int& scatter= 17,
const float& minAstroidRadius = 1,const float& maxAstroidRadius = 1, bool debug = false); const float& minAstroidRadius = 1,const float& maxAstroidRadius = 1, bool debug = false);
int add_agent(const float *p, unsigned long long int entityID); int add_agent(const float *p, unsigned long long int entityID);
void set_move_target(const float *p, bool adjust); void set_move_target(const float *p, bool adjust);
void update(const float dt); void update(const float dt);
void render(); void render();
void removeAgent(int idx); void removeAgent(int idx);
void set_agent_pos(int idx, const glm::vec3 &pos); void set_agent_pos(int idx, const glm::vec3 &pos);
void set_agent_pos(unsigned long long int entityID, const glm::vec3 &pos); void set_agent_pos(unsigned long long int entityID, const glm::vec3 &pos);
private: private:
void cleanup(); void cleanup();
void generate_cylinder(float size, void generate_cylinder(float size,
float x, float y,float z,std::vector<int>& in, float x, float y,float z,std::vector<int>& in,
std::vector<float>& v, std::vector<float>& v,
int segments = 8); int segments = 8);
}; };
} }
#endif //GDW_SS15_CPP_MAPLOADER_HPP #endif //GDW_SS15_CPP_MAPLOADER_HPP
...@@ -14,38 +14,38 @@ class entity; ...@@ -14,38 +14,38 @@ class entity;
class movement_component : public component { class movement_component : public component {
public: public:
movement_component(engine& engine, entity& owner); movement_component(engine& engine, entity& owner);
~movement_component(); ~movement_component();
void update(float dt); void update(float dt);
void set_mass(float mass){mass_ = mass;} void set_mass(float mass){mass_ = mass;}
void set_damping(float damping){damping_ = damping;} void set_damping(float damping){damping_ = damping;}
float mass() {return mass_;} float mass() {return mass_;}
float damping() {return damping_;} float damping() {return damping_;}
void set_current_velocity(glm::vec3 velocity) {current_velocity_ = velocity;} void set_current_velocity(glm::vec3 velocity) {current_velocity_ = velocity;}
glm::vec3 current_velocity() {return current_velocity_;} glm::vec3 current_velocity() {return current_velocity_;}
void add_force(glm::vec3 force) {forces_.push_back(force);} void add_force(glm::vec3 force) {forces_.push_back(force);}
void set_constant_force(glm::vec3 constant_force) {constant_force_ = constant_force;} void set_constant_force(glm::vec3 constant_force) {constant_force_ = constant_force;}
glm::vec3 constant_force() {return constant_force_;} glm::vec3 constant_force() {return constant_force_;}
void add_angular_force(glm::vec3 force){angular_forces_.push_back(force);} void add_angular_force(glm::vec3 force){angular_forces_.push_back(force);}
static unsigned long long type_id() { static unsigned long long type_id() {
return gdw::type_id<gdw::movement_component>(); return gdw::type_id<gdw::movement_component>();
} }
private: private:
float mass_; float mass_;
float damping_; float damping_;
glm::vec3 current_velocity_; glm::vec3 current_velocity_;
glm::vec3 constant_force_; glm::vec3 constant_force_;
std::vector<glm::vec3> forces_; std::vector<glm::vec3> forces_;
std::vector<glm::vec3> angular_forces_; std::vector<glm::vec3> angular_forces_;
glm::vec3 angular_velocity_; glm::vec3 angular_velocity_;
......
...@@ -16,35 +16,39 @@ class ray; ...@@ -16,35 +16,39 @@ class ray;
class physics_system { class physics_system {
public: public:
physics_system(engine& engine); physics_system(engine& engine);
~physics_system(); ~physics_system();
void update(float dt); void update(float dt);
void narrow_phase(); void narrow_phase();
template <shape_type A, shape_type B> template <shape_type A, shape_type B>
bool checkCollisions(const collision_shape* s1, const collision_shape* s2); bool checkCollisions(const collision_shape* s1, const collision_shape* s2);
template <shape_type A> template <shape_type A>
std::vector<unsigned long long> checkCollisions(const collision_shape* s1); std::vector<unsigned long long> checkCollisions(const collision_shape* s1);
gdw::collision_data_manager& collision_data_manager() noexcept { gdw::collision_data_manager& collision_data_manager() noexcept {
return collision_data_manager_; return collision_data_manager_;
} }
auto& collision_components() {
return collision_components_;
}
private: private:
void register_movement_component(movement_component* comp); void register_movement_component(movement_component* comp);
void unregister_movement_component(movement_component* comp); void unregister_movement_component(movement_component* comp);
void register_collision_component(collision_component* comp); void register_collision_component(collision_component* comp);
void unregister_collision_component(collision_component* comp); void unregister_collision_component(collision_component* comp);
private: private:
friend class movement_component; friend class movement_component;
friend class collision_component; friend class collision_component;
engine &engine_; engine &engine_;
std::vector<movement_component*> movement_components_; std::vector<movement_component*> movement_components_;
std::vector<collision_component*> collision_components_; std::vector<collision_component*> collision_components_;
gdw::collision_data_manager collision_data_manager_; gdw::collision_data_manager collision_data_manager_;
}; };
} //Namespace gdw } //Namespace gdw
......
#include <glm/gtx/string_cast.hpp>
#include <gameplay/game_input_manager.h> #include <gameplay/game_input_manager.h>
#include <core/engine.hpp> #include <core/engine.hpp>
#include <SDL.h> #include <SDL.h>
...@@ -60,7 +62,7 @@ namespace gdw { ...@@ -60,7 +62,7 @@ namespace gdw {
auto plane_normal = glm::vec3(0,1,0); //Hardcoded ground plane auto plane_normal = glm::vec3(0,1,0); //Hardcoded ground plane
//HNF //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); return camera->position()-glm::vec3((glm::normalize(ray_dir))*d);
} }
...@@ -145,24 +147,13 @@ namespace gdw { ...@@ -145,24 +147,13 @@ namespace gdw {
auto intersection_point = get_world_mouse_pos(); auto intersection_point = get_world_mouse_pos();
auto cannon = level_manager.cannon(); auto cannon = level_manager.cannon();
auto cannon_pos = glm::vec3(cannon->position().x, 3.f, cannon->position().z); if (!cannon) {
ray r(cannon_pos, intersection_point); return;
}
//SPAWN LASER auto cannon_pos = cannon->position();
auto v1 = glm::vec3(0,0,-1); auto cannon_rotation = glm::toMat4(cannon->rotation());
auto v2 = glm::normalize(r.destination-r.source); auto offset = glm::vec3(cannon_rotation * glm::vec4(0.f, 0.57352f, 1.3088f, 1.f));
auto angle = std::acos(glm::dot(v1, v2) / glm::length(v1) * glm::length(v2)); ray r(cannon_pos + offset, intersection_point);
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);
auto ray_unnormalized = glm::vec3(1.f); auto ray_unnormalized = glm::vec3(1.f);
if (input.isDown(inputMapping::beam_normal)) { if (input.isDown(inputMapping::beam_normal)) {
...@@ -183,10 +174,10 @@ namespace gdw { ...@@ -183,10 +174,10 @@ namespace gdw {
if (input.isDown(inputMapping::beam_normal)) { if (input.isDown(inputMapping::beam_normal)) {
auto ray_length = (std::abs(ray_length_ - 0.f) < 0.001f)? glm::length(ray_unnormalized): ray_length_; 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 { } else {
auto ray_length = (std::abs(ray_length_ - 0.f) < 0.001f)? glm::length(ray_unnormalized): ray_length_; 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 { // do {
switch(portal_type) { switch(portal_type) {
...@@ -198,9 +189,9 @@ namespace gdw { ...@@ -198,9 +189,9 @@ namespace gdw {
ray_length_ = remaining_length; ray_length_ = remaining_length;
process_beam_collision(r2, ray_unnormalized, beam_velocity, ray_length_, asteroid_hit, portal_type, remaining_length); process_beam_collision(r2, ray_unnormalized, beam_velocity, ray_length_, asteroid_hit, portal_type, remaining_length);
if (input.isDown(inputMapping::beam_normal)) { 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 { } 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; break;
...@@ -213,9 +204,9 @@ namespace gdw { ...@@ -213,9 +204,9 @@ namespace gdw {
ray_length_ = remaining_length; ray_length_ = remaining_length;
process_beam_collision(r2, ray_unnormalized, beam_velocity, ray_length_, asteroid_hit, portal_type, remaining_length); process_beam_collision(r2, ray_unnormalized, beam_velocity, ray_length_, asteroid_hit, portal_type, remaining_length);
if (input.isDown(inputMapping::beam_normal)) { 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 { } 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; break;
...@@ -239,6 +230,16 @@ namespace gdw { ...@@ -239,6 +230,16 @@ namespace gdw {
break; 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) { 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 { ...@@ -262,7 +263,6 @@ namespace gdw {
//BEAM X ASTEROIDS //BEAM X ASTEROIDS
for(auto &coll : collider) { for(auto &coll : collider) {
auto c = engine_.entity_manager().resolve(coll); auto c = engine_.entity_manager().resolve(coll);
auto type_comp = c->component<object_type_component>(); auto type_comp = c->component<object_type_component>();
if(type_comp) { if(type_comp) {
if(type_comp->get_object_type() == "ASTEROID") { if(type_comp->get_object_type() == "ASTEROID") {
...@@ -315,27 +315,20 @@ namespace gdw { ...@@ -315,27 +315,20 @@ namespace gdw {
auto distance = glm::length(r.source - c->position()); auto distance = glm::length(r.source - c->position());
if(glm::length(r.source - c->position()) < ray_length_) { if(glm::length(r.source - c->position()) < ray_length_) {
move->add_force((attract_dir * beam_velocity) / std::max(distance/20.f, 2.f)); 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) { void game_input_manager::render_ray(ray* r, const glm::vec3& offset, float length, float radius, bool invert, glm::vec3 color) {
auto v1 = glm::vec3(0,0,-1); const static auto cannon_dir = glm::vec3(0.f, 0.f, 1.f);
auto v2 = glm::normalize(r->destination-r->source); auto ray_dir = glm::normalize(r->destination - (r->source - offset));
auto angle = std::acos(glm::dot(v1, v2) / glm::length(v1) * glm::length(v2)); auto alpha = glm::atan(ray_dir.x, ray_dir.z);
auto cross = glm::cross(v1, v2); auto beta = glm::atan(cannon_dir.x, cannon_dir.z);
if(glm::length(cross) < 0.01f) return; auto cannon_rotation = glm::angleAxis(alpha + beta, glm::vec3(0.f, 1.f, 0.f));
auto axis = glm::normalize(cross); engine_.rendering_system().render_trakor_beam(r->source, cannon_rotation, length, radius, invert, color);
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);
} }
} //Namespace gdw } //Namespace gdw
...@@ -34,7 +34,7 @@ namespace gdw { ...@@ -34,7 +34,7 @@ namespace gdw {
auto &placed_item_coll = placed_item.emplace_back<collision_component>(new sphere(glm::vec3(0.f),7.f)); auto &placed_item_coll = placed_item.emplace_back<collision_component>(new sphere(glm::vec3(0.f),7.f));
placed_item.emplace_back<object_type_component>("MAGNET_FIELD"); placed_item.emplace_back<object_type_component>("MAGNET_FIELD");
placed_item_coll.set_trigger([this](unsigned long long self_id, unsigned long long coll_id) { placed_item_coll.set_trigger([this](unsigned long long self_id, unsigned long long coll_id) {
auto collider = engine_.entity_manager().resolve(coll_id); auto collider = engine_.entity_manager().resolve(coll_id);
auto type_comp = collider->component<object_type_component>(); auto type_comp = collider->component<object_type_component>();
if(type_comp) { if(type_comp) {
...@@ -128,7 +128,7 @@ namespace gdw { ...@@ -128,7 +128,7 @@ namespace gdw {
auto pos = ent->position(); auto pos = ent->position();
ray r(pos-glm::vec3(2.5f,0,0), pos+glm::vec3(2.5f,0,0)); 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; a->alive_timer += dt;
if(a->alive_timer >= 20.f) { if(a->alive_timer >= 20.f) {
......
#include <glm/gtx/string_cast.hpp>
#include <gameplay/level_manager.h> #include <gameplay/level_manager.h>
#include <core/engine.hpp> #include <core/engine.hpp>
#include <ecs/entity_manager.hpp> #include <ecs/entity_manager.hpp>
...@@ -44,7 +46,10 @@ namespace gdw { ...@@ -44,7 +46,10 @@ namespace gdw {
player_(0), player_(0),
camera_(0), camera_(0),
cannon_(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"); auto ptr = engine_.config_manager().load("config/settings");
map_load_timer_ = ptr->get<float>("map_load_timer", 0.f); map_load_timer_ = ptr->get<float>("map_load_timer", 0.f);
} }
...@@ -99,7 +104,7 @@ namespace gdw { ...@@ -99,7 +104,7 @@ namespace gdw {
// -------- PLAYER -------- // -------- PLAYER --------
if (player_ == 0) { 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::angleAxis(glm::radians(90.f),
glm::vec3(0.f, 1.f, 0.f))); glm::vec3(0.f, 1.f, 0.f)));
player.emplace_back<object_type_component>("PLAYER"); player.emplace_back<object_type_component>("PLAYER");
...@@ -133,18 +138,18 @@ namespace gdw { ...@@ -133,18 +138,18 @@ namespace gdw {
// ------- CANNON ----------- // ------- CANNON -----------
if (cannon_ == 0) { 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::angleAxis(glm::radians(-90.f),