A Flash Developer Resource Site

# Thread: Calculating a predicted hit

1. ## 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:

Code:
```function predictHit(vox, voy)
{
predHit.graphics.clear();
//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;
trace("d: " + d);*/
var d = ansArr[0];
expectedHit[0] = xOffset - d * Math.cos(thetarad);
expectedHit[1] = yOffset - d * Math.sin(thetarad);
predHit.graphics.beginFill(0x00000FF);
predHit.graphics.drawCircle(expectedHit[0], expectedHit[1], 4);
predHit.graphics.endFill();
trace(expectedHit);
}```
Here's my quadratic equation solver (I confirmed already that it works correctly):

Code:
```package
{
{
public static function quadEq(a:Number, b:Number, c:Number):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);

}

}
}```
I also included all of my code to this post.

#### Posting Permissions

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

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap