- Home /
Trajectory prediction with linear drag
Hi, Ive been trying to make "golf" kind of game, where you throw a ball angry birds style. I needed soemthing to predict and draw trajectory, I looked on the internet and found what I was looking for. It works perfectly when linear drag is set to 0, but now i need it to predict correct trajectory when I change the drag.
void OnCollisionStay2D(Collision2D collision)
{
Debug.Log( "Is on ground" );
rigidbody2d.drag = 2f;
}
void OnCollisionExit2D(Collision2D collision)
{
Debug.Log( "Not on ground" );
rigidbody2d.drag = 0.4f;
}
Vector2 CalculatePosition(float elapsedTime)
{
return ((Physics2D.gravity * rigidbody2d.gravityScale)) * elapsedTime * elapsedTime * 0.5f + LaunchVelocity * elapsedTime + InitialPosition;
}
Now, when the ball is touching the ground, it has linear drag set to 2, when it leaves the ground it has linear drag set to 0.4. That is because I need the ball to slow down while rolling on the ground obviously and to "simulate" air friction. I could be satisfied for now with linear drag be set to 0 when in the air, but for a tiny moment, when I throw the ball, it has drag set to 2, because it is touching the ground and trajectory prediction is off as well.
Do you have any ideas how to include the drag into the equation please? Or, maybe, figure out some other way to slow down the ball when it is touching the ground? Friction isnt working.
And I work in 2D.
Thanks for every answer :)
Answer by Bakugan · Feb 12, 2020 at 02:33 PM
Ok, so after days of looking on the internet, I finally found something that works. Kind of.
The drag is now taken into account and the prediction is now more or less accurate, maybe like 90 %. But only for a small drag values, like 0 to 1, which is fine for air drag. And fine for me.
For all who seek to predict the future: I give thee the source code.link text