...
 
Commits (2)
......@@ -13,5 +13,6 @@
#include "components/ray.hpp"
#include "components/animation.hpp"
#include "components/ai_player.hpp"
#include "components/player_component.hpp"
#endif // !COMPONENTS_HPP_INCLUDED
#ifndef COMPONENTS_PLAYER_COMPONENT_HPP_INCLUDED
#define COMPONENTS_PLAYER_COMPONENT_HPP_INCLUDED
#include "ECS/Component.hpp"
struct Player{};
using PlayerComponent = ecs::Component<Player>;
#endif // !COMPONENTS_PLAYER_COMPONENT_HPP_INCLUDED
......@@ -31,6 +31,8 @@ void scene_model::setup_data(std::map<std::string,GLuint>& shaders, scene_struct
auto& pointDrawer = PointDrawer::get();
pointDrawer.scene = &scene;
pointDrawer.shader = shaders["mesh"];
m_timer.periodic_event_time_step = .3f;
}
/** This function is called at each frame of the animation loop.
......@@ -50,6 +52,7 @@ void scene_model::createResources()
setupSkybox();
setupPlayerEntity();
setupAimCursor();
buildAIHierarchyPool(m_resources);
m_resources.collisionGrid = CollisionGrid(gridResolution, gridResolution, &m_resources.terrainGeometry);
m_resources.meshes["collision_box"] = mesh_primitive_cylinder(1.f, vec3{}, vec3{ 0.f, 0.f, 1.f });
setupCollisionRates();
......@@ -60,7 +63,6 @@ void scene_model::buildEntities(std::map<std::string,GLuint>& shaders)
EntityBuilder builder { m_ecs, m_resources };
builder.buildTerrain(shaders);
builder.buildAIHierarchyPool();
constexpr auto num_trees { 30 };
auto tree_positions = getForest(m_resources.terrainGeometry, num_trees, trunk_radius * 7.f);
......@@ -256,7 +258,7 @@ void scene_model::setupPlayerEntity()
{ GLFW_MOUSE_BUTTON_LEFT, GLFW_RELEASE}
};
auto cylinderRadius = 0.5f;
auto cylinderRadius = 0.3f;
const vec3 vertebraShape { 0.3, 0.15, 0.05 };
auto& playerMesh = m_resources.meshes["player"] = createVertebra(vertebraShape, { 0, 0, vertebraShape.z }, 30, 30);
playerMesh.uniform.color = { 0.3f, 0.25f, 0.09f };
......@@ -280,7 +282,8 @@ void scene_model::setupPlayerEntity()
void scene_model::setupAimCursor()
{
mesh billboard_cpu{};
billboard_cpu.position = { {-0.1f,-0.1f,0}, { 0.1f,-0.1f,0}, { 0.1f, 0.1f,0}, {-0.1f, 0.1f,0} };
float cursorSize = 0.05f;
billboard_cpu.position = { {-cursorSize,-cursorSize,0}, {cursorSize,-cursorSize,0}, { cursorSize,cursorSize,0}, {-cursorSize,cursorSize,0} };
billboard_cpu.texture_uv = { {0,1}, {1,1}, {1,0}, {0,0} };
billboard_cpu.connectivity = { {0,1,2}, {0,2,3} };
......@@ -298,8 +301,9 @@ void scene_model::setupPlayerAnimation()
void scene_model::setupCollisionRates()
{
m_resources.collisionRates = {
{ std::make_pair(CollisionMaterial::Tree, CollisionMaterial::Character), CollisionRates { 0.1f, 0.1f } }
, { std::make_pair(CollisionMaterial::Ground, CollisionMaterial::Character), CollisionRates { 0.f, 0.1f } }
{ std::make_pair(CollisionMaterial::Tree, CollisionMaterial::Character), CollisionRates { 0.1f, 0.9f } }
, { std::make_pair(CollisionMaterial::Ground, CollisionMaterial::Character), CollisionRates { 0.f, 0.2f } }
, { std::make_pair(CollisionMaterial::Character, CollisionMaterial::Character), CollisionRates { 0.8f, 1.0f } }
};
completeWithDefaults(m_resources.collisionRates);
completeWithSymetricals(m_resources.collisionRates);
......@@ -308,7 +312,7 @@ void scene_model::setupCollisionRates()
void scene_model::setupCamera(scene_structure& scene)
{
scene.camera.camera_type = camera_control_spherical_coordinates;
scene.camera.scale = 10.0f;
scene.camera.scale = 2.0f;
scene.camera.apply_rotation(0,0,0,1.2f);
}
......
......@@ -6,7 +6,7 @@
#include "components.hpp"
#include "systems.hpp"
struct MyEcs : ecs::ecs<ecs::Components<HierarchyCollisionBoxComponent, TransformComponent, DerivedTransformComponent, RenderComponent, ArticulatedHierarchyComponent, CollisionComponent, ForceComponent, PhysicsComponent, PlayerControlComponent, RayComponent, AnimationComponent, AIPlayerComponent>
struct MyEcs : ecs::ecs<ecs::Components<HierarchyCollisionBoxComponent, TransformComponent, DerivedTransformComponent, RenderComponent, ArticulatedHierarchyComponent, CollisionComponent, ForceComponent, PhysicsComponent, PlayerControlComponent, RayComponent, AnimationComponent, AIPlayerComponent, PlayerComponent>
, ecs::Systems<RayRender, RayTrajectory,
GravitySystem, PhysicsSystem, DeadEntityCollector,
PositionUpdater::Collision2Transform, PositionUpdater::Transform2Collision, HierarchyPositionUpdater::Collision2Transform, HierarchyPositionUpdater::Transform2Collision,
......
......@@ -87,7 +87,9 @@ ecs::Entity EntityBuilder::buildCharacter(CharacterDescription& characterInfo, C
ecs::Entity EntityBuilder::buildPlayer(vcl::vec3 const& position, std::map<std::string,GLuint>& shaders)
{
auto& hierarchy = m_resources.AIHierarchyPool.getFreeElement().first;
return buildCharacter(m_resources.standardCharacter, hierarchy, position, shaders);
auto playerEntity = buildCharacter(m_resources.standardCharacter, hierarchy, position, shaders);
m_ecs.add<PlayerComponent>(playerEntity);
return playerEntity;
}
ecs::Entity EntityBuilder::buildAICharacter(ecs::Entity const target, vcl::vec3 const& position, std::map<std::string,GLuint>& shaders)
......@@ -98,10 +100,3 @@ ecs::Entity EntityBuilder::buildAICharacter(ecs::Entity const target, vcl::vec3
m_ecs.add<AIPlayerComponent>(ai, target);
return ai;
}
static void createPlayerMeshHierarchyWrapper(CompletedMeshHierarchy& hierarchy) { hierarchy = createPlayerMeshHierarchy(); }
void EntityBuilder::buildAIHierarchyPool()
{
m_resources.AIHierarchyPool.map(createPlayerMeshHierarchyWrapper);
//m_resources.AIHierarchyPool.map([](CompletedMeshHierarchy const& h) { printHierarchy(h.hierarchy); });
}
......@@ -18,7 +18,6 @@ public:
ecs::Entity buildCharacter(CharacterDescription& characterInfo, CompletedMeshHierarchy& meshHierarchy, vcl::vec3 const& position, std::map<std::string,GLuint>& shaders);
ecs::Entity buildPlayer(vcl::vec3 const& position, std::map<std::string,GLuint>& shaders);
ecs::Entity buildAICharacter(ecs::Entity const target, vcl::vec3 const& position, std::map<std::string,GLuint>& shaders);
void buildAIHierarchyPool();
private:
MyEcs& m_ecs;
......
......@@ -35,57 +35,69 @@ std::vector<vec3> getForest(TerrainGeometry const& terrainGeometry, int num_tree
return forest;
}
CompletedMeshHierarchy createPlayerMeshHierarchy()
static void createPlayerMeshInPlace(CompletedMeshHierarchy& completedHierarchy)
{
vcl::hierarchy_mesh_drawable playerMeshHierarchy{};
vcl::hierarchy_mesh_drawable& playerMeshHierarchy = completedHierarchy.hierarchy;
const vec3 vertebraShape { 0.3f, 0.15f, 0.05f };
const vec3 vertebraShape{ 0.3f, 0.15f, 0.05f };
auto vertebra = createVertebra(vertebraShape, vec3{ 0, 0, vertebraShape.z }, 30, 30);
auto vertebraName = [](int i)
{
std::ostringstream oss { "vertebra" };
oss << i;
return oss.str();
};
const vec3 vertebraTranslation { 0, 0, 2.f * vertebraShape.z };
{
std::ostringstream oss{ "vertebra" };
oss << i;
return oss.str();
};
const vec3 vertebraTranslation{ 0, 0, 2.f * vertebraShape.z };
const auto rootName = vertebraName(0);
const auto numVertebrae = 4;
playerMeshHierarchy.add(vertebra, rootName);
for(auto i = 1; i < numVertebrae; ++i)
for (auto i = 1; i < numVertebrae; ++i)
{
playerMeshHierarchy.add(vertebra, vertebraName(i), vertebraName(i-1), vertebraTranslation);
playerMeshHierarchy.add(vertebra, vertebraName(i), vertebraName(i - 1), vertebraTranslation);
}
const auto vertebraSemiWidth = vertebraShape.x;
const auto legRadius = vertebraSemiWidth / 3.f;
const auto legLength = vertebraShape.z * 6.f;
auto legElem = mesh_primitive_cylinder(legRadius, {0, 0, 0}, {0, 0, -legLength});
playerMeshHierarchy.add(legElem, "left_thigh", rootName, {-vertebraSemiWidth + 1.3f * legRadius, 0, -vertebraShape.z});
playerMeshHierarchy.add(legElem, "right_thigh", rootName, {vertebraSemiWidth - 1.3f * legRadius, 0, -vertebraShape.z});
auto legElem = mesh_primitive_cylinder(legRadius, { 0, 0, 0 }, { 0, 0, -legLength });
playerMeshHierarchy.add(legElem, "left_thigh", rootName, { -vertebraSemiWidth + 1.3f * legRadius, 0, -vertebraShape.z });
playerMeshHierarchy.add(legElem, "right_thigh", rootName, { vertebraSemiWidth - 1.3f * legRadius, 0, -vertebraShape.z });
const auto kneeLength = 0.3f * legLength;
playerMeshHierarchy.add(legElem, "left_leg", "left_thigh", { 0, 0, -legLength - kneeLength});
playerMeshHierarchy.add(legElem, "right_leg", "right_thigh", { 0, 0, -legLength - kneeLength});
playerMeshHierarchy.add(legElem, "left_leg", "left_thigh", { 0, 0, -legLength - kneeLength });
playerMeshHierarchy.add(legElem, "right_leg", "right_thigh", { 0, 0, -legLength - kneeLength });
auto footElem = mesh_primitive_sphere(legRadius, {0, 0, 0});
playerMeshHierarchy.add(footElem, "left_foot", "left_leg", {0, 0, - legLength });
playerMeshHierarchy.add(footElem, "right_foot", "right_leg", {0, 0, - legLength });
auto footElem = mesh_primitive_sphere(legRadius, { 0, 0, 0 });
playerMeshHierarchy.add(footElem, "left_foot", "left_leg", { 0, 0, -legLength });
playerMeshHierarchy.add(footElem, "right_foot", "right_leg", { 0, 0, -legLength });
const auto headRadius = 0.6f * vertebraSemiWidth;
auto head = mesh_primitive_sphere(headRadius, { 0, 0, headRadius });
playerMeshHierarchy.add(head, "head", vertebraName(numVertebrae-1), { 0, 0, 2.5f * vertebraShape.z });
playerMeshHierarchy.add(head, "head", vertebraName(numVertebrae - 1), { 0, 0, 2.5f * vertebraShape.z });
const auto armRadius = 0.5f * legRadius;
const auto armLength = 2.f * vertebraSemiWidth;
const auto lateralArmShift = vertebraSemiWidth - 2*armRadius;
const auto lateralArmShift = vertebraSemiWidth - 2 * armRadius;
auto rightArm = mesh_primitive_cylinder(armRadius, { 0, 0, 0 }, { -lateralArmShift, std::sqrt(std::pow(armLength, 2.f) - std::pow(vertebraSemiWidth, 2.f)), 0 });
playerMeshHierarchy.add(rightArm, "right_arm", vertebraName(numVertebrae-1), { vertebraSemiWidth - armRadius, 0, 0 });
playerMeshHierarchy.add(rightArm, "right_arm", vertebraName(numVertebrae - 1), { vertebraSemiWidth - armRadius, 0, 0 });
auto leftArm = mesh_primitive_cylinder(armRadius, { 0, 0, 0 }, { lateralArmShift, std::sqrt(std::pow(armLength, 2.f) - std::pow(vertebraSemiWidth, 2.f)), 0 });
playerMeshHierarchy.add(leftArm, "left_arm", vertebraName(numVertebrae-1), { -vertebraSemiWidth + armRadius, 0, 0 });
playerMeshHierarchy.add(leftArm, "left_arm", vertebraName(numVertebrae - 1), { -vertebraSemiWidth + armRadius, 0, 0 });
HierarchyCollisionBoxInfos::CollectionType boundaries;
HierarchyCollisionBoxInfos::CollectionType& boundaries = completedHierarchy.boundaries;
boundaries.push_back(HierarchyCollisionBoxBoundary{ "left_foot", -legRadius, legRadius });
boundaries.push_back(HierarchyCollisionBoxBoundary{ "right_foot", -legRadius, legRadius });
boundaries.push_back(HierarchyCollisionBoxBoundary{ "head", 0, 2 * headRadius });
return { playerMeshHierarchy, rootName, boundaries };
completedHierarchy.rootName = rootName;
}
CompletedMeshHierarchy createPlayerMeshHierarchy()
{
CompletedMeshHierarchy completedHierarchy{};
createPlayerMeshInPlace(completedHierarchy);
return completedHierarchy;
}
void buildAIHierarchyPool(CommonSceneResources& ressources)
{
ressources.AIHierarchyPool.map(createPlayerMeshInPlace);
}
......@@ -8,6 +8,7 @@
std::vector<vcl::vec3> getForest(TerrainGeometry const& terrain, int num_trees, float min_distance);
CompletedMeshHierarchy createPlayerMeshHierarchy();
void buildAIHierarchyPool(CommonSceneResources& ressources);
constexpr auto trunk_height = 1.f;
constexpr auto trunk_radius = 0.2f;
......
......@@ -40,9 +40,9 @@ void AIHandlingSystem::update(const MyEcs& m_ecs, const bool AIEvent, PlayerCont
controls.currentControls[PlayerControlComponent::Control::Jump] = true;
}
/*const vec3 currentAxis = transform.rotation * vec3(1, 0, 0);
const vec3 currentAxis = transform.rotation * vec3(0, 1, 0);
const vec3 verticalAxis = vec3(0, 0, 1);
const vec3 targetDirection = normalize(m_ecs.get<TransformComponent>(player.target).position - transform.position);
transform.rotation = rotation_between_vector_mat3(currentAxis, targetDirection - dot(verticalAxis, targetDirection) * verticalAxis);*/
vec3 targetDirection = normalize(m_ecs.get<TransformComponent>(player.target).position - transform.position);
targetDirection = normalize(targetDirection - dot(verticalAxis, targetDirection) * verticalAxis);
transform.rotation = transform.rotation * rotation_between_vector_mat3(currentAxis, targetDirection);
}
\ No newline at end of file
......@@ -17,8 +17,8 @@ public :
using UpdatePolicy = ecs::OncePerEntity;
private :
const float changeDirectionProba = .1f;
const float changeDirectionProba = .2f;
const float shootProba = 0.1f;
const float jumpProba = 0.05f;
const float jumpProba = 0.1f;
};
#endif // !SYSTEMS_AI_HANDLING_HPP_INCLUDED
......@@ -6,9 +6,6 @@ void AnimationApplier::update(ArticulatedHierarchyComponent& hierarchyComponent,
{
for(auto const& angle : animationComponent.angles)
{
std::cout << "Bla" << std::endl;
std::cout << angle.first << std::endl;
std::cout << "Blou" << std::endl;
hierarchyComponent.hierarchy.get()[angle.first].transform.rotation = vcl::rotation_from_axis_angle_mat3({1,0,0}, angle.second);
}
}
......@@ -19,20 +19,20 @@ void CameraHandlingSystem::update(vcl::camera_scene& camera, MyEcs& m_ecs, ecs::
const auto sagittalAxis = normalize(transform.rotation * vec3{ 0, 1, 0 });
const auto lateralAxis = normalize(transform.rotation * vec3{ 1, 0, 0 });
camera.translation = -transform.position - lateralAxis * 0.5f - verticalAxis * 0.4f;
camera.translation = -transform.position - lateralAxis * 0.5f - verticalAxis * 0.6f;
if (isActive(Control::Shoot))
createRay(m_ecs, playerEntity, aimCursorEntity, /*transform.position + verticalAxis * 0.4f*/ camera, /*camera.orientation * vec3(0,0.15f,-1)*/ transform);
createRay(m_ecs, playerEntity, aimCursorEntity, camera, transform);
vcl::ray const shootInfos = vcl::picking_ray(camera, vec2(0, 0.1f));
vcl::ray const shootInfos = vcl::picking_ray(camera, m_cursorPosition);
auto& cursorPosition = m_ecs.get<TransformComponent>(aimCursorEntity).position;
cursorPosition = shootInfos.p + shootInfos.u * camera.scale;
}
void createRay(MyEcs& m_ecs, ecs::Entity const& playerEntity, ecs::Entity const& aimCursorEntity, vcl::camera_scene const& camera, TransformComponent const& playerTransform)
void CameraHandlingSystem::createRay(MyEcs& m_ecs, ecs::Entity const& playerEntity, ecs::Entity const& aimCursorEntity, vcl::camera_scene const& camera, TransformComponent const& playerTransform)
{
auto toolRayEntity = m_ecs.createEntity();
vcl::ray const shootInfos = vcl::picking_ray(camera, vec2(0, 0.1f));
vcl::ray const shootInfos = vcl::picking_ray(camera, m_cursorPosition);
auto& toolRay = m_ecs.add<RayComponent>(toolRayEntity, shootInfos.p + shootInfos.u*camera.scale ,shootInfos.u, 0.0f, playerEntity);
m_ecs.getSystem<RayTrajectory>().computeRayTrajectory(toolRay, m_ecs);
m_ecs.getSystem<RayTrajectory>().addDeadRay(toolRay);
......@@ -41,7 +41,7 @@ void createRay(MyEcs& m_ecs, ecs::Entity const& playerEntity, ecs::Entity const&
cursorPosition = shootInfos.p + shootInfos.u * camera.scale;
auto realRayEntity = m_ecs.createEntity();
float const speed = 5.0f;
float const speed = 50.0f;
vec3 const position = playerTransform.position + vec3(0, 0, 1) * 0.4;
vec3 const direction = (*toolRay.endTrajectory - position) / norm(*toolRay.endTrajectory - position);
auto& realRay = m_ecs.add<RayComponent>(realRayEntity, position, direction, speed, playerEntity);
......
......@@ -11,9 +11,14 @@ struct MyEcs;
struct CameraHandlingSystem
{
public:
void update(vcl::camera_scene& camera, MyEcs& myEcs, ecs::Entity const& playerEntity, ecs::Entity const& aimCursorEntity, PlayerControlComponent const& controls, TransformComponent const& transform);
void createRay(MyEcs& m_ecs, ecs::Entity const& playerEntity, ecs::Entity const& aimCursorEntity, vcl::camera_scene const& camera, TransformComponent const& playerTransform);
using Requirements = ecs::Requirements<ecs::Auto>;
using Requirements = ecs::Requirements<ecs::Auto, PlayerComponent>;
using UpdatePolicy = ecs::OncePerEntity;
private:
vcl::vec2 m_cursorPosition{ 0.0f, 0.2f };
};
#endif // !SYSTEMS_CAMERA_HANDLING_HPP_INCLUDED
......@@ -5,6 +5,7 @@ using namespace vcl;
enum CollisionDirection : int { Top, Bottom, Side };
static CollisionDirection getCollisionDirectionWithOneMoving(CollisionCylinder const& cylinderA, DerivedTransformComponent const& dTransformA, CollisionCylinder const& cylinderB);
static CollisionDirection getCollisionDirectionWithBothMoving(CollisionCylinder const& cylinderA, DerivedTransformComponent const& dTransformA, CollisionCylinder const& cylinderB, DerivedTransformComponent const& dTransformB);
constexpr auto EPSILON = 0.001f;
......@@ -36,6 +37,7 @@ void CollisionResolver::handleOneCollision(CollisionRateMap const& collisionRate
auto aCanMove = dTransforms.has(a.entity);
auto bCanMove = dTransforms.has(b.entity);
std::cout << collisionComponentA.material << collisionComponentB.material << std::endl;
auto collisionRates = collisionRateMap.find({ collisionComponentA.material, collisionComponentB.material });
assert(collisionRates != std::end(collisionRateMap));
auto slowdownRate = collisionRates->second.bounceSlowdown;
......@@ -91,9 +93,48 @@ struct NotImplementedError : public std::logic_error
{}
};
void CollisionResolver::handleCollisionWithBothMoving(CollisionCylinder const& /*collidingCylinderA*/, CollisionComponent& /*collisionComponentA*/, DerivedTransformComponent& /*dTransformA*/, CollisionCylinder const& /*collidingCylinderB*/, CollisionComponent& /*collisionComponentB*/, DerivedTransformComponent& /*dTransformB*/, float /*slowdownRate*/)
void CollisionResolver::handleCollisionWithBothMoving(CollisionCylinder const& cylinderA, CollisionComponent& collisionComponentA, DerivedTransformComponent& dTransformA, CollisionCylinder const& cylinderB, CollisionComponent& collisionComponentB, DerivedTransformComponent& dTransformB, float slowdownRate)
{
throw NotImplementedError{"Et c'est la panique !! Cette fonctionnalité n'est pas encore implémentée."};
CollisionDirection typeOfCollision = getCollisionDirectionWithBothMoving(cylinderA, dTransformA, cylinderB, dTransformB);
vec3 normal;
float dist;
switch (typeOfCollision)
{
case Top:
normal = vec3(0, 0, 1);// Formula for tilted normal : cylinderB.center - (cylinderA.center + vec3(0, 0, cylinderA.height / 2));
normal = normalize(normal);
dist = cylinderB.height + cylinderB.center.z - cylinderA.center.z + EPSILON;
collisionComponentA.shape.setPosition(collisionComponentA.shape.getPosition() + dist / 2 * normal);
collisionComponentB.shape.setPosition(collisionComponentB.shape.getPosition() - dist / 2 * normal);
break;
case Bottom:
normal = vec3(0, 0, -1);
normal = normal / norm(normal);
dist = cylinderB.height + cylinderB.center.z - cylinderA.center.z + EPSILON;
collisionComponentA.shape.setPosition(collisionComponentA.shape.getPosition() + dist / 2 * normal);
collisionComponentB.shape.setPosition(collisionComponentB.shape.getPosition() - dist / 2 * normal); break;
case Side:
auto positionA = collisionComponentA.shape.getPosition();
auto positionB = collisionComponentB.shape.getPosition();
normal = vec3(positionA.x - positionB.x, positionA.y - positionB.y, 0);
normal = normalize(normal);
vec2 posAHorizontal{ positionA.x, positionA.y };
vec2 posBHorizontal{ positionB.x, positionB.y };
dist = -norm(posAHorizontal - posBHorizontal) + cylinderB.radius + cylinderA.radius + EPSILON;
collisionComponentA.shape.setPosition(collisionComponentA.shape.getPosition() + dist / 2 * normal);
collisionComponentB.shape.setPosition(collisionComponentB.shape.getPosition() - dist / 2 * normal);
}
vec3& speedA = dTransformA.speed;
float dotProduct = dot(speedA, normal);
speedA = slowdownRate * (speedA - 2 * dotProduct * normal);
vec3& speedB = dTransformB.speed;
dotProduct = dot(speedB, normal);
speedB = slowdownRate * (speedB - 2 * dotProduct * normal);
}
static float getTimeAtDistance(vec3 const positionA, vec3 const speedA, vec3 const positionB, const float distance)
......@@ -116,3 +157,15 @@ static CollisionDirection getCollisionDirectionWithOneMoving(CollisionCylinder c
return CollisionDirection::Bottom;
return Side;
}
static CollisionDirection getCollisionDirectionWithBothMoving(CollisionCylinder const& cylinderA, DerivedTransformComponent const& dTransformA, CollisionCylinder const& cylinderB, DerivedTransformComponent const& dTransformB)
{
const float t_0 = getTimeAtDistance(cylinderA.center, dTransformA.speed - dTransformB.speed, cylinderB.center, cylinderA.radius + cylinderB.radius);
const float zAAtT0 = cylinderA.center.z + t_0 * dTransformA.speed.z;
const float zBAtT0 = cylinderB.center.z + t_0 * dTransformB.speed.z;
if (zAAtT0 > zBAtT0 + cylinderB.height)
return CollisionDirection::Top;
if (zAAtT0 + cylinderA.height < zBAtT0)
return CollisionDirection::Bottom;
return Side;
}
......@@ -3,6 +3,7 @@
void RayRender::update(scene_structure& scene, std::map<std::string, GLuint>& shaders, RayComponent const& ray)
{
segment_drawer.uniform_parameter.p1 = ray.position;
segment_drawer.uniform_parameter.p2 = *ray.endTrajectory;
vcl::vec3 shortEnd = ray.position + 2 * vcl::normalize(ray.direction);
segment_drawer.uniform_parameter.p2 = norm(shortEnd - ray.position) < norm(*ray.endTrajectory - ray.position) ? shortEnd : *ray.endTrajectory;
segment_drawer.draw(shaders["segment_im"], scene.camera);
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ static vcl::vec3 getPositionAtZ(vcl::vec3 const& a, vcl::vec3 const& b, float co
if (std::abs(a.z - b.z) < epsilon)
return a;
float const lambda = (z - a.z) / (b.z - a.z);
return a + lambda * b;
return (1 - lambda) * a + lambda * b;
}
......@@ -44,10 +44,15 @@ static std::optional<vcl::vec3> getBulletImpact(RayComponent const& ray, Collisi
res = intersect1;
else if (intersect1.z < cylinder.center.z)
{
res = getPositionAtZ(intersect1, intersect2, cylinder.center.z);
}
else if (intersect1.z > cylinder.center.z + cylinder.height)
{
res = getPositionAtZ(intersect1, intersect2, cylinder.center.z + cylinder.height);
}
return res;
}
......
......@@ -23,9 +23,9 @@ mat3 rotation_between_vector_mat3(const vec3& a, const vec3& b)
const vec3 u0 = normalize(a);
const vec3 u1 = normalize(b);
if( norm(u0-u1)<1e-4f )
if( norm(u0-u1)<5e-4f )
return mat3::identity();
if( norm(u0+u1)<1e-4f )
if( norm(u0+u1)<5e-4f )
return -mat3::identity();
const float d = dot(u0,u1);
......