Author Topic: May be jpct would add AABBIntersectsAABB  (Read 865 times)

Offline Uncle Ray

  • int
  • **
  • Posts: 81
    • View Profile
May be jpct would add AABBIntersectsAABB
« on: January 27, 2015, 06:17:12 am »
I have done it,may be someone need?
In my project it was worked perfect.
public boolean AABBIntersectsAABB(Object3D obj1,Object3D obj2, Update_trackandcar update,World world)
{
   SimpleVector obj1_0=this.new_getworldspacebounds(obj1)[0];
   SimpleVector obj1_1=this.new_getworldspacebounds(obj1)[1];
   SimpleVector obj1_2=this.new_getworldspacebounds(obj1)[2];
   SimpleVector obj1_3=this.new_getworldspacebounds(obj1)[3];
   SimpleVector obj2_0=this.new_getworldspacebounds(obj2)[0];
   SimpleVector obj2_1=this.new_getworldspacebounds(obj2)[1];
   SimpleVector obj2_2=this.new_getworldspacebounds(obj2)[2];
   SimpleVector obj2_3=this.new_getworldspacebounds(obj2)[3];

   
   SimpleVector obj1_front=new SimpleVector(obj1_0.x-obj1_3.x,0,obj1_0.z-obj1_3.z);
   SimpleVector obj1_left=new SimpleVector(obj1_0.x-obj1_1.x,0,obj1_0.z-obj1_1.z);
   SimpleVector obj2_front=new SimpleVector(obj2_0.x-obj2_3.x,0,obj2_0.z-obj2_3.z);
   SimpleVector obj2_left=new SimpleVector(obj2_0.x-obj2_1.x,0,obj2_0.z-obj2_1.z);
   obj1_front.normalize();
   obj1_left.normalize();
   obj2_front.normalize();
   obj2_left.normalize();
   
   
   
   
   
   float distance1_z0=obj1_0.calcDot(obj1_front);
    float distance1_z1=obj1_1.calcDot(obj1_front);
    float distance1_z2=obj1_2.calcDot(obj1_front);
    float distance1_z3=obj1_3.calcDot(obj1_front);
    float distance1_z00=obj1_0.calcDot(obj2_front);
    float distance1_z01=obj1_1.calcDot(obj2_front);
    float distance1_z02=obj1_2.calcDot(obj2_front);
    float distance1_z03=obj1_3.calcDot(obj2_front);
   
    float distance1_x0=obj1_0.calcDot(obj1_left);
    float distance1_x1=obj1_1.calcDot(obj1_left);
    float distance1_x2=obj1_2.calcDot(obj1_left);
    float distance1_x3=obj1_3.calcDot(obj1_left);
    float distance1_x00=obj1_0.calcDot(obj2_left);
    float distance1_x01=obj1_1.calcDot(obj2_left);
    float distance1_x02=obj1_2.calcDot(obj2_left);
    float distance1_x03=obj1_3.calcDot(obj2_left);
   
   
   
    float[] distance1_z=new float[4];
    distance1_z[0]=distance1_z0;
    distance1_z[1]=distance1_z1;
    distance1_z[2]=distance1_z2;
    distance1_z[3]=distance1_z3;
    float[] distance1_z_00=new float[4];
    distance1_z_00[0]=distance1_z00;
    distance1_z_00[1]=distance1_z01;
    distance1_z_00[2]=distance1_z02;
    distance1_z_00[3]=distance1_z03;
   
   
   
    float[] distance1_x=new float[4];
    distance1_x[0]=distance1_x0;
    distance1_x[1]=distance1_x1;
    distance1_x[2]=distance1_x2;
    distance1_x[3]=distance1_x3;
   
    float[] distance1_x_00=new float[4];
    distance1_x_00[0]=distance1_x00;
    distance1_x_00[1]=distance1_x01;
    distance1_x_00[2]=distance1_x02;
    distance1_x_00[3]=distance1_x03;
   
   
   
   
    float distance2_z0=obj2_0.calcDot(obj1_front);
    float distance2_z1=obj2_1.calcDot(obj1_front);
    float distance2_z2=obj2_2.calcDot(obj1_front);
    float distance2_z3=obj2_3.calcDot(obj1_front);
    float distance2_z00=obj2_0.calcDot(obj2_front);
    float distance2_z01=obj2_1.calcDot(obj2_front);
    float distance2_z02=obj2_2.calcDot(obj2_front);
    float distance2_z03=obj2_3.calcDot(obj2_front);
   
   
    float distance2_x0=obj2_0.calcDot(obj1_left);
    float distance2_x1=obj2_1.calcDot(obj1_left);
    float distance2_x2=obj2_2.calcDot(obj1_left);
    float distance2_x3=obj2_3.calcDot(obj1_left);
    float distance2_x00=obj2_0.calcDot(obj2_left);
    float distance2_x01=obj2_1.calcDot(obj2_left);
    float distance2_x02=obj2_2.calcDot(obj2_left);
    float distance2_x03=obj2_3.calcDot(obj2_left);
   
   
    float[] distance2_z=new float[4];
    distance2_z[0]=distance2_z0;
    distance2_z[1]=distance2_z1;
    distance2_z[2]=distance2_z2;
    distance2_z[3]=distance2_z3;
    float[] distance2_z_00=new float[4];
    distance2_z_00[0]=distance2_z00;
    distance2_z_00[1]=distance2_z01;
    distance2_z_00[2]=distance2_z02;
    distance2_z_00[3]=distance2_z03;
   
    float[] distance2_x=new float[4];
    distance2_x[0]=distance2_x0;
    distance2_x[1]=distance2_x1;
    distance2_x[2]=distance2_x2;
    distance2_x[3]=distance2_x3;
    float[] distance2_x_00=new float[4];
    distance2_x_00[0]=distance2_x00;
    distance2_x_00[1]=distance2_x01;
    distance2_x_00[2]=distance2_x02;
    distance2_x_00[3]=distance2_x03;
   
   
   
    this.sort(distance1_x);
    this.sort(distance1_z);
    this.sort(distance2_x);
    this.sort(distance2_z);
    this.sort(distance1_x_00);
    this.sort(distance1_z_00);
    this.sort(distance2_x_00);
    this.sort(distance2_z_00);
   
   
   
   
   
   
   
   
   
   
    if(
       (distance1_x[0]>distance2_x[3])
                   ||
        (distance1_x[3]<distance2_x[0])
                   ||           
        (distance1_z[0]>distance2_z[3])
                   ||
        (distance1_z[3]<distance2_z[0])
                   ||
        (distance1_x_00[0]>distance2_x_00[3])
                   ||
        (distance1_x_00[3]<distance2_x_00[0])
                   ||           
        (distance1_z_00[0]>distance2_z_00[3])
                   ||
        (distance1_z_00[3]<distance2_z_00[0])
    )
   
    {return false;}
   
   
    else return true;
}

Offline Uncle Ray

  • int
  • **
  • Posts: 81
    • View Profile
Re: May be jpct would add AABBIntersectsAABB
« Reply #1 on: January 27, 2015, 06:18:19 am »
public void sort(float[] a)
{
  float temp=0;
  for(int i=a.length-1;i>0;--i)
 {
   for(int j=0;j<i;++j)
   {
    if(a[j+1]<a[j])
    {
     temp=a[j];
     a[j]=a[j+1];
     a[j+1]=temp;
    }
   }
  }
}






Offline Uncle Ray

  • int
  • **
  • Posts: 81
    • View Profile
Re: May be jpct would add AABBIntersectsAABB
« Reply #2 on: January 27, 2015, 06:19:49 am »
public SimpleVector[] new_getworldspacebounds(Object3D obj) {
   
   float[] objectSpaceBounds = obj.getMesh().getBoundingBox();
   SimpleVector mins = new SimpleVector(objectSpaceBounds[0], objectSpaceBounds[2], objectSpaceBounds[4]);
   SimpleVector maxs = new SimpleVector(objectSpaceBounds[1], objectSpaceBounds[3], objectSpaceBounds[5]);
   SimpleVector[] p = new SimpleVector[8];
   p[0] = new SimpleVector(mins.x,0, maxs.z); p[1] = new SimpleVector(mins.x, 0, mins.z); p[2] = new SimpleVector(maxs.x, 0, mins.z);
   p[3] = new SimpleVector(maxs.x, 0, maxs.z); p[4] = new SimpleVector(maxs.x, 0, mins.z);
   p[5] = new SimpleVector(maxs.x, 0, maxs.z); p[6] = new SimpleVector(mins.x, 0, mins.z); p[7] = new SimpleVector(mins.x, 0, maxs.z);
   
   for(int i=0;i<8;i++)
   {
    p.matMul(obj.getWorldTransformation());   
   }
   
   return p;
  }

Offline Uncle Ray

  • int
  • **
  • Posts: 81
    • View Profile
Re: May be jpct would add AABBIntersectsAABB
« Reply #3 on: January 27, 2015, 06:25:26 am »
it was in 2d situation,in this way 3d situation will solved not very hard.

AABB check AABB was usefull method in car racing game,may be in next release of jpct will add a AABB check AABB method.

if not?Above code would reached it too.