Pages: [1]
  Print  
Author Topic: Collisions  (Read 1443 times)
Offline (Male) retep998
Posted on: March 23, 2010, 10:27:56 AM

Member
Location: Where else?
Joined: Jan 2010
Posts: 248
MSN Messenger - retep998@charter.net AOL Instant Messenger - retep998 Yahoo Instant Messenger - retep998
View Profile Email
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!
« Last Edit: March 24, 2010, 09:21:56 AM by retep998 » Logged
Post made March 23, 2010, 07:49:02 PM was deleted at the author's request.
Offline (Male) Josh @ Dreamland
Reply #2 Posted on: March 23, 2010, 07:53:31 PM

Prince of all Goldfish
Developer
Location: Ohio, United States
Joined: Feb 2008
Posts: 2946

View Profile Email
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...
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) retep998
Reply #3 Posted on: March 23, 2010, 09:34:36 PM

Member
Location: Where else?
Joined: Jan 2010
Posts: 248
MSN Messenger - retep998@charter.net AOL Instant Messenger - retep998 Yahoo Instant Messenger - retep998
View Profile Email
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.
« Last Edit: March 23, 2010, 09:51:20 PM by retep998 » Logged
Pages: [1]
  Print