GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2009-03-29 23:40:30

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

CollisionPointIDs, super fast find to estimate the collision point.

I could not sleep last night because I had this sudden idea on finding the collision point between 2 instances using a binary search type setup with collision rectangle...

http://host-a.net/icuurd12b42/intersectrect.gm6

Expand//CollisionPointIDs(id1,id2)

//determines the collision point of 2 instances
//return 0 if no collision point
//return 1 if collision point
//you should call it on collision
//it will work if the 2 instances overlap or if 2 egdes are touching (Better result)
//so make sure you move them into contact with move contact solid.
//sets the following instance variables for you to get the region 
//(Eventually set to about a 1x1 region)
//__left;
//__top;
//__right;
//__bottom;
//__x is the average (center of rect)
//__y is the average (center of rect)

if(argument2)
{
    var left,top,right,bottom;
    left = argument3;
    top = argument4;
    right = argument5;
    bottom = argument6;
    if(right-left < 1)
    if(bottom-top < 1)
        return 1;
    if(right-left > bottom-top)
    {
        if(collision_rectangle(left,top,(left+right)/2,bottom,argument0,true,false))
        if(collision_rectangle(left,top,(left+right)/2,bottom,argument1,true,false))
        {
            __left = left;
            __top = top;
            __right = (left+right)/2;
            __bottom = bottom;
            __x = (__left+__right)/2;
            __y = (__top+__bottom)/2;
            if(CollisionPointIDs(argument0,argument1,argument2,__left,__top,__right,__bottom))
            {
                if(__right-__left < 1)
                if(__bottom-__top < 1)
                return 1;
            }
        }
        if(collision_rectangle((left+right)/2,top,right,bottom,argument0,true,false))
        if(collision_rectangle((left+right)/2,top,right,bottom,argument1,true,false))
        {
            __left = (left+right)/2;
            __top = top;
            __right = right;
            __bottom = bottom;
            __x = (__left+__right)/2;
            __y = (__top+__bottom)/2;
            if( CollisionPointIDs(argument0,argument1,argument2,__left,__top,__right,__bottom))
            {
                if(__right-__left < 1)
                if(__bottom-__top < 1)
                return 1;
            }

        }
        return 0;
    }
    else
    {
        if(collision_rectangle(left,top,right,(top+bottom)/2,argument0,true,false))
        if(collision_rectangle(left,top,right,(top+bottom)/2,argument1,true,false))
        {
            __left = left;
            __top = top;
            __right = right;
            __bottom = (top+bottom)/2;
            __x = (__left+__right)/2;
            __y = (__top+__bottom)/2;
            if( CollisionPointIDs(argument0,argument1,argument2,__left,__top,__right,__bottom))
            {
                if(__right-__left < 1)
                if(__bottom-__top < 1)
                return 1;
            }

        }
        if(collision_rectangle(left,(top+bottom)/2,right,bottom,argument0,true,false))
        if(collision_rectangle(left,(top+bottom)/2,right,bottom,argument1,true,false))
        {
            __left = left;
            __top = (top+bottom)/2;
            __right = right;
            __bottom = bottom;
            __x = (__left+__right)/2;
            __y = (__top+__bottom)/2;
            if( CollisionPointIDs(argument0,argument1,argument2,__left,__top,__right,__bottom))
            {
                if(__right-__left < 1)
                if(__bottom-__top < 1)
                return 1;
            }

        }
        return 0;
    }
}
else
{
    var xx,yy,xxx,yyy;
    
    xx = max(argument0.bbox_left-1,argument1.bbox_left-1)
    yy = max(argument0.bbox_top-1,argument1.bbox_top-1)
    xxx = min(argument0.bbox_right+1,argument1.bbox_right+1)
    yyy = min(argument0.bbox_bottom+1,argument1.bbox_bottom+1)
    
    if(xx>xxx) return 0;
    if(yy>yyy) return 0;
    
    __left = xx;
    __top = yy;
    __right = xxx;
    __bottom = yyy;
    __x = (xx+xxx)/2
    __y = (yy+yyy)/2
    return CollisionPointIDs(argument0,argument1,1,__left,__top,__right,__bottom);
}

Last edited by icuurd12b422 (2009-03-29 23:54:33)

Offline

#2 2018-05-13 20:40:36

Roadhammer Gaming
Member
Registered: 2018-05-12
Posts: 2

Re: CollisionPointIDs, super fast find to estimate the collision point.

Thank you for making this, I've been racking my brain trying to do this because I have a player that doesn't move, a background that does move, and the player can collide with lines drawn in background border images that are the same size as, and stay with the background. Great script!

Offline

Board footer

Powered by FluxBB