Welcome to Dream.In.Code
Getting Help is Easy!

Join 105,415 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,865 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Projectile Leading

 
Reply to this topicStart new topic

Projectile Leading, Derived correctly?

Einherjar
post 2 Jul, 2008 - 09:43 PM
Post #1


D.I.C Head

**
Joined: 10 Feb, 2008
Posts: 73


My Contributions


Unfortunately I don't have access to a compiler and I need to see if I've derived the correct equation for projectile leading. This assumes I need to find out the time as well as the direction of the shot given Spos(shot initial position - a vector), Sspeed(speed of the shot - float), Tpos(target initial position), Tvel(velocity vector for target or speed + direction). I guess where I'm fuzzy is where I invert the quadratic equation because technically u = 1/t. Here is what I have done.

We need to use two equations:
(I)Spos + tSdir = Tpos + tTvel
(II)Sdirdot(Sdir) = Sspeed^2

Divide (I) by t solve for Sdir:
Spos/t + V = Tpos/t + Tvel
Sdir = (Tpos – Spos)/t + Tvel = Du + Tvel where D = Tpos – Spos and u = 1/t

using equation (II):
Sspeed^2 = Sdirdot(Sdir) = (Ddot(D))u^2 + 2u(Ddot(Tvel)) + Tveldot(Tvel)
u^2(Ddot(D)) + 2u(Ddot(Tvel)) + Tveldot(Tvel) - Sspeed^2 = 0

using quadratic equation: A = Ddot(D) B = 2Ddot(Tvel) C = Tveldot(Tvel) - Sspeed^2
u = (-2Ddot(Tvel) + sqrt((2Ddot(Tvel))^2 – 4(Ddot(D))(Tveldot(Tvel) - Sspeed^2)) / 2Ddot(d)
so t = the inverse of the above equation // should I be inverting here?

And Sdir = (Tpos – Spos) / t + Tvel

Finally in code:
CODE

Vector LeadTarget(Vector Spos, float Sspeed, Vector Tpos, Vector Tvel)
{
    Vector D = Tpos - Spos;
    float A = D.dot(D);
    float B = 2 * D.dot(Tvel);
    float C = Tvel.dot(Tvel) - Sspeed * Sspeed;
    float t = (A * 2) / (-B + sqrt(B * B + 4 * A * C)); // this is where I invert but am unsure if I should.

    return (D/t + Tvel).Normal();
}
User is offlineProfile CardPM

Go to the top of the page


Einherjar
post 3 Jul, 2008 - 02:23 PM
Post #2


D.I.C Head

**
Joined: 10 Feb, 2008
Posts: 73


My Contributions


So no one knows? sad.gif Come on guys!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/20/08 06:00AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month