I am working on a 2d side-view soccer game that looks like this image:
But now I stuck on how to implement parabola pass ball. I have found many questions corresponding to my problem:
- Calculating velocity needed to hit target in parabolic arc
- Throw object towards predefined place?
- http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
But they all just tell how to draw parabola in simple 2d space. In my game, except for the normal x,y
there is also a virtual height coordinate, so the final viewing position of the ball is:
world position + current_height * Calc_Scale
. (Calc_Scale is 0.3f)
Currently I did it the following way: I calculate the pass time(T) and distance(D) from point A to B
the pct = current_time / T
current_height = D * (4* pct - 4 * pct* pct)
Unfortunately, it looks bad...
UPDATE: @ashes999 I tried quadratic equation like current_height = D * (4* pct - 4 * pct* pct)
, it is not good(I am continuing to try different factors).I am pretty sure sin/cos can not work here, for example, player A is in(300, 200), B is(300, 800), the ball will make a direct line beyond B's head then drop to B's feet(or head), and this can not be done by cos/sin way, And I am thinking can this be done by some sort of projection, I just calculate the x,y,z then project z value into y axis?
void RSSoccerBall::Pass(const vector2d& target, double force)
{
vector2d DistVec(target - m_vPosition);
m_PassDistance = DistVec.getLength();
if(m_PassDistance > MaxShortPassDistance)
{
m_CurrentPassTime = 0.f;
m_PassTime = TimeToCoverDistance(target, m_vPosition, force, true);
}
}
void RSSoccerBall::update(float dt)
{
//for parabola calc
if(m_CurrentPassTime < m_PassTime)
{
m_CurrentPassTime += dt;
float pct = m_CurrentPassTime / m_PassTime;
m_Height = m_PassDistance * 0.7 * (4 * pct - 4 * pct * pct);
}
vector2d exactViewPos = m_vPosition; //m_vPosition is the 2d world position
exactViewPos.y += m_Height;
//render football at exactViewPos;
...
}
@Mario, These are my code ,I have used an extra variable to store height. - Determing an object's position along a curve over time this is where the equation from
I do not want the top of the arc always to be at 50% distance traveled, because player A and B probably not in same Y value, if B's Y value is higher than A's Y, the top of the arc will be exceed 50%. I found a wiki link: - http://en.wikipedia.org/wiki/Range_of_a_projectile describe Ideal projectile motion on uneven ground, but the formulas are complicated for me to convert into current situation(we have start point, end point, start velocity, travel time)
Can anyone give me some pointers how to accomplish that?
y=x^2 ...
) orsin
/cos
) to make it work. \$\endgroup\$ – ashes999 Aug 26 '13 at 8:50