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