A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Maths + lack of sleep = Need help please

  1. #1
    Mr. GT4
    Join Date
    Jul 2008
    Location
    Seriously in the hills of Donegal, Ireland
    Posts
    3

    Question Maths + lack of sleep = Need help please

    I've been sitting here for hours wasting time and watching my IQ pack up its bags to leave...
    My last resolve is you guys before something gets thrown
    I just quickly registered and did a quick search first of all before posting, if I have missed a similar post my apology. On to the problem.

    What I want to do is have say a turret fire a bullet at a constant velocity towards a moving object which say is moving around the screen. Now if the turret fires a bullet it will likely miss due to the fact that the moving object has moved from the position it was aiming at.
    What I would want the turret to do is take the objects vector and position and use that to calculate the angle that it should fire at inorder to hit the object.

    I have attempted this but as usual my math skills have failed me. I have no idea now if this is the wright way to be going about the code, so if you could help please I would very much appreciate it.

    Code:
    	// Calculate the distance to the moving object 'mover'.
    	var dx = mover._x - 275;
    	var dy = mover._y - 200;
    	var dist = Math.sqrt(dx*dx+dy*dy);
    	
    	// The time it may take to reach the 'mover'.
    	var timeToTarget = (dist/shotSpeed);
    	
    	// Predicted position of the 'mover'.
    	var cx = mover._x + (mover.vector.x*timeToTarget);
    	var cy = mover._y + (mover.vector.y*timeToTarget);	
    	
    	// Predicted angle.
    	var predictAngle = Math.atan2((cy-200),(cx-275));

  2. #2
    Likes Pankakes
    Join Date
    Mar 2008
    Location
    Germany
    Posts
    59
    might want to show us your program...right now what you calculated only works if it's like this:

  3. #3
    Mr. GT4
    Join Date
    Jul 2008
    Location
    Seriously in the hills of Donegal, Ireland
    Posts
    3

    Thats the right direction

    The program is not anything different from what you have drawn.
    I've added a green circle to the image in the attachment and its direction to the predicted point. What I am trying to do is have the green circle reach the predicted point that the blue circle will be at.
    Doing that I would work out how much more of an angle would it take to hit the blue circle if the green circle was moving at a constant velocity.
    Does that make it any clearer? I can upload a quick swf if that will help?
    Attached Images Attached Images

  4. #4
    Likes Pankakes
    Join Date
    Mar 2008
    Location
    Germany
    Posts
    59
    I'm not really sure if I know how to help you, but since there's no use in sleeping right now for me anymore, I thought I'd give it a shot...here's what I came up with:



    I could be wrong though since I'm strung out on nicotine and coffee :P

    If you're looking for the angle between the vertical line going through the projectile (which I forgot to put in there) you're firing and alpha, then it's just 90degrees - alpha
    Last edited by Articulate-Dee; 07-04-2008 at 07:49 AM.

  5. #5
    Likes Pankakes
    Join Date
    Mar 2008
    Location
    Germany
    Posts
    59
    oh damn it should be x2/x1 not the other way around...so in the end you get arccos(v2 / v1)

    Last edited by Articulate-Dee; 07-04-2008 at 08:54 AM.

  6. #6
    Senior Member rachil0's Avatar
    Join Date
    Jul 2007
    Location
    Columbus, OH.
    Posts
    465
    This thread might be of some use.

    http://board.flashkit.com/board/showthread.php?t=754442

    There's lots of different approaches in there, I think the one by "rje" was what the OP eventually settled on.

    The approach I supplied in there, post #20, solves a tougher problem than what you need to solve but a lot of the discussion around it is relevant. It solves the "target moves w/ constant acceleration" case, but it appears you need to solve the "target moves w/ constant velocity, acceleration==0" case.

    Yours is a simpler problem - you only need to solve a quadratic equation in t.

    Take the quartic posted there, and set ax=ay=0. This:

    Code:
    (1/4*ax^2+1/4*ay^2)*t^4+(vy*ay+vx*ax)*t^3+(py*ay+vy^2+px*ax+vx^2-V^2)*t^2+(2*py*vy+2*px*vx)*t+px^2+py^2 = 0
    Becomes that:

    Code:
    (vy^2+vx^2-V^2)*t^2+(2*py*vy+2*px*vx)*t+px^2+py^2 = 0
    Solve it for t using the quadratic formula (2 possible values). Then, go back to that other thread, and see how you compute the firing angle from t.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center