3D 3D Collision Detection Script for public use

H

Harrison Vanderbyl

Guest
feel free to use as long as you credit me and send me an email to
harrbri1222 @ gmail . com
to let me know how its working or any problems you encounter
if anyone needs the script for converting d3d models into a collision buffer pm me

input is as specified
x1,y1,z1,x2,y2,z2,buffer
xyz1 being the start of the ray
xyz2 being the destination

buffer format is as follows
buffer type: bufferf32

x1,y1,z1 polygon point 1
nx,ny,nz normal of polygon
x2,y2,z2
x3,y3,z3
tx1,ty1 texture coords
tx2,ty2
tx3,ty3

it returns a array with the following specifications
Code:
[0] 1: no collision, 0: collision
[1] the location in the buffer of the polygon, useful for highlighting polygon thats been collided with
[2]\
[3] x.y,z normal of the polygon collided with
[4]/
[5]\
[6]x,y,z of where the interception line will end up if traveled parallel to wall, useful for sliding a camera along the polygon
[7]/
[8]\
[9]x,y,z of the exact position the line intercepts the polygon
[10]/
[11] unused
[12] x,y exact texture coordinates of intercept, useful for painting bullet holes in a surface or etc
[13]/
Code:
///checkintercepts(x1,y1,z1,x2,y2,z2,buffer)
var p2,output,finaldistance,buff_tell,buffer_size,cosin,dis,m,n,t1,t2,check_vector,in,sls,osls,cp,cpn,dp,p1,abuff;
abuff = argument6
p1[0]=argument0
p1[1]=argument1
p1[2]=argument2
p2[0]=argument3
p2[1]=argument4
p2[2]=argument5
output[0]=1
output[1] = 0
output[2] = 0
output[3] = 0
output[4] = 0
output[5] = p2[0]
output[6] = p2[1]
output[7] = p2[2]
output[8] = p2[0]
output[9] = p2[1]
output[10] = p2[2]
output[11] = 0
output[12] = 0
output[13]=0
dista = point_distance_3d(p1[0],p1[1],p1[2],p2[0],p2[1],p2[2])
finaldistance=100000


buffer_size = buffer_get_size(abuff)
for(buff_tell=0;buff_tell<(buffer_size-18*4);buff_tell=buff_tell+(18*4)){
   
    buffer_seek(abuff,buffer_seek_start,buff_tell)
   
    m[0] = buffer_read(abuff, buffer_f32);
    m[1] = buffer_read(abuff, buffer_f32);
    m[2] = buffer_read(abuff, buffer_f32);
   
    n[0] = buffer_read(abuff, buffer_f32);
    n[1] = buffer_read(abuff, buffer_f32);
    n[2] = buffer_read(abuff, buffer_f32);
   
   
   
    //determins perpendicular distance to plane of polygon
    perpendicular_distance = dot_product_3d(p1[0]-m[0],p1[1]-m[1],p1[2]-m[2],n[0],n[1],n[2])
    //discard following if all normals are facing the correct way, gives unknown performance boost if deleted
    if perpendicular_distance < 0{
        perpendicular_distance = -perpendicular_distance
        n[0] = -n[0]
        n[1]=-n[1]
        n[2]=-n[2]
    }
   
   
    //determins if possibility of intersection exists
    if (perpendicular_distance<dista){
       
        if perpendicular_distance > dot_product_3d(p1[0]-p2[0],p1[1]-p2[1],p1[2]-p2[2],-n[0],-n[1],-n[2]){
           
           
            m[3] = buffer_read(abuff, buffer_f32);
            m[4] = buffer_read(abuff, buffer_f32);
            m[5] = buffer_read(abuff, buffer_f32);
            m[6] = buffer_read(abuff, buffer_f32);
            m[7] = buffer_read(abuff, buffer_f32);
            m[8] = buffer_read(abuff, buffer_f32);
           
           
            dis = point_distance_3d(0,0,0,(p2[0]-p1[0]),(p2[1]-p1[1]),(p2[2]-p1[2]))
            check_vector[0] = (p2[0]-p1[0])/dis
            check_vector[1] = (p2[1]-p1[1])/dis
            check_vector[2] = (p2[2]-p1[2])/dis
           
            collision_distance = -1
            cosin = dot_product_3d(n[0],n[1],n[2],check_vector[0],check_vector[1],check_vector[2])
            //prevents /0 errors
            if cosin!=0{
                //determins collision distance
                collision_distance = -perpendicular_distance/cosin
            }                      
           
           
            if ((collision_distance > 0)){
                //locates intersection on plane
                in[0] = p1[0]+check_vector[0]*collision_distance
                in[1] = p1[1]+check_vector[1]*collision_distance
                in[2] = p1[2]+check_vector[2]*collision_distance
                //determins if intersection is within triangle
                sls[0]=point_distance_3d(m[0],m[1],m[2],m[3],m[4],m[5])
                sls[1]=point_distance_3d(m[0],m[1],m[2],m[6],m[7],m[8])
                sls[2]=point_distance_3d(m[6],m[7],m[8],m[3],m[4],m[5])
                sls[3]=(sls[0]+sls[1]+sls[2])/2
                sls[4]=sqrt(abs(sls[3]*(sls[3]-sls[0])*(sls[3]-sls[1])*(sls[3]-sls[2])))
                osls[0]=point_distance_3d(in[0],in[1],in[2],m[0],m[1],m[2])
                osls[1]=point_distance_3d(in[0],in[1],in[2],m[3],m[4],m[5])
                osls[2]=point_distance_3d(in[0],in[1],in[2],m[6],m[7],m[8])
                osls[3]=(sls[0]+osls[0]+osls[1])/2
                osls[4]=(sls[1]+osls[0]+osls[2])/2
                osls[5]=(sls[2]+osls[2]+osls[1])/2
                osls[6]=sqrt(abs(osls[3]*(osls[3]-sls[0])*(osls[3]-osls[0])*(osls[3]-osls[1])))
                osls[7]=sqrt(abs(osls[4]*(osls[4]-sls[1])*(osls[4]-osls[0])*(osls[4]-osls[2])))
                osls[8]=sqrt(abs(osls[5]*(osls[5]-sls[2])*(osls[5]-osls[2])*(osls[5]-osls[1])))
                osls[9]=(osls[6]+osls[7]+osls[8])
                area = abs(osls[9]-sls[4])
               
                if (area<0.005){
                    //determins if polygon is the first polygon between points
                    if collision_distance<finaldistance{
                       
                        finaldistance=collision_distance
                        //reads texture coordinates
                        tx[0] = buffer_read(abuff, buffer_f32);
                        tx[1] = buffer_read(abuff, buffer_f32);
                        tx[2] = buffer_read(abuff, buffer_f32);
                        tx[3] = buffer_read(abuff, buffer_f32);
                        tx[4] = buffer_read(abuff, buffer_f32);
                        tx[5] = buffer_read(abuff, buffer_f32);
                       
                     
                       
                        output[0] = 0
                        output[1] = buff_tell/(18*4)
                        output[2] = n[0]
                        output[3] = n[1]
                        output[4] = n[2]
                        output[5] = in[0]+n[0]*perpendicular_distance
                        output[6] = in[1]+n[1]*perpendicular_distance
                        output[7] = in[2]+n[2]*perpendicular_distance
                        output[8] = in[0]
                        output[9] = in[1]
                        output[10] = in[2]
                        output[11] = 0
                        output[12] = tx[0]*osls[8]/sls[4]+tx[2]*osls[7]/sls[4]+tx[4]*osls[6]/sls[4]
                        output[13] = tx[1]*osls[8]/sls[4]+tx[3]*osls[7]/sls[4]+tx[5]*osls[6]/sls[4]
                      //use if you dont need to worry about intersecting multiple polygons
                      //  return output;
}}}}}//}}
}
return output
 
Last edited by a moderator:
Top