-
Notifications
You must be signed in to change notification settings - Fork 13
Add basic orbital camera implementation #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maaxxaam
wants to merge
9
commits into
OpenAWE-Project:master
Choose a base branch
from
maaxxaam:orbital-cam
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
24b47f6
Tweak pitch limit on FreeCamera to avoid warping
maaxxaam bbc84fa
Add raycastStatic() method for PhysicsMan
maaxxaam 8a15aca
Add ControlledOrbitalCamera
maaxxaam 924ae8a
Move near clipping plane closer
maaxxaam a498f6f
Add PhysicsMan::shapeCastStatic method
maaxxaam 10ffbc1
Improve ControlledOrbitalCamera's behaviour
maaxxaam 4e64b07
Change return type of casting methods to use glm values
maaxxaam 1bd9208
Add comments for OrbitalCamera class
maaxxaam 27e2167
Add sensitivity fields for ControlledOrbitalCamera
maaxxaam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| }; | ||
|
|
||
| typedef std::shared_ptr<ControlledOrbitalCamera> ControlledOrbitalCameraPtr; | ||
|
|
||
|
|
||
| #endif //OPENAWE_CONTROLLEDORBITALCAMERA_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.