Merge branch 'NewSmoothCamera'

This commit is contained in:
auria.mg 2017-01-30 18:51:24 -05:00
commit 62e8ec86fa
2 changed files with 51 additions and 35 deletions

View File

@ -38,11 +38,10 @@
*/
CameraNormal::CameraNormal(Camera::CameraType type, int camera_index,
AbstractKart* kart)
: Camera(type, camera_index, kart)
: Camera(type, camera_index, kart), m_camera_offset(0, 0, -15.0f)
{
m_distance = kart ? kart->getKartProperties()->getCameraDistance() : 1000.0f;
m_ambient_light = Track::getCurrentTrack()->getDefaultAmbientColor();
m_smooth_dt = 0.0f;
// TODO: Put these values into a config file
// Global or per split screen zone?
@ -55,6 +54,10 @@ CameraNormal::CameraNormal(Camera::CameraType type, int camera_index,
m_rotation_range = 0.0f;
reset();
m_camera->setNearValue(1.0f);
btTransform btt = kart->getTrans();
m_kart_position = btt.getOrigin();
m_kart_rotation = btt.getRotation();
} // Camera
//-----------------------------------------------------------------------------
@ -69,15 +72,15 @@ void CameraNormal::smoothMoveCamera(float dt)
if (kart->isFlying())
{
Vec3 vec3 = m_kart->getXYZ() + Vec3(sin(m_kart->getHeading()) * -4.0f,
0.5f,
cos(m_kart->getHeading()) * -4.0f);
0.5f,
cos(m_kart->getHeading()) * -4.0f);
m_camera->setTarget(m_kart->getXYZ().toIrrVector());
m_camera->setPosition(vec3.toIrrVector());
return;
} // kart is flying
core::vector3df current_position = m_camera->getPosition();
core::vector3df current_position = m_camera->getPosition();
// Smoothly interpolate towards the position and target
const KartProperties *kp = m_kart->getKartProperties();
float max_increase_with_zipper = kp->getZipperMaxSpeedIncrease();
@ -88,44 +91,54 @@ void CameraNormal::smoothMoveCamera(float dt)
float skid_factor = ks->getVisualSkidRotation();
float skid_angle = asin(skid_factor);
float ratio = (current_speed - max_speed_without_zipper) / max_increase_with_zipper;
float ratio = current_speed / max_speed_without_zipper;
ratio = ratio > -0.12f ? ratio : -0.12f;
// distance of camera from kart in x and z plane
float camera_distance = -3 * (0.5f + ratio);
if (camera_distance > -2.0f) camera_distance = -2.0f;
float camera_distance = -4.0f * ratio;
if (camera_distance > -2.0f) camera_distance = -2.0f; // don't get too close to the kart
// Defines how far camera should be from player kart.
Vec3 camera_offset(camera_distance * sin(skid_angle / 2),
1.1f * (1 + ratio / 2),
camera_distance * cos(skid_angle / 2));
Vec3 m_kart_camera_position_with_offset = m_kart->getTrans()(camera_offset);
Vec3 wanted_camera_offset(camera_distance * sin(skid_angle / 2),
1.1f * (1 + ratio / 2),
camera_distance * cos(skid_angle / 2));
//m_smooth_dt = 0.3f * dt + 0.7f * m_smooth_dt;
float delta = (dt*5.0f);
if (delta < 0.0f)
delta = 0.0f;
else if (delta > 1.0f)
delta = 1.0f;
m_camera_offset += (wanted_camera_offset - m_camera_offset) * delta;
float delta2 = dt * 8.0f;
if (delta2 < 0)
delta2 = 0;
else if (delta2 > 1)
delta2 = 1;
btTransform btt = m_kart->getTrans();
m_kart_position = btt.getOrigin();// m_kart_position + (btt.getOrigin() - m_kart_position) * delta2;
m_kart_rotation = m_kart_rotation.normalized().slerp(btt.getRotation().normalized(), delta2);
btt.setOrigin(m_kart_position);
btt.setRotation(m_kart_rotation);
Vec3 m_kart_camera_position_with_offset = btt(m_camera_offset);
// next target
Vec3 current_target = m_kart->getTrans()(Vec3(0, 0.5f, 0));
Vec3 current_target = btt(Vec3(0, 0.5f, 0));
// new required position of camera
core::vector3df wanted_position = m_kart_camera_position_with_offset.toIrrVector();
current_position = m_kart_camera_position_with_offset.toIrrVector();
float f = 5.0f;
if ((current_speed > 5 ) || (current_speed < 0 ))
{
f = current_speed >0 ? current_speed/3 + 1.0f
: -1.5f * current_speed + 2.0f;
}
m_smooth_dt = 0.3f * dt + 0.7f * m_smooth_dt;
current_position += (wanted_position - current_position) * (m_smooth_dt*f);
// Avoid camera crash: if the speed is negative, the current_position
// can oscillate between plus and minus, getting bigger and bigger. If
// this happens often enough, floating point overflow happens (large
// negative speeds can happen when the kart is tumbling/falling)
// To avoid this, we just move the camera to the wanted position if
// the distance becomes too large (see #1356).
if( (current_position - wanted_position).getLengthSQ() > 100)
{
Log::debug("camera", "Resetting camera position to avoid crash");
current_position = wanted_position;
}
//Log::info("CAM_DEBUG", "OFFSET: %f %f %f TRANSFORMED %f %f %f TARGET %f %f %f",
// wanted_camera_offset.x(), wanted_camera_offset.y(), wanted_camera_offset.z(),
// m_kart_camera_position_with_offset.x(), m_kart_camera_position_with_offset.y(),
// m_kart_camera_position_with_offset.z(), current_target.x(), current_target.y(),
// current_target.z());
if(getMode()!=CM_FALLING)
m_camera->setPosition(current_position);

View File

@ -49,8 +49,8 @@ private:
/** Factor of the effects of steering in camera aim. */
float m_rotation_range;
/** Used to smoothly move the camera. */
float m_smooth_dt;
Vec3 m_camera_offset;
void smoothMoveCamera(float dt);
void handleEndCamera(float dt);
void getCameraSettings(float *above_kart, float *cam_angle,
@ -59,6 +59,9 @@ private:
void positionCamera(float dt, float above_kart, float cam_angle,
float side_way, float distance, float smoothing);
btVector3 m_kart_position;
btQuaternion m_kart_rotation;
// Give a few classes access to the constructor (mostly for inheritance)
friend class Camera;
friend class CameraDebug;