Skip to content

Commit

Permalink
Jump animation
Browse files Browse the repository at this point in the history
  • Loading branch information
wivlaro committed Dec 14, 2024
1 parent 1d3ddd5 commit 0269877
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 31 deletions.
Binary file modified models/levels-src/level-1.blend
Binary file not shown.
Binary file modified models/levels-src/level-1.blend1
Binary file not shown.
Binary file modified models/levels/level-1.glb
Binary file not shown.
12 changes: 6 additions & 6 deletions src/Animator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ namespace MagnumGame {
auto targetBone = asset._bonesById.find(animatedObjectId);

if (targetBone != asset._bonesById.end()) {
Debug{} << "Track" << animationName << "track" << trackIndex
<< "target" << animationData.trackTarget(trackIndex)
<< targetBone->second.name
<< "type" << animationData.trackType(trackIndex)
<< "(name" << animationData.trackTargetName(trackIndex) << ")"
<< "result" << animationData.trackResultType(trackIndex);
// Debug{} << "Track" << animationName << "track" << trackIndex
// << "target" << animationData.trackTarget(trackIndex)
// << targetBone->second.name
// << "type" << animationData.trackType(trackIndex)
// << "(name" << animationData.trackTargetName(trackIndex) << ")"
// << "result" << animationData.trackResultType(trackIndex);
addAnimationTrack(animationData, trackIndex, targetBone->second, boneMap);
}
else {
Expand Down
9 changes: 6 additions & 3 deletions src/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,13 @@ namespace MagnumGame {
&_animatorDrawables, &_opaqueDrawables);
animationOffset.setTransformation(Matrix4::translation({0, -0.4f, 0}));

auto& rb = rigidBody->rigidBody();
//Prevent rotation in X & Z
rigidBody->rigidBody().setAngularFactor(btVector3(0.0f, 0.5f, 0.0f));
Debug{} << "Friction: " << rigidBody->rigidBody().getFriction() << "rolling friction" << rigidBody->rigidBody().getRollingFriction();
rigidBody->rigidBody().setRollingFriction(0.5f);
rb.setAngularFactor(btVector3(0.0f, 0.0f, 0.0f));
rb.setFriction(0.5f);
rb.setRollingFriction(0.5f);

Debug{} << "Friction: " << rb.getFriction() << "rolling friction" << rb.getRollingFriction();

_player.emplace("Someone", rigidBody, animator);
_player->resetToStart(Matrix4::translation({0,2,0}));
Expand Down
20 changes: 20 additions & 0 deletions src/MagnumGameApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ namespace MagnumGame {
_gameState->loadLevel(*gltfImporter);
_gameState->setupPlayer();

_tweakables->addDebugMode("Friction", 0, {
{
"Friction", [&]() {
return _gameState->getPlayer()->getBody()->rigidBody().getFriction();
},
[&](float value) {
_gameState->getPlayer()->getBody()->rigidBody().setFriction(value);
}
},
{
"Rolling Friction", [&]() {
return _gameState->getPlayer()->getBody()->rigidBody().
getRollingFriction();
},
[&](float value) {
_gameState->getPlayer()->getBody()->rigidBody().setRollingFriction(value);
}
}
});

#ifndef CORRADE_TARGET_EMSCRIPTEN
setSwapInterval(0);
setMinimalLoopPeriod(8.0_msec);
Expand Down
12 changes: 8 additions & 4 deletions src/MagnumGameApp_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
#include <sstream>
#include <Corrade/PluginManager/Manager.h>
#include <Corrade/Utility/Path.h>
#include <Magnum/Timeline.h>
#include <Magnum/Math/Matrix3.h>
#include <Magnum/Text/AbstractFont.h>
#include <Magnum/Text/Alignment.h>
#include <Magnum/Text/Renderer.h>
#include <Magnum/TextureTools/Atlas.h>
#include <Magnum/Math/Color.h>
#include <Magnum/SceneGraph/Camera.h>


#include "GameState.h"
#include "MagnumGameApp.h"
#include "Player.h"
#include "Tweakables.h"
#include "OnGroundQuery.h"

namespace MagnumGame {

Expand Down Expand Up @@ -106,7 +102,15 @@ namespace MagnumGame {
oss << std::setprecision(5);
oss << std::fixed;

auto onGround = _gameState->getPlayer()->isOnGround();

oss << "Pos " << rigidBody->transformationMatrix().translation();
if (onGround) {
oss << " OnGround";
}
else {
oss << " OffGround";
}
}
else {
oss << "No player";
Expand Down
44 changes: 28 additions & 16 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,40 @@ namespace MagnumGame {
bool Player::isAlive() const { return _pBody->rigidBody().isInWorld(); }

void Player::update(Float deltaTime) {
auto& rigidBody = _pBody->rigidBody();
auto velocity = btVector3(_control * WalkSpeed);
// auto motion = velocity * deltaTime;

auto currentVelocity = rigidBody.getLinearVelocity();
velocity.setY(currentVelocity.y());
rigidBody.setLinearVelocity(btVector3(velocity));

if (!_control.isZero()) {
auto motion = _control * WalkSpeed * deltaTime;
auto position = _pBody->transformation().translation();

auto matrix = Matrix4::lookAt({0,0,0}, -_control, {0,1,0});
matrix.translation() = position + motion;

matrix.translation() = position;
_pBody->setTransformation(matrix);
_pBody->syncPose();
}

_isOnGround = OnGroundQueryResult{rigidBody}.run(_pBody->getWorld());
if (_markJumpFrame && _isOnGround) {
rigidBody.applyImpulse({0,4.0f,0},{});
}
_markJumpFrame = false;

if (_animator != nullptr) {
if (!_isOnGround) {
_animator->play("jump", false);
}
else if (_control.isZero()) {
_animator->play("idle", false);
}
else {
_animator->play("walk", false);
}
}

}

Vector3 Player::getPosition() const { return _pBody->transformation().translation(); }
Expand All @@ -62,15 +85,6 @@ namespace MagnumGame {
void Player::setControl(const Vector3 &control) {
if (_control != control) {
_control = control;

if (_animator != nullptr) {
if (_control.isZero()) {
_animator->play("idle", false);
}
else {
_animator->play("walk", false);
}
}
}
}

Expand All @@ -95,8 +109,6 @@ namespace MagnumGame {

void Player::tryJump() {

if (OnGroundQueryResult{_pBody->rigidBody()}.run(_pBody->getWorld())) {
_pBody->rigidBody().applyImpulse({0,4.0f,0},{});
}
_markJumpFrame = true;
}
}
4 changes: 4 additions & 0 deletions src/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ namespace MagnumGame {

RigidBody* getBody() { return _pBody; }

bool isOnGround() const { return _isOnGround; }

private:
std::string _name;
RigidBody *_pBody;
Animator *_animator;
IEnableDrawable *_pBodyDrawable{};
Vector3 _control{};
bool _markJumpFrame;
bool _isOnGround;
};

}
5 changes: 3 additions & 2 deletions src/Tweakables.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace MagnumGame {
class Tweakables {
public:
Tweakables();
explicit Tweakables();

/**
* @brief Named pointer for tweaking a configurable float value at runtime
Expand Down Expand Up @@ -38,8 +38,9 @@ namespace MagnumGame {
void tweakBy(int sign, int powerAdjust) {
float value = get();

float absValue = std::max(abs(value), 0.00001f);
//Compute the nearest sensible power of 10 to for adjusting a value
set(value + sign * pow(10, round(log10(abs(value))) + powerAdjust));
set(value + sign * pow(10, round(log10(absValue)) + powerAdjust));
}

private:
Expand Down

0 comments on commit 0269877

Please sign in to comment.