flash - How to use a parabola formula in AS3 for firing an arrow that will always intercept a given point -
first note: mathematically, i'm not skilled @ all.
i played game on iphone while press point, , arrow fires castle intersect point pressed. wanted make similar game, thinking easy quick make; ran realization mathematics beyond skill level.
i'm assuming they're using parabola formula or determine velocity , angle needed when arrow launched arrow intersect clicked point.
i vaguely remember how parabolas work school , have no chance of working out formulas.
any mathematical or ideas might easier implement great.
i want end function in castle so:
package { import avian.framework.objects.avelement; public class castle extends avelement { /** * fires arrow * @param ix x intersection point * @param iy y intersection point */ public function fire(ix:number, iy:number):void { var ar:arrow = new arrow(); ar.x = x; ar.y = y; // define angle , velocity based on ix, iy // ar.fireangle = ?? // ar.firevelocity = ?? parent.addchild(ar); } } }
update per questions in comments:
there no forces applied arrow such wind, friction, etc. also, starting point of arrow fixed throughout game (at castle).
here example image more clarity:
to clear possible:
- arrow begins journey fixed point (say: 40, 120).
- the arrow must intercept given coordinate.
- a realistic possible path i'd achieve (obviously can fire arrow straight intercept point, goal have arrow first rise, descend; passing through desired coordinate @ realistic point in journey).
note: avoid issue of there being infinite possible parabolas - velocity of arrow can fixed - @ defining angle arrow can leave at.
the flight path of projectile through gravitational field can described applying equations of motion
the equations use are
1. v = u + @ 2. s = ut + (at^2)/2
where
s = distance between initial , final positions
u = initial velocity
v = final velocity
= constant acceleration
t = time taken move initial state final state
ok. animate arrow calculate new velocity , position @ regular intervals (every frame) based on previous velocity, position , acceleration. acceleration in case entirely due gravity.
lets simplify , measure time intervals in frames rather seconds. gives t = 1 above equations allowing rewrite them as
1. v = u + a*1 => v = u + 2. s = u*1 + (a*1^2)/2 => s = u + a/2
now in x direction acceleration, = 0 (we're not taking drag account). in y direction = g, acceleration due gravity. if rewite these equations resolved each axis get
for x:
1. vx = ux + 0 => vx = ux (no change we'll ignore this) 2. sx = ux + 0/2 => sx = ux (convenient eh?)
for y:
1. vy = uy + g 2. sy = uy + g/2
so lets plug them sample script
public class arrow extends sprite { //g constant //it's closer 10 our world public static const g:number = 2; //our arrow private var arrow:shape; //start velocities private var ux:number; private var uy:number; public function arrow() { arrow = new shape(); arrow.graphics.linestyle( 1, 0 ); arrow.graphics.lineto( 30, 0 ); } public function fire( vx:number, vy:number ):void { ux = vx; uy = vy; addchild( arrow ); addeventlistener( event.enter_frame, fly ); } private function fly( e:event ):void { //lets use our equations var sx:number = ux; //distance moved in x dir var vy:number = uy + g //new velocity in y dir var sy:number = uy + g/2 //distance moved in y dir //apply arrow arrow.x += sx; arrow.y += sy; //save new y velocity uy = vy; //extra bonus rotation of arrow point in right direction arrow.rotation = math.atan2( uy, ux ) * 180 / math.pi; } }
Comments
Post a Comment