@@ -273,7 +273,7 @@ namespace ECS::Systems
273273 if (registry->all_of <Components::Model>(targetEntity))
274274 {
275275 auto & model = registry->get <Components::Model>(targetEntity);
276- modelLoader->SetModelHighlight (model, 1 .5f );
276+ modelLoader->SetModelHighlight (model, 1 .25f );
277277 }
278278 }
279279
@@ -374,190 +374,6 @@ namespace ECS::Systems
374374 return true ;
375375 });
376376 }
377-
378- #if USE_CHARACTER_CONTROLLER_V2
379- void CharacterController::Update (entt::registry& registry, f32 deltaTime)
380- {
381- InputManager* inputManager = ServiceLocator::GetInputManager ();
382- KeybindGroup* keybindGroup = inputManager->GetKeybindGroupByHash (" CharacterController" _h);
383-
384- Util::EventUtil::OnEvent<Components::MapLoadedEvent>([&](const Components::MapLoadedEvent& event)
385- {
386- InitCharacterController (registry);
387- });
388-
389- if (!keybindGroup->IsActive ())
390- return ;
391-
392- entt::registry::context& ctx = registry.ctx ();
393- auto & orbitalCameraSettings = ctx.get <Singletons::OrbitalCameraSettings>();
394- bool isInputForwardDown = keybindGroup->IsKeybindPressed (" Forward" _h) || (orbitalCameraSettings.mouseLeftDown && orbitalCameraSettings.mouseRightDown );
395- bool isInputBackwardDown = keybindGroup->IsKeybindPressed (" Backward" _h);
396- bool isInputLeftDown = keybindGroup->IsKeybindPressed (" Left" _h);
397- bool isInputRightDown = keybindGroup->IsKeybindPressed (" Right" _h);
398-
399- bool isMovingForward = (isInputForwardDown && !isInputBackwardDown);
400- bool isMovingBackward = (isInputBackwardDown && !isInputForwardDown);
401- bool isMovingLeft = (isInputLeftDown && !isInputRightDown);
402- bool isMovingRight = (isInputRightDown && !isInputLeftDown);
403- bool isMoving = isMovingForward || isMovingBackward || isMovingLeft || isMovingRight;
404-
405- auto & characterSingleton = ctx.get <Singletons::CharacterSingleton>();
406- auto & characterMovementInfo = registry.get <Components::MovementInfo>(characterSingleton.modelEntity );
407- auto & characterModelTransform = registry.get <Components::Transform>(characterSingleton.modelEntity );
408-
409- DebugRenderer* debugRenderer = ServiceLocator::GetGameRenderer ()->GetDebugRenderer ();
410- auto & transformSystem = TransformSystem::Get (registry);
411- auto & joltState = ctx.get <Singletons::JoltState>();
412- static vec3 Gravity = vec3 (0 .0f , -19 .291105f , 0 .0f );
413-
414- quat characterRotation = quat (vec3 (characterMovementInfo.pitch , characterMovementInfo.yaw , 0 .0f ));
415- transformSystem.SetWorldRotation (characterSingleton.modelEntity , characterRotation);
416-
417- vec3 characterModelStartPosition = characterModelTransform.GetWorldPosition ();
418- vec3 characterModelEndPosition = characterModelStartPosition;
419- quat characterModelRotation = characterModelTransform.GetWorldRotation ();
420-
421- vec3 horizontalMoveVector = vec3 ((isMovingLeft * 1 .0f ) + (isMovingRight * -1 .0f ), 0 .0f , (isMovingForward * -1 .0f ) + (isMovingBackward * 1 .0f ));
422- vec3 horizontalVelocity = vec3 (0 .0f );
423- if (isMoving)
424- {
425- horizontalMoveVector = glm::normalize (horizontalMoveVector);
426- }
427-
428- bool wasGrounded = characterMovementInfo.movementFlags .grounded ;
429- bool isGrounded = false ;
430-
431- // Check if we are still grounded
432- if (wasGrounded)
433- {
434- vec3 rayStart = characterModelStartPosition + Components::Transform::WORLD_UP;
435- vec3 rayDirection = -Components::Transform::WORLD_UP * 2 .0f ;
436- JPH::RayCastResult hitResult;
437-
438- if (::Util::Physics::CastRay (joltState.physicsSystem , rayStart, rayDirection, hitResult))
439- {
440- if (hitResult.mFraction <= 0 .51f )
441- {
442- vec3 hitPos = rayStart + (hitResult.mFraction * rayDirection);
443-
444- characterModelStartPosition = hitPos;
445- isGrounded = true ;
446-
447- if (isMoving)
448- {
449- if (JPH::Body* body = joltState.physicsSystem .GetBodyLockInterface ().TryGetBody (hitResult.mBodyID ))
450- {
451- JPH::Vec3 surfaceNormal = body->GetWorldSpaceSurfaceNormal (hitResult.mSubShapeID2 , JPH::Vec3 (characterModelStartPosition.x , characterModelStartPosition.y , characterModelStartPosition.z ));
452- vec3 surfaceNormalVec = glm::normalize (vec3 (surfaceNormal.GetX (), surfaceNormal.GetY (), surfaceNormal.GetZ ()));
453-
454- vec3 localForward = glm::normalize (characterModelTransform.GetLocalForward ());
455- vec3 rightVec = glm::normalize (glm::cross (surfaceNormalVec, localForward));
456- vec3 forwardVec = glm::normalize (glm::cross (rightVec, surfaceNormalVec));
457-
458- // Draw Surface Normal, RightVec, ForwardVec
459- debugRenderer->DrawLine3D (hitPos, hitPos + surfaceNormalVec, Color::Green);
460- debugRenderer->DrawLine3D (characterModelStartPosition, characterModelStartPosition + rightVec, Color::Red);
461- debugRenderer->DrawLine3D (characterModelStartPosition, characterModelStartPosition + forwardVec, Color::Blue);
462-
463- horizontalVelocity = forwardVec * horizontalMoveVector.z + rightVec * horizontalMoveVector.x ;
464- };
465- }
466- }
467- }
468- }
469-
470- if (isGrounded)
471- {
472- if (isMoving)
473- {
474- vec3 velocity = horizontalVelocity * characterMovementInfo.speed ;
475- vec3 verticalVelocity = vec3 (0 .0f , 0 .0f , 0 .0f );
476-
477- vec3 horizontalMove = velocity * deltaTime;
478-
479- vec3 rayStart = characterModelStartPosition + Components::Transform::WORLD_UP;
480- vec3 rayDirection = horizontalMove;
481- JPH::RayCastResult hitResult;
482-
483- if (::Util::Physics::CastRay (joltState.physicsSystem , rayStart, rayDirection, hitResult))
484- {
485- vec3 hitPos = rayStart + (hitResult.mFraction * rayDirection);
486- characterModelEndPosition = hitPos;
487- }
488- else
489- {
490- characterModelEndPosition = characterModelStartPosition + horizontalMove;
491- }
492- }
493- }
494- else
495- {
496- vec3 gravityDirection = Gravity * deltaTime;
497-
498- JPH::Vec3 start = JPH::Vec3 (characterModelStartPosition.x , characterModelStartPosition.y + 1 .0f , characterModelStartPosition.z );
499- JPH::Vec3 direction = JPH::Vec3 (gravityDirection.x , gravityDirection.y + -1 .0f , gravityDirection.z );
500-
501- JPH::RRayCast ray (start, direction);
502- JPH::RayCastResult hit;
503-
504- if (joltState.physicsSystem .GetNarrowPhaseQuery ().CastRay (ray, hit))
505- {
506- JPH::Vec3 hitPos = ray.GetPointOnRay (hit.mFraction );
507- vec3 hitPosition = vec3 (hitPos.GetX (), hitPos.GetY (), hitPos.GetZ ());
508-
509- characterModelEndPosition = hitPosition;
510- isGrounded = true ;
511- }
512- else
513- {
514- characterModelEndPosition = characterModelStartPosition + gravityDirection;
515- }
516- }
517-
518- characterMovementInfo.movementFlags .grounded = isGrounded;
519- transformSystem.SetWorldPosition (characterSingleton.entity , characterModelEndPosition);
520- }
521-
522- void CharacterController::InitCharacterController (entt::registry& registry)
523- {
524- entt::registry::context& ctx = registry.ctx ();
525-
526- auto & characterSingleton = ctx.get <Singletons::CharacterSingleton>();
527- auto & movementInfo = registry.get <Components::MovementInfo>(characterSingleton.modelEntity );
528-
529- ModelLoader* modelLoader = ServiceLocator::GetGameRenderer ()->GetModelLoader ();
530- modelLoader->LoadDisplayIDForEntity (characterSingleton.modelEntity , 50 );
531-
532- MapLoader* mapLoader = ServiceLocator::GetGameRenderer ()->GetMapLoader ();
533- JPH::Vec3 newPosition = JPH::Vec3 (-1500 .0f , 350 .0f , 1250 .0f );
534-
535- if (mapLoader->GetCurrentMapID () == std::numeric_limits<u32 >().max ())
536- {
537- newPosition = JPH::Vec3 (0 .0f , 0 .0f , 0 .0f );
538- }
539-
540- characterSingleton.targetEntity = entt::null;
541- characterSingleton.positionOrRotationUpdateTimer = 0 .0f ;
542- characterSingleton.positionOrRotationIsDirty = true ;
543- characterSingleton.canControlInAir = true ;
544-
545- movementInfo.pitch = 0 .0f ;
546- movementInfo.yaw = 0 .0f ;
547- movementInfo.speed = 7 .1111f ;
548- movementInfo.jumpSpeed = 7 .9555f ;
549- movementInfo.gravityModifier = 1 .0f ;
550- movementInfo.jumpState = Components::JumpState::None;
551-
552- auto & transformSystem = ctx.get <TransformSystem>();
553- auto & orbitalCameraSettings = ctx.get <Singletons::OrbitalCameraSettings>();
554-
555- transformSystem.ParentEntityTo (characterSingleton.entity , orbitalCameraSettings.entity );
556- transformSystem.SetLocalPosition (orbitalCameraSettings.entity , orbitalCameraSettings.cameraCurrentZoomOffset );
557- transformSystem.SetWorldPosition (characterSingleton.entity , vec3 (newPosition.GetX (), newPosition.GetY (), newPosition.GetZ ()));
558- }
559-
560- #else
561377 void CharacterController::Update (entt::registry& registry, f32 deltaTime)
562378 {
563379 ZoneScopedN (" ECS::CharacterController::Preprocessing" );
@@ -968,5 +784,4 @@ namespace ECS::Systems
968784 characterSingleton.moverEntity = entt::null;
969785 }
970786 }
971- #endif
972787}
0 commit comments