To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 07-02-2007, 11:05 AM   #1
TOdorus
Funkalicious
 
TOdorus's Avatar
 
Join Date: Nov 2006
Location: Nijmegen, Netherlands
Posts: 599
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?
__________________
Blog: Vipe Games updated 3 dec 2009
TOdorus is offline   Reply With Quote
Old 07-02-2007, 01:10 PM   #2
TOdorus
Funkalicious
 
TOdorus's Avatar
 
Join Date: Nov 2006
Location: Nijmegen, Netherlands
Posts: 599
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)
}
};
__________________
Blog: Vipe Games updated 3 dec 2009
TOdorus is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:30 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.