(20111222) 20:00
This commit is contained in:
61
Util.c
61
Util.c
@@ -30,7 +30,6 @@ int SolveQuadratic(float a,float b,float c,float *Rmin,float *Rmax){
|
||||
return(1);
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Intersec_RayUnitCircle
|
||||
//
|
||||
@@ -38,16 +37,21 @@ int SolveQuadratic(float a,float b,float c,float *Rmin,float *Rmax){
|
||||
int Intersec_RayUnitCircle(vec2 orig,vec2 vel,vec2 center,float *t){
|
||||
float a,b,c;
|
||||
float Rmin,Rmax;
|
||||
vec2 temp;
|
||||
vec2 distv;
|
||||
float qlvel;
|
||||
float qdistv;
|
||||
|
||||
// Check if the collision is even posible
|
||||
qlvel=vec2_dot(vel,vel);
|
||||
if(fabs(qlvel)<=0.0f)
|
||||
return(0);
|
||||
vec2_minus(distv,orig,center);
|
||||
qdistv=vec2_dot(distv,distv);
|
||||
|
||||
// Solve as a unit circle
|
||||
a=vec2_dot(vel,vel);
|
||||
if(fabs(a)<=0.0f){
|
||||
return(0);
|
||||
}
|
||||
vec2_minus(temp,orig,center);
|
||||
b=2.0f*vec2_dot(temp,vel);
|
||||
c=vec2_dot(temp,temp)-1.0f;
|
||||
a=qlvel;
|
||||
b=2.0f*vec2_dot(distv,vel);
|
||||
c=qdistv-1.0f;
|
||||
if(SolveQuadratic(a,b,c,&Rmin,&Rmax)){
|
||||
if(Rmin>=-0.0f && Rmin<Rmax && Rmin<=1.0f){
|
||||
*t=Rmin;
|
||||
@@ -67,25 +71,46 @@ int Intersec_RayUnitCircle(vec2 orig,vec2 vel,vec2 center,float *t){
|
||||
//
|
||||
// Colision point of a circle against another circle.
|
||||
int Colision_CircleCircle(
|
||||
vec2 cir1[2],float rad1,
|
||||
vec2 cir1,float rad1,vec2 vel,
|
||||
vec2 cir2,float rad2,
|
||||
float *t,vec2 n)
|
||||
{
|
||||
vec2 vel,orig,cen,temp;
|
||||
vec2 vel_a,orig_a,cen_a,temp;
|
||||
float rads,invrads;
|
||||
float maxx,minx;
|
||||
float maxy,miny;
|
||||
|
||||
// Check if the collision is even posible
|
||||
rads=rad1+rad2;
|
||||
minx=cir1[0]-rads;
|
||||
maxx=cir1[0]+rads;
|
||||
if(vel[0]>0){
|
||||
maxx+=vel[0];
|
||||
}else{
|
||||
minx+=vel[0];
|
||||
}
|
||||
if(cir2[0]<minx || cir2[0]>maxx)
|
||||
return(0);
|
||||
miny=cir1[1]-rads;
|
||||
maxy=cir1[1]+rads;
|
||||
if(vel[1]>0){
|
||||
maxy+=vel[1];
|
||||
}else{
|
||||
miny+=vel[1];
|
||||
}
|
||||
if(cir2[1]<miny || cir2[1]>maxy)
|
||||
return(0);
|
||||
|
||||
// Convert to a unit circle vs ray
|
||||
rads=rad1+rad2;
|
||||
invrads=1.0f/rads;
|
||||
vec2_minus(vel,cir1[1],cir1[0]);
|
||||
vec2_scale(vel,vel,invrads);
|
||||
vec2_scale(orig,cir1[0],invrads);
|
||||
vec2_scale(cen,cir2,invrads);
|
||||
if(Intersec_RayUnitCircle(orig,vel,cen,t)){
|
||||
vec2_scale(vel_a,vel,invrads);
|
||||
vec2_scale(orig_a,cir1,invrads);
|
||||
vec2_scale(cen_a,cir2,invrads);
|
||||
if(Intersec_RayUnitCircle(orig_a,vel_a,cen_a,t)){
|
||||
// Calculate n
|
||||
vec2_minus(vel,cir1[1],cir1[0]);
|
||||
vec2_scale(temp,vel,*t);
|
||||
vec2_plus(temp,cir1[0],temp);
|
||||
vec2_plus(temp,cir1,temp);
|
||||
vec2_minus(temp,cir2,temp);
|
||||
vec2_scale(n,temp,1.0f/rads);
|
||||
return(1);
|
||||
|
||||
Reference in New Issue
Block a user