PDA

View Full Version : Link, and Collision checking using angles.



Gleeok
03-02-2008, 08:31 AM
I need some help collision checking if Link is within the path of an angle...or put in other words; If one were to use a Screen drawing function to form a line from the top-right to the bottom-left of the screen, How could we check if Link is within this scope?

To make this more interesting, there are a few of these at work all rotating clockwise. To simplify this, I assumed that each one rotates from its center at 138,80, and a degree is incremented each frame. (n)

So far I cannot match the angle at all, and my script seems random. This is the best I got:



int lx = Link->X;
int ly = Link->Y;

for(int i=8;i<138;i+=8){

if(black){
int radx = 138 + (i * Sin(n));
int rady = r + (i * Cos(n));
if(Abs(lx-radx)<10&&Abs(ly-rady)<10){Link->HP--;Game->PlaySound(11);}
}
else if(white){
int radx = 138 + (i * Sin(n+90));
int rady = r + (i * Cos(n+90));
if(Abs(lx-radx)<10&&Abs(ly-rady)<10){Link->HP--;Game->PlaySound(11);}
}

}



Hooboy...

C-Dawg
03-02-2008, 01:35 PM
I was going to get around to this eventually. One of my bosses uses a pinpoint lazer that sweeps along the ground. It uses a Draw Line function. Right now, it doesn't damage the player.

What you want is for the code to check if the player's (x,y) coordinates are within 8 pixels or so of the damaging line. There's a formula that lets you determine the distance between a point and a line.

http://www.mathwords.com/d/distance_point_to_line.htm

Basically, the distance between (x,y) and line Ax + By + C = 0 is:

(Ax + By + C)/sqrt(A^2 + B^2)

Finding (x,y) is easy, but getting your line equation into Ax+By+C is trickier. It's just another way of writing the usual equation for a line, y = mx + b.

If you're using Line Draw function, you have to give the game two points to draw a line between (x1, y1) and (x2, y2). So getting the slope, m, is easy.

Slope is just rise over run, or :
m = (y1 - y2)/(x1 - x2).

Now you can plug in one of your points and solve for b.

y1 = x1((y1-y2)/(x1-x2)) + b
b = y1 - x1((y1-y2)/(x1-x2))

Now you've got m and b. So you have the equation:
y = mx + b.

Just convert into the form:

mx - y + b = 0

Which gives you your values for:

Ax + By + C = 0

That is:

m = A
-1 = B
b = C

So plug these numbers into the distance formula at top and you're good to go. Got all that?

Also, I think std.zh might have some functions that short cut alot of this math for you, but what fun is that?

Gleeok
03-02-2008, 02:35 PM
Got all that?

Uuuuuuuuuhhh.....shitsauce. Um, did I mention it rotates? I 'm not really sure x or y, only the angle of degree of rotation and rx,ry of Rectangle. I figured you could somehow check to see if Link is along the same degree line using a for loop...I don't know, Is all that really necessary?



Also, I think std.zh might have some functions that short cut alot of this math for you, but what fun is that?
less unfun for sure. :p

You mean this:

int Distance(int x1, int y1, int x2, int y2) {
return Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}



edit; mx? where do you find mx?

Gleeok
03-05-2008, 01:29 PM
I get that you need to find the closest point from the line to Link and check collision from there, but I don't really understand the method to get those numbers. Anyway I've fixed the degree based hit detection loop. It works! ..But for some reason I had to reverse all the angles for it to work...odd. I was googling a bit and as far as I know it might be the first time collision checking has been done like this. I'm like a Pioneer or something..



for(int i=8;i<144;i+=8){

radx = 138 + (i * Sin(n-90));
rady = r + (i * Cos(n+90));
if(Abs(lx-radx)<9&&Abs(ly-rady)<9){
if(white){
if(Link->MP<Link->MaxMP)Link->MP++;
Game->PlaySound(23);
}
else {Link->HP--;Game->PlaySound(19);}
}
radx = 138 - (i * Sin(n-90));
rady = r - (i * Cos(n+90));
if(Abs(lx-radx)<9&&Abs(ly-rady)<9){
if(white){
if(Link->MP<Link->MaxMP)Link->MP++;
Game->PlaySound(23);
}
else {Link->HP--;Game->PlaySound(19);}
}