|
-
Funkalicious
Circle-Line intercept
I'm developing a little game, in which I was planning to use circle-shaped enemies to shoot at. Instead of using the standard hittest, I wanted to calculate if the line (the players shot) would intersect with a circle (enemy).
After finding a method to achieve this, I tried to implement it in my code. This is giving me a headache, as it almost always gives a hit
Method
Implementation:
Enemy.prototype.globalHit = function() {
this.HitPos.x = this.Graph.Holder.Circle._x
this.HitPos.y = this.Graph.Holder.Circle._y
this.Graph.Holder.localToGlobal(this.HitPos);
};
Enemy.prototype.HitTest = function(line) {
this.globalHit();
//
var m:Number = line[0];
var b:Number = line[1];
var k:Number = this.HitPos.x;
var h:Number = this.HitPos.y;
var r:Number = 21
//
var A:Number = 1-2*k+(m*m)
var B:Number = 2*b*m-2*h*m
var C:Number = k+(b*b)-2*h*b+(h*h)-(r*r)
//
var D:Number = (B*B)-4*A*C
//
if (D>0){
return (true);
} else {
return (false);
}
};
I've checked all the stating valeus (m,b,k,h and r) and they're all correct, so there mus be a mistake in the equations. Can anybody help?
-
Funkalicious
Nevermind:
Method2
Enemy.prototype.HitTest = function(line) {
this.globalHit();
//
var m:Number = line[0];
var b:Number = line[1];
var k:Number = this.HitPos.x;
var h:Number = this.HitPos.y;
var r:Number = this.R
//
var b2 = h-(m*k)
//
var m3 = -1/m
var b3 = h-(m3*k)
//
var X2 = Intersect([m,b], [m3,b3])
var Y2 = m3*X2+b3
//
dX = X2-k
dY = Y2-h
//
var dist = Math.sqrt((dX*dX)+(dY*dY))
if (dist < r){
return (true)
}else{
return (false)
}
};
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|