- Home /
Calculate Projectille impact point
Hey all!
So I'm here attempting to so a function that calculates the most probable impact point between the projectile and the moving object. However, when I run it, the projectille appears to choose a random direction instead of the desired point, and it doesn't even gets close to the target.
Here's the function:
///<summary>This function calculates the most likelly colision between two objects a (target) and b (projectille)</summary>
///<param name="Pa">Vector3 with the position of object a</param>
///<param name="Va">Vector3 with the velocity of object a</param>
///<param name="Pb">Vector3 with the position of object b</param>
///<param name="Vb">Vector3 with the velocity of object b</param>
///<returns>Most likelly intersect point</returns>
function calculateIntersectPoint(Pa : Vector3, Va : Vector3, Pb : Vector3, Vb : Vector3) : Vector3 {
var t : float = calculateIntersectTime(Pa, Va, Pb, Vb);
return calculatePosition(Pa, Va, t);
}
function calculatePosition(p : Vector3, v : Vector3, t : float) : Vector3 {
return p + (v*t);
}
///<summary>This function calculates the intersect time</summary>
///<param name="Pa">Vector3 with the position of object a</param>
///<param name="Va">Vector3 with the velocity of object a</param>
///<param name="Pb">Vector3 with the position of object b</param>
///<param name="Vb">Vector3 with the velocity of object b</param>
///<returns>Most likelly intersect point</returns>
function calculateIntersectTime(Pa : Vector3, Va : Vector3, Pb : Vector3, Vb : Vector3) {
var Pab : Vector3 = Pa - Pb;
var Vab : Vector3 = Va - Vb;
var a : float = Vector3.Dot(Vab, Vab);
var b : float = Vector3.Dot(Pab, Pab);
var c : float = b;
var discriminant : float = calculateDiscriminant(a,b,c);
var t : float;
if(discriminant <= 0) {
t = calculateSinglePoint(a, b);
} else {
t = calculateTwoPoints(a, b, discriminant);
}
if(t < 0) {
t = 0;
}
return t;
}
///<summary>Calculates de discriminant of a quadratic function</summary>
function calculateDiscriminant(a : float, b : float, c : float) : float {
return b * b - 4 * a * c;
}
function calculateSinglePoint(a : float, b : float) : float {
return -(b/(2*a));
}
function calculateTwoPoints(a : float, b : float, discriminant : float) : float {
var t0 : float = (-b + Mathf.Sqrt(discriminant))/(2*a);
var t1 : float = (-b - Mathf.Sqrt(discriminant))/(2*a);
return Mathf.Min(t0, t1);
}
And here's how it's called:
function OnEnable() {
var audio : AudioSource = gameObject.GetComponent(AudioSource);
audio.Play();
rigidbody.velocity = status.speed * transform.forward;
if(target)
{
var trans : Transform = target.transform;
var Pa : Vector3 = trans.position;
var Va : Vector3 = trans.rigidbody.velocity;
var Pb : Vector3 = transform.position;
var Vb : Vector3 = rigidbody.velocity;
var intersect : Vector3 = calculateIntersectPoint(Pa, Va, Pb, Vb);
transform.LookAt(intersect);
}
launched = Time.time;
effect.setExploded(false);
}
So, does anyone knows why this is happening? If so, what can I change in order to make it work? Thanks in advance!
João Borrego
Just a quick question, is this supposed to be in a 3d world or 2d? I see vector3, but we never know...
also, is it that much of a problem to change the bullet's orientation every frame ?
3D world. The point here is that the "bullet" direction only gets setup at the start, so if the target changes direction, the bullet will miss. Also there might be situations where 500 projectiles are flying around and changing the orientation every frame might kill a bit of performance...
Answer by jokim · Sep 04, 2014 at 07:46 PM
Aight, After searching the net for a week, I finally found a satisfying (and understandable - or at least well explained) answer!
Here : http://danikgames.com/blog/?p=52
This guy actually illustrates what he does, it's a very good starting point.
Now, at the end of his blog post, he actually links to a Unity3d version of it, Here
Answer by LSPressWorks · Aug 26, 2014 at 04:05 PM
I just found this while trying to find more info on my issue, seems like it might be helpful.
http://forum.unity3d.com/threads/calculating-impact-velocity-on-collision.35909/
Your answer
Follow this Question
Related Questions
how to make bullet ascend/descend relative to uneven ground level? 2 Answers
Throwing a Spear 1 Answer
Network physics problem in client side,collision problem over network 0 Answers
Jump with physics 1 Answer
How to make my Character run through walls with the use of the Character Controller component 1 Answer