3d Rigidbody Trajectory Prediction

Hello all,

I am attempting to produce a linerender model of a rigidbody’s trajectory using an initial force applied to a dragless rigidbody and it’s mass. However, the simulation wildly overestimates the distance travelled.

I begin by applying a force to a gameobject, ‘ball’ with a ‘forward force’ relative to me as follows:

 ball.rigidbody.AddForceAtPosition (kick_direction * forward_force, ball.transform.position);

To simulate the trajectory, I consider the following:

Velocity after one second of movement:

F=ma

a= F/m

therefore v1 = (F/m)*t = F/m

Distance travelled due to deceleration (see SUVAT equations):

s=u*t+0.5at^2

which results in the following code:

void Update(){
		LineRenderer line = GetComponent<LineRenderer> ();
		line.SetVertexCount (20);
		distance_to_ball = Vector3.Distance (ball.position, transform.position);
	//	Mathf.Clamp (forward_force, 100f, 700f);
	//	Mathf.Clamp (angle, 0f, 90f);
		if (Input.GetMouseButton (0)) {
			forward_force += 300f * Time.deltaTime;
			float ball_mass = ball.rigidbody.mass;
			float ball_drag = ball.rigidbody.drag;
			float u_x =(kick_direction.x*forward_force)/ball_mass;
			float u_y =(kick_direction.y*forward_force)/ball_mass;
			float u_z =(kick_direction.z*forward_force)/ball_mass;
				for(int i=0; i<20; i++){
				float x_pos = u_x*(i+1)- 0.5f*(ball_drag*u_x)*Mathf.Pow ((i+1),2);
				float y_pos =u_y*(i+1)+0.5f*(Physics.gravity.y-ball_drag*u_y)*Mathf.Pow ((i+1),2);
				float z_pos = u_z*(i+1)-0.5f*(ball_drag*u_z)*Mathf.Pow ((i+1),2);	

				line.SetPosition(i, new Vector3(x_pos, y_pos, z_pos));
				}

			}

	}

I have included what I believe to be the correct deceleration due to drag, but this is irrelevant as the object i am considering has 0 drag.

Any help would be much appreciated!

Cheers,

Jack

when working with physic use vector instead of float there is already plenty of method and operator overload for you . Then you can simply * two vectors or call normalize() or simply calculate the sum of forces with + .

You should end up with something like that

 for( .. i ...)
{
    Vector3 f_initial
    Vector3 f_current = CalculateInstantanateForce(f_initial , time);
    AddForce (f_current , i);

Vector3 CalculateInstantanateForce(Vector3 initial , float time)
{
   return (/*to do some calculus*/)
}
void AddForce(Vector3 force , int index)
{
   vertexArray.Insert(index , force);
    

}

void Render()
{
   foreach(.. vertexArray.Count ...)
   {
       //to do
    }
}

This is simply a mock-up …