Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions src/controlledorbitalcamera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* OpenAWE - A reimplementation of Remedys Alan Wake Engine
*
* OpenAWE is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* OpenAWE is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* OpenAWE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenAWE. If not, see <http://www.gnu.org/licenses/>.
*/

#include "src/common/crc32.h"

#include "src/events/eventman.h"

#include "src/physics/charactercontroller.h"
#include "src/physics/physicsman.h"

#include "src/controlledorbitalcamera.h"

static constexpr uint32_t kRotateMouse = Common::crc32("ORBITALCAM_ROTATE_MOUSE");
static constexpr uint32_t kRotateGamepad = Common::crc32("ORBITALCAM_ROTATE_GAMEPAD");

static constexpr uint32_t kSwitchShoulder = Common::crc32("ORBITALCAM_SWITCH_SHOULDER");
static constexpr uint32_t kAimWeapon = Common::crc32("ORBITALCAM_AIM_WEAPON");
static constexpr uint32_t kFocusCamera = Common::crc32("ORBITALCAM_FOCUS_CAMERA");

ControlledOrbitalCamera::ControlledOrbitalCamera() : _castSphere(0.75f) {
Events::EventCallback callbackRotate = [this](auto && PH1) { handleRotation(std::forward<decltype(PH1)>(PH1)); };
Events::EventCallback callbackStates = [this](auto && PH1) { handleStates(std::forward<decltype(PH1)>(PH1)); };

EventMan.setActionCallback(
{kSwitchShoulder, kAimWeapon, kFocusCamera},
callbackStates
);

EventMan.setActionCallback(
{kRotateMouse, kRotateGamepad},
callbackRotate
);

EventMan.addBinding(kSwitchShoulder, Events::kKeyTab);
EventMan.addBinding(kAimWeapon, Events::kMouseLeft);
EventMan.addBinding(kFocusCamera, Events::kKeyF);

EventMan.addBinding(kSwitchShoulder, Events::kGamepadButtonLeftBumper);
EventMan.addBinding(kAimWeapon, Events::kGamepadButtonRightBumper);
EventMan.addBinding(kFocusCamera, Events::kGamepadButtonA);

EventMan.add2DAxisBinding(kRotateMouse, Events::kMousePosition);
EventMan.add2DAxisBinding(kRotateGamepad, Events::kGamepadAxisRight);
}

void ControlledOrbitalCamera::handleRotation(const Events::Event &event) {
const Events::AxisEvent<glm::vec2> axisEvent = std::get<Events::AxisEvent<glm::vec2>>(event.data);
if (event.action == kRotateMouse) {
_movementRotation.x += axisEvent.delta.x * _mouseSensitivity;
_movementRotation.y += axisEvent.delta.y * _mouseSensitivity;
} else if (event.action == kRotateGamepad) {
_movementRotation.x += axisEvent.absolute.x * _gamepadSensitivity;
_movementRotation.y += axisEvent.absolute.y * _gamepadSensitivity;
}
}

void ControlledOrbitalCamera::handleStates(const Events::Event &event) {
const Events::KeyEvent keyEvent = std::get<Events::KeyEvent>(event.data);
if (event.action == kAimWeapon) {
if (keyEvent.state == Events::kPress && _state == Graphics::kCameraDefault) {
setAiming(true);
} else if (keyEvent.state == Events::kRelease && _state == Graphics::kCameraAiming) {
setAiming(false);
}
} else if (keyEvent.state == Events::kPress) {
if (event.action == kSwitchShoulder) {
switchShoulder();
} else if (event.action == kFocusCamera) {
if (_state == Graphics::kCameraDefault) {
focusOn(glm::vec3(1000, 1000, 1000));
} else if (_state == Graphics::kCameraFocused) {
unfocus();
}
}
}
}

void ControlledOrbitalCamera::attachTo(Physics::CharacterControllerPtr object) {
_followedObject = object;
glm::vec3 newOrigin = _followedObject->getUpperPosition();
newOrigin.y += 0.6f;
setOrbitOrigin(newOrigin);
_orbitOriginCurrent = _orbitOriginTarget;
_rotationOrientation = glm::vec3(-M_PI, 0.0f, 0.0f) * object->getRotation();
_position = calcOrbitPosition(_orbitRadiusCurrent);
_direction = _rotationDirection;
}

void ControlledOrbitalCamera::update(float delta) {
glm::vec3 newOrigin = _followedObject->getUpperPosition();
newOrigin.y += 0.6f;
setOrbitOrigin(newOrigin);
Graphics::OrbitalCamera::update(delta);
// Resolving colisions with static objects
const glm::vec4
mat1(1.f, 0.f, 0.f, 0.f),
mat2(0.f, 1.f, 0.f, 0.f),
mat3(0.f, 0.f, 1.f, 0.f);
const glm::vec3 basePoint{calcOrbitPosition(_orbitRadiusTargetFull)};
const glm::vec4 cameraOrigin{_orbitOriginCurrent.x, _orbitOriginCurrent.y, -_orbitOriginCurrent.z, 1.f};
glm::mat4
from{mat1, mat2, mat3, cameraOrigin},
to{mat1, mat2, mat3, glm::vec4(basePoint.x, basePoint.y, -basePoint.z, 1.f)};
Physics::CastResult camCast = PhysicsMan.shapeCastStatic(&_castSphere, from, to);
if (camCast.hasHit) {
float rad = glm::length(camCast.rayHitPoint - glm::vec3(_orbitOriginCurrent.x, _orbitOriginCurrent.y, -_orbitOriginCurrent.z));
snapRadius(rad);
};
}
56 changes: 56 additions & 0 deletions src/controlledorbitalcamera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* OpenAWE - A reimplementation of Remedys Alan Wake Engine
*
* OpenAWE is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* OpenAWE is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* OpenAWE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenAWE. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef OPENAWE_CONTROLLEDORBITALCAMERA_H
#define OPENAWE_CONTROLLEDORBITALCAMERA_H

#include <memory>

#include <bullet/btBulletDynamicsCommon.h>

#include "src/events/event.h"

#include "src/graphics/orbitalcamera.h"

#include "src/physics/charactercontroller.h"

class ControlledOrbitalCamera : public Graphics::OrbitalCamera {
public:
ControlledOrbitalCamera();

void attachTo(Physics::CharacterControllerPtr object);

void update(float delta) override;

private:
void handleRotation(const Events::Event &event);
void handleStates(const Events::Event &event);

float _mouseSensitivity = 1.0f, _gamepadSensitivity = 10.0f;

Physics::CharacterControllerPtr _followedObject;

btSphereShape _castSphere;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I described in another comment, bullet stuff should be exclusively located in the physics part

Copy link
Contributor Author

@maaxxaam maaxxaam Mar 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea of this was to avoid frequent creation-destruction of bullet shapes for handling casts (especially because it happens every frame for the orbital camera) by storing an object used for collision inside the camera and passing it as a parameter. This could be instead mitigated by storing a sphere object in PhysicsManager (which seemed inappropriate at the time as you would have to keep changing sphere's radius for each cast) or by creating an object pool for the shapes.

};

typedef std::shared_ptr<ControlledOrbitalCamera> ControlledOrbitalCameraPtr;


#endif //OPENAWE_CONTROLLEDORBITALCAMERA_H
14 changes: 12 additions & 2 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
#include "src/common/strutil.h"
#include "src/common/threadpool.h"

#include "src/physics/charactercontroller.h"

#include "src/video/playerprocess.h"

#include "src/graphics/gfxman.h"
#include "src/graphics/animationcontroller.h"

#include "src/controlledorbitalcamera.h"
#include "src/engine.h"
#include "src/task.h"
#include "src/utils.h"
Expand Down Expand Up @@ -149,8 +152,15 @@ void Engine::initEpisode() {
if (!task.isActiveOnStartup())
continue;

if (!task.getPlayerCharacter().isNil())
_playerController.setPlayerCharacter(getEntityByGID(_registry, task.getPlayerCharacter()));
if (!task.getPlayerCharacter().isNil()) {
auto playerEntity = getEntityByGID(_registry, task.getPlayerCharacter());
ControlledOrbitalCameraPtr cam
= _registry.emplace<ControlledOrbitalCameraPtr>(playerEntity)
= std::make_shared<ControlledOrbitalCamera>();
cam->attachTo(_registry.get<Physics::CharacterControllerPtr>(playerEntity));
_playerController.setPlayerCharacter(playerEntity);

}

spdlog::debug("Firing OnTaskActivate on {} {:x}", gid.type, gid.id);
bytecode->run(*_context, "OnTaskActivate", item);
Expand Down
9 changes: 4 additions & 5 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
#include "src/sound/soundman.h"

#include "src/game.h"
#include "src/controlledfreecamera.h"
#include "src/controlledorbitalcamera.h"
#include "src/task.h"
#include "src/timerprocess.h"
#include "src/transform.h"
Expand Down Expand Up @@ -275,9 +275,8 @@ void Game::init() {
void Game::start() {
spdlog::info("Starting AWE...");

ControlledFreeCamera freeCamera;

GfxMan.setCamera(freeCamera);
ControlledOrbitalCameraPtr camera = _registry.get<ControlledOrbitalCameraPtr>(_registry.view<ControlledOrbitalCameraPtr>().back());
GfxMan.setCamera(*(camera.get()));

Graphics::Text text;
text.setText(u"OpenAWE - v0.0.1");
Expand Down Expand Up @@ -376,7 +375,7 @@ void Game::start() {
_registry.get<Graphics::Light>(lightEntity).setTransform(transform.getTransformation());
}

freeCamera.update(time - lastTime);
camera->update(time - lastTime);

PhysicsMan.update(time - lastTime);
GfxMan.drawFrame();
Expand Down
3 changes: 3 additions & 0 deletions src/graphics/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ class Camera {

virtual void update(float time);

float getFOV() { return _fov; };

protected:
glm::vec3 _position;
glm::vec3 _direction;
glm::vec3 _up;
float _fov = 45.f;
};

} // End of namespace Graphics
Expand Down
5 changes: 3 additions & 2 deletions src/graphics/freecamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ FreeCamera::FreeCamera() :
_clearDirection(false), _clearRotation(false) {}

void FreeCamera::update(float delta) {
constexpr double cameraPitchLimit = M_PI_2 - 1e-5;
_rotationAttitude.x += glm::radians(_movementRotation.x * delta * _rotationFactor);
_rotationAttitude.y -= glm::radians(_movementRotation.y * delta * _rotationFactor);
// roll is not used so far
_rotationAttitude.z += glm::radians(_movementRotation.z * delta * _rotationFactor);
// _rotationAttitude.z += glm::radians(_movementRotation.z * delta * _rotationFactor);
// limit pitch to -90..90 degree range
_rotationAttitude.y = glm::clamp(double(_rotationAttitude.y), -M_PI_2, M_PI_2);
_rotationAttitude.y = glm::clamp(double(_rotationAttitude.y), -cameraPitchLimit, cameraPitchLimit);
_direction.x = cos(_rotationAttitude.y) * cos(_rotationAttitude.x);
_direction.y = sin(_rotationAttitude.y);
_direction.z = sin(_rotationAttitude.x) * cos(_rotationAttitude.y);
Expand Down
Loading