Calculate force needed to reach certain point (AddForce)
Hello everyone, So I have the following system setup for an arrow shooting game:
A force (from 0 to a certain maximum value) is chosen by the player. Say force is F.
The angle (from 0 to 90) is also chosen by the player. Say degrees are D.
Afterwards, having the force and degrees, AddForce is used. Two forces are applied - one for X, and one for Y. F_Y is calculated like this: F * (D / 90f), and F_X = F - F_Y. Thus the force added is AddForce(F_X, F_Y)
The Arrow is fired. Gravity value is default. Arrows may have different weights (rigidbody). Distance between the starting position and end position are given.
I need a way to figure out the total force or just one of the forces needed to reach a SPECIFIC point (X and Y, Z axis is not used at all). I am aware that there are multiple X & Y possibilities, however, is there a way to know at least one of the forces (or the total) having only the following variables? (I need to find out this value in a script for different degree, distance and arrow weight values)
Thank you!
Answer by sysmaya · Jul 04, 2017 at 11:12 PM
This function calculate the Vector3 and speed for parabolic shoots. EJ: Shoot Bows, Shoot canons, etc.
Vector3 calcBallisticVelocityVector(Vector3 source, Vector3 target, float angle){
Vector3 direction = target - source;
float h = direction.y;
direction.y = 0;
float distance = direction.magnitude;
float a = angle * Mathf.Deg2Rad;
direction.y = distance * Mathf.Tan(a);
distance += h/Mathf.Tan(a);
// calculate velocity
float velocity = Mathf.Sqrt(distance * Physics.gravity.magnitude / Mathf.Sin(2*a));
return velocity * direction.normalized;
}
Hello, Sorry to Necro the thread. Does this factor in the mass of the object being launched? Or is it assumed the mass is 1?
Thanks, this helped me get the correct bounce direction of a ball to reach a point.
Hey, any idea how to calculate this considering the drag of the rigidbody?
Can you explain how the math works here? Why is direction.y distance * Tan(a)? why is h/Tan(a) added to distance? Basically the stuff from that point would be great if you could explain how it works.