c# - How to find an intercept on a moving target -


so here code

distance = vector3.distance(transform.position,target.transform.position); float timetotarget;  float burndistance =  projectile.meterspersecond * 2f * 2f * 0.5f;  if (distance < burndistance) {     timetotarget = mathf.sqrt(2 * distance / projectile.meterspersecond); } else {     float velocity = projectile.meterspersecond * 2;     timetotarget = 2 + (distance - burndistance) / velocity; } aimpoint = target.transform.position + (target.transform.rigidbody.velocity * timetotarget) + (random.insideunitsphere * accuracy); distance = vector3.distance(transform.position,aimpoint); timetotarget = mathf.sqrt(2 * distance / projectile.meterspersecond); 

i'm trying intercept target projectile.

the issue finding proper time target think.

basically when find distance target use find time target changes aimpoint target in time seconds. distance has changed. i'm longer distance target away i'm distance aimpoint away want hit.

basically target moves away me takes longer projectile hit aimpoint predicted distance target. can fix running algorithm again. using aimpoint target , getting closer approximation. keep doing close seems highly ineffiecient. there better way?

if know velocity vector v of target, can work out velocity of missile, , time intercept this:

suppose @ p , target @ q. missile launched p velocity u hit target @ time t must have

p + t*u = q + t*v 

so

u = v + (q-p)/t 

suppose missile speed -- length of u -- fixed @ s so

s*s = v.v + 2*(q-p).v/t + (q-p).(q-p)/(t*t) 

or, rearranging)

(s*s - v.v)*t*t - 2*(q-p).v*t - (q.p).(q-p) = 0 

which quadratic equation t. if there positive solution t0 this, can launch missile, velocity u given

u = v + (q-p)/t0 

to hit target @ time t0


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -