Trigonometry

Probably something that has confused many people before me but…

i think the hitTestObject in Flash isn’t really good enough but what is the alternative?

I’ve experimented with using this:

distance=Math.round(Math.sqrt(xCoOrdsxCoOrds+yCoOrdsyCoOrds));

which is able to tell me if there are less than a certain amount of pixels between two clips meaning a hit will probably occur but what i’m still yet to work out is how are you able to tell where the hit occurred, i’m trying to make a pinball type game and would like the ball to have just one ENTER_FRAME and off this be able to determine when it’s hit an object of any shape and when it has hit the ‘walls’ of my stage, in addition to this i need to work out at what angle the hit occurred so that my pinball can bounce off the object at a reflective angle, it also needs to be able to work out how far away from any given object it is not just one other object which is in my example.

source attached and full code below:

var tfDistance:TextField = new TextField();
addChild(tfDistance);
tfDistance.autoSize=“left”;
tfDistance.x=10;
tfDistance.y=10;

var tfHitStatus:TextField = new TextField();
addChild(tfHitStatus);
tfHitStatus.autoSize=“left”;
tfHitStatus.x=10;
tfHitStatus.y=25;

var tfHitAngle:TextField = new TextField();
addChild(tfHitAngle);
tfHitAngle.autoSize=“left”;
tfHitAngle.x=10;
tfHitAngle.y=40;

var pinball:aObj = new aObj();
addChild(pinball);
pinball.x=180;
pinball.y=150;

var clipB:bObj = new bObj();
addChild(clipB);
clipB.x=180;
clipB.y=90;

var aY:Number;
var aX:Number;
var bY:Number;
var bX:Number;

var xCoOrds:Number;
var yCoOrds:Number;

var minDist:Number=pinball.width;

var distance:Number;
var hitStatus:Boolean;
var hitAngle:Number;

var xSpeed:Number=10;
var ySpeed:Number=10;

var gravity:Number=0.9;
var drag:Number=0.98;
var rightEdge:Number=300;
var leftEdge:Number=0;
var topEdge:Number=0;
var bottomEdge:Number=250;

addEventListener(Event.ENTER_FRAME, checkBallPos);

function checkBallPos(e:Event):void {

pinball.x=pinball.x+xSpeed;
pinball.y=pinball.y+ySpeed;

//ySpeed=ySpeed*drag+gravity;
//xSpeed=xSpeed*drag;

function rightHit() {
	xSpeed*=-1;
}
function leftHit() {
	xSpeed*=-1;
}
function bottomHit() {
	ySpeed*=-1;
}
function topHit() {
	ySpeed*=-1;
}

if (pinball.x+pinball.width/2>rightEdge) {
	pinball.x=rightEdge-pinball.width/2;
	rightHit();
}
if (pinball.x-pinball.width/2<leftEdge) {
	pinball.x=leftEdge+pinball.width/2;
	leftHit();
}
if (pinball.y+pinball.height/2>bottomEdge) {
	pinball.y=bottomEdge-pinball.height/2;
	bottomHit();
}
if (pinball.y-pinball.height/2<topEdge) {
	pinball.y=topEdge+pinball.height/2;
	topHit();
}


aY=pinball.y;
aX=pinball.x;
bY=clipB.y;
bX=clipB.x;

xCoOrds=aX-bX;
yCoOrds=aY-bY;

distance=Math.round(Math.sqrt(xCoOrds*xCoOrds+yCoOrds*yCoOrds));

if (distance<minDist) {
	hitStatus=true;
} else {
	hitStatus=false;
}

hitAngle=Math.round(Math.atan2(aY-bY,aX-bX)*180/Math.PI);
hitAngle+=180;

if (hitAngle>270&&hitAngle<360&&hitStatus==true) {
	topHit();
}
if (hitAngle>0&&hitAngle<90&&hitStatus==true) {
	bottomHit();
}
if (hitAngle>90&&hitAngle<180&&hitStatus==true) {
	leftHit();
}
if (hitAngle>180&&hitAngle<270&&hitStatus==true) {
	leftHit();
}


tfDistance.text="distance between clips = "+distance;
tfHitStatus.text="hit status = "+hitStatus;
tfHitAngle.text="hit angle = "+hitAngle;

}