Temporary movement code.

This commit is contained in:
King_DuckZ 2014-03-05 22:09:32 +01:00
parent fce08ed793
commit d27b5773ca
3 changed files with 37 additions and 13 deletions

View file

@ -62,7 +62,7 @@ namespace cloonel {
void GameplaySceneClassic::Prepare() {
std::unique_ptr<MoverSine> moverSine(new MoverSine());
std::unique_ptr<Character> player(new Character("resources/graphics/player.png", SDLObject(), ushort2(80, 120)));
std::unique_ptr<MoverLeftRight> moverLeftRight(new MoverLeftRight());
std::unique_ptr<MoverLeftRight> moverLeftRight(new MoverLeftRight(1.5f, 5.0f, 40.0f));
player->Prepare();
moverSine->RegisterPlaceable(player.get());

View file

@ -18,27 +18,49 @@
*/
#include "moverleftright.hpp"
#include <algorithm>
#include <cmath>
namespace cloonel {
namespace {
float Clamp ( float parValue, float parMin, float parMax ) __attribute__((pure));
float Clamp ( float parValue, float parMin, float parMax ) {
assert(parMin <= parMax);
return std::max(parMin, std::min(parMax, parValue));
}
} //unnamed namespace
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
MoverLeftRight::MoverLeftRight() :
m_movement(0.0f),
m_velocity(320.0f),
m_step(0.0f)
MoverLeftRight::MoverLeftRight (float parMaxVelocity, float parMass, float parForce) :
//TODO: mass does not belong to here
m_velocity(0.0f),
m_maxVelocity(parMaxVelocity),
m_invMass(1.0f / parMass),
m_force(parForce),
m_movementTarget(0.0f)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void MoverLeftRight::ApplyMotion (float parDelta) {
m_step = m_velocity * parDelta * m_movement;
//TODO: implement a movement that is more mathematically correct and
//that feels nice.
//const float forceDirection = std::abs(m_movementTarget) * 2.0f - 1.0f;
const float notMoving = 1.0f - std::abs(m_movementTarget);
const float friction = 1.757f * m_velocity * notMoving * parDelta;
m_velocity += m_movementTarget * m_force * m_invMass * parDelta - std::copysign(friction, m_velocity);
m_velocity = Clamp(m_velocity, -m_maxVelocity, m_maxVelocity);
if (m_movementTarget == 0.0f and std::abs(m_velocity) < 0.1f)
m_velocity = 0.0f;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
float2 MoverLeftRight::GetOffset() const {
return float2(m_step, 0.0f);
return float2(m_velocity, 0.0f);
}
///--------------------------------------------------------------------------
@ -46,13 +68,13 @@ namespace cloonel {
void MoverLeftRight::SetMovement (MovementDirectionType parDirection) {
switch (parDirection) {
case MovementDirection_Left:
m_movement = -1.0f;
m_movementTarget = -1.0f;
break;
case MovementDirection_Right:
m_movement = 1.0f;
m_movementTarget = 1.0f;
break;
case MovementDirection_Still:
m_movement = 0.0f;
m_movementTarget = 0.0f;
break;
}
}

View file

@ -31,7 +31,7 @@ namespace cloonel {
MovementDirection_Still
};
MoverLeftRight ( void );
MoverLeftRight ( float parMaxVelocity, float parMass, float parForce );
virtual ~MoverLeftRight ( void ) noexcept = default;
void SetMovement ( MovementDirectionType parDirection );
@ -40,9 +40,11 @@ namespace cloonel {
virtual float2 GetOffset ( void ) const;
virtual void ApplyMotion ( float parDelta );
float m_movement;
float m_velocity;
float m_step;
float m_maxVelocity;
float m_invMass;
float m_force;
float m_movementTarget;
};
} //namespace cloonel