A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: Calculating a predicted hit

  1. #1
    FF6 Junky
    Join Date
    Jun 2008
    The Dragon's Den

    Unhappy Calculating a predicted hit

    Good afternoon all,
    I have a game that is in pseudo-isometric view, by which I mean that instead of actually using 3D equations, we are using 2D equations and just drawing everything at an angle. We have a cannon that you can tilt up and down using, appropriately enough, up and down keys. You cannot swivel the cannon, only tilt it, so that means that the line of sight of the cannon's aim will always be the same. The line of sight is represented by a thick black line extending from the cannon's base to the end of the map.
    Herein lies the problem. I am trying to predict where on the line the projectile will hit the line of sight. The projectile (press space to shoot) is a little light blue ball. The predicted point of impact is a larger blue dot. As the cannon is tilted, the predicted impact point changes (for now velocity does not change). My predicted point of impact is never on the line of sight and is never correct.
    The equation I am using comes from a physics problem I found in a book. The solution is here: http://www.atmosp.physics.utoronto.c...ics/ch3p43.pdf
    The difference between the problem and my example is that in my game, the projectile starts at an offset above the ground. I rewrote the equation with the projectile's origin included, but the math still is not working out. I think what's going wrong is my offsets from the bottom of the slope are throwing me off somehow

    Can somebody check and see if they can figure out where I'm going wrong? I think I need a fresh pair of eyes. Thanks!
    Here's the code for the phsyics part:

    function predictHit(vox, voy) 
    	//theta is the angle of the line of sight, and phi is the angle of the cannon
    	var thetarad = theta * Math.PI/180;
    	var phirad = phi * Math.PI/180;
    	/*var d = (2 * Math.pow(velocity, 2) * Math.cos(phirad) * Math.sin(phirad - thetarad))/
    			(g * Math.pow((Math.cos(thetarad)),2));
    			trace("d: " + d);*/
    	var ansArr = QuadSolver.quadEq((g * Math.pow((Math.cos(phirad)),2)/(Math.pow(2 * velocity * Math.cos(phirad), 2))),
    						Math.sin(phirad) - (Math.cos(phirad)*Math.tan(phirad)) +
    						(g * Math.cos(phirad) * cannonX2)/(Math.pow((velocity * Math.cos(phirad)), 2)*2),
    						Math.tan(phirad)*cannonX2 + cannonY2 - ((cannonX2*cannonX2)/Math.pow((velocity * Math.cos(phirad)), 2)));
    	var d = ansArr[0];
    	expectedHit[0] = xOffset - d * Math.cos(thetarad);
    	expectedHit[1] = yOffset - d * Math.sin(thetarad);
    	predHit.graphics.drawCircle(expectedHit[0], expectedHit[1], 4);
    Here's my quadratic equation solver (I confirmed already that it works correctly):

    	public class QuadSolver
    		public static function quadEq(a:Number, b:Number, c:Number):Array
    			var answer:Array = new Array;
    			answer[0] = (-b + Math.sqrt((b*b) - 4*a*c))/(2 *a);
    			answer[1] = (-b - Math.sqrt((b*b) - 4*a*c))/(2 *a);
    			return answer;
    I also included all of my code to this post.
    Attached Files Attached Files

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