From bf90805448b670a3d35d296e1cf32c296db83a37 Mon Sep 17 00:00:00 2001 From: hikerstk Date: Thu, 21 Jul 2011 00:07:09 +0000 Subject: [PATCH] Added bouncing to rubber ball (though it's imho not yet smooth enough). git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@9313 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/items/rubber_ball.cpp | 14 ++++++++------ src/items/rubber_ball.hpp | 7 +++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/items/rubber_ball.cpp b/src/items/rubber_ball.cpp index 5c810d00e..dd9e80614 100644 --- a/src/items/rubber_ball.cpp +++ b/src/items/rubber_ball.cpp @@ -68,7 +68,7 @@ RubberBall::RubberBall(Kart *kart) : Flyable(kart, PowerupManager::POWERUP_RUBBE // target: m_aiming_at_target = false; m_wrapped_around = false; - + m_previous_height = 0.0f; } // RubberBall // ----------------------------------------------------------------------------- @@ -156,7 +156,6 @@ void RubberBall::update(float dt) // FIXME: do we want to test if we have overtaken the target kart? -#if 0 LinearWorld *world = dynamic_cast(World::getWorld()); float target_distance = @@ -165,16 +164,19 @@ void RubberBall::update(float dt) float x = target_distance - m_distance_along_track; if(x<0) x+=track_length; + + // A formula to determine height depending on distance to target float height = 0.5f*sqrt(x)*fabsf(sinf(10.0f*log(0.005f*(x+5.0f)))); // Stephen_irc:: float height = 0.5*x^0.333*fabsf(sinf(15*log(sqrt(x+5)))); - static float prev_height = 0; - float average_height = 0.5f*prev_height + height1*0.5f; + const float weight = 0.7f; + float average_height = weight*m_previous_height + (1-weight)*height; + m_previous_height = height; +#if 0 printf("ball z %f dt %f x %f h %f, ah %f\n", next_xyz.getZ(),dt, x, height1, average_height); - prev_height = height1; - next_xyz.setY(getHoT() + average_height); #endif + next_xyz.setY(getHoT() + average_height); setXYZ(next_xyz); } // update diff --git a/src/items/rubber_ball.hpp b/src/items/rubber_ball.hpp index ed04456c8..b1bb8fbae 100644 --- a/src/items/rubber_ball.hpp +++ b/src/items/rubber_ball.hpp @@ -45,6 +45,13 @@ private: * point we aimed at and have to aim at the next point. */ float m_distance_along_track; + /** Since the distance between target and ball can vary a bit, + * using it to determine height on a formula is not smooth (e.g. + * ball can actually go up a bit during one frame while actually + * going down). Therefore we use a weighted average of previous + * height newly determined height. */ + float m_previous_height; + /** True if the ball just crossed the start line, i.e. its * distance changed from close to length of track in the * previous time step to a bit over zero now. */