#60780 - jandujar - Mon Nov 14, 2005 8:15 pm
Hello people,
I'm getting stark mad.
I don't know how to detect if I have an enemy in front of my player.
I move my player and I have his coordinates in (x,y), for example (x1,y1).
My player moves arround itself 360 degrees, and I have the point touched with stylus (x2,y2).
So my player points at the direction of the line
R = (x1,y1) + t(x2-x1,y2-y1)
The cpu's player has coordinates (a,b).
To know if my player are pointing the cpu, I calcule the distance between the cpu and the line and then I look if this distance is less than a value r(radius of the cpu's body) (i calcule the distance^2 to reduce calculations)
d(P,r)= |Ax+By+C|
---------
sqrt(A^2 + B^2)
Where A=1/(x2-x1)
B=-1/(y2-y1)
C=y1/(y2-y1) -x1/(x2,x1)
This is the code:
But this code make incorrects values.
What I'm making bad?
Note: Sorry for my english.
You can view a java applet to show you the distance between point and a line http://www.pntic.mec.es/Descartes/Bach_CNST_1/Geometria_afin_analitica_plano_lugares_geome[url]tricos/Geometria4.htm
Please, help me. I have 2 weeks working on this.
_________________
http://jandujar.homelinux.com
http://www.dsrobot.com
I'm getting stark mad.
I don't know how to detect if I have an enemy in front of my player.
I move my player and I have his coordinates in (x,y), for example (x1,y1).
My player moves arround itself 360 degrees, and I have the point touched with stylus (x2,y2).
So my player points at the direction of the line
R = (x1,y1) + t(x2-x1,y2-y1)
The cpu's player has coordinates (a,b).
To know if my player are pointing the cpu, I calcule the distance between the cpu and the line and then I look if this distance is less than a value r(radius of the cpu's body) (i calcule the distance^2 to reduce calculations)
d(P,r)= |Ax+By+C|
---------
sqrt(A^2 + B^2)
Where A=1/(x2-x1)
B=-1/(y2-y1)
C=y1/(y2-y1) -x1/(x2,x1)
This is the code:
Code: |
s32 CSDS_distancia_recta_punto(u8 x1,u8 y1,u8 x2,u8 y2,u8 a,u8 b){ s32 A,B,C,arriba,abajo,resultado; PA_OutputSimpleText(1,0,1," "); PA_OutputSimpleText(1,0,2," "); if(x2-x1!=0 && y2-y1!=0){ A=1/(x2-x1); B=-1/(y2-y1); C=(-y1*B) - (x1*A); arriba=(A*a)+(B*b) +(C); arriba=arriba^2; abajo=(A^2)+(B^2); resultado=arriba/abajo; PA_OutputText(1,0,1,"(%d,%d) (%d,%d) (%d,%d)",x1,y1,x2,y2,a,b); PA_OutputText(1,0,2,"A = %d, B= %d, C=%d, resultado=%d",A,B,C,resultado); if(resultado<0){PA_OutputText(1,0,2,"Es -"); return 80000;} return resultado; }else{ PA_OutputSimpleText(1,0,0,"es 0 "); return 8000; } } |
But this code make incorrects values.
What I'm making bad?
Note: Sorry for my english.
You can view a java applet to show you the distance between point and a line http://www.pntic.mec.es/Descartes/Bach_CNST_1/Geometria_afin_analitica_plano_lugares_geome[url]tricos/Geometria4.htm
Please, help me. I have 2 weeks working on this.
_________________
http://jandujar.homelinux.com
http://www.dsrobot.com