ENIGMA Forums

Contributing to ENIGMA => Function Peer Review => Topic started by: retep998 on March 23, 2010, 10:27:56 am

Title: Collisions
Post by: retep998 on March 23, 2010, 10:27:56 am
Imma be working on collision code right here on the forum without compiling it!

collision_point:
Code: [Select]
//Depending upon how fast different variations of the following code will run, this code will be rearranged to optimize speed.
//Oh wow, I forgot that this returns the id of the colliding instance, so I fixed it
int/*Or whatever datatype an instance id is...*/ collision_point(x,y,obj,prec,notme){
    with(obj){//x,y refer to the arguments. this.x,this.y refer to the object we are checking
        if(obj==notme){continue;}//I could check earlier if the calling instance is not a member of obj, to avoid having to do this check every single iteration.
        if(this.x-bb_left<=x and this.x+bb_right>=x and this.y-bb_top<y and this.y+bb_bottom>y){//the four bb_ variables are assumed to be the bounding box of the mask of the object relative to the origin, and they are all assumed to be positive.
            if(!prec){//I might put precise and nonprecise checking into their own separate loops. With properly optimized mask handling, i could check the point collisions for precise checking without having to check the bounding box!
                return id;
            }
            //The following code may be altered depending upon the relativity of mask texture coordinates and the origin and the bounding box
            if(mask[x-this.x+bb_left,y-this.y+bb_top]){//Assuming mask is a bool rectangular array with coordinates starting at the top left of the bounding box
                return id;
            }
        }
    }
}

Code: [Select]
//This is the same as the above except optimized to run a bit faster by calling certain checks only if needed.
int collision_point(x,y,obj,prec,notme){
if(this_is_an_instance_of(obj)){//Checks if the notme check should be checked
if(prec){
with(obj){
if(obj==notme){continue;}
if(this.x-bb_left<=x and this.x+bb_right>=x and this.y-bb_top<y and this.y+bb_bottom>y){
if(mask[x-this.x+bb_left,y-this.y+bb_top]){
return id;
}
}
}
return noone;
}
with(obj){
if(obj==notme){continue;}
if(this.x-bb_left<=x and this.x+bb_right>=x and this.y-bb_top<y and this.y+bb_bottom>y){
return id;
}
return noone;
}
}
if(prec){
with(obj){
if(this.x-bb_left<=x and this.x+bb_right>=x and this.y-bb_top<y and this.y+bb_bottom>y){
if(mask[x-this.x+bb_left,y-this.y+bb_top]){
return id;
}
}
}
return noone;
}
with(obj){
if(this.x-bb_left<=x and this.x+bb_right>=x and this.y-bb_top<y and this.y+bb_bottom>y){
return id;
}
return noone;
}
}

Also here is how rotation and scaling will be handled:
There will be a macro called INSTANT_MASK_UPDATE
Each instance will have a mask sprite and copy of the mask with rotation and scaling applied.
If INSTANT_MASK_UPDATE is enabled, then every single time you modify image_angle, image_xscale, or image_yscale, the second mask will be immediately updated, even in the middle of a collision event. Also, if you modify mask_index, both masks will be updated right away.
If it is disabled, then they will only update at the end of each event. However you can still update your masks by using mask_flush();

Moar to come!
Title: Re: Collisions
Post by: Fede-lasse on March 23, 2010, 07:49:02 pm
Oh God...
Title: Re: Collisions
Post by: Josh @ Dreamland on March 23, 2010, 07:53:31 pm
Your assumptions are for the most part correct. I have not yet implemented a type for instances. I'm thinking it'll be of form typedef something instance_t, but have not yet decided.

Also, doesn't support rotation and scaling, yes? The math for that is out of my league due to current sleep levels. Perhaps I'll add it tomorrow...
Title: Re: Collisions
Post by: retep998 on March 23, 2010, 09:34:36 pm
Quote
Oh God...
I practically memorized all the essential gm functions, and it's much faster to just code without having to test it or wait for an IDE to load or create a test program to try my code.
Quote
Perhaps I'll add it tomorrow...
No you will not.
I got everything under control, so as long as everyone keeps their grubby hands off the collisions, everything will be fine.