Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Which is more efficient  (Read 574 times)

0 Members and 1 Guest are viewing this topic.
Which is more efficient
« on: January 14, 2012, 09:38:07 am »
  • *
  • Reputation: +12/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1260
So the problem is as follows:
My gamemaker game uses two methods of checking for collisions against solids (I'm talking about the rectangular solid objects only right now) and I'm wondering which is better / more efficient. I have a strong hunch that method 1 is faster, but would like to see it confirmed before I spend a couple of days on changing things back. If anybody knows of a method to check the differency myself (get time before and after lines of code?) then that would also help.

Method 1: Default mask checking.
You'd get something like the code below if used (in a larger movement script). The downside to this is that you need to make a lot of masks ahead of time for every single size you want. All dimensions are multiples of 8, but that still means a lot of sprites. 8x8, 16,8, 32x8, 8x16, 8x32, 8x80, 16x16 etc. I've got around 60+ different rectangular sized sprites in use right now and that's only because I often avoid adding new masks when a combination of older masks can do the same. To limit the number of sprites, the filesize, the loading time etc. I do this reluctantly though because less objects, to check collisions against, the better.

Code: [Select]

// Set depth
if (creator_id.depth_scale = c_Lower) {argument[4] = obj_Solid_Lower_Floor_Unpassable} else {argument[4] = obj_Solid_Upper_Floor_Unpassable};

// Check collisions against solid masking
argument[3] = script_Check_If_Two_Objects_Collide(id, argument[4], round(New_X_Position+Final_xspeed)-x,round(New_Y_Position+Final_yspeed)-y, true);
if (argument[3]!=0)
    // Check if you didn't start in the same object
    if (place_meeting(round(creator_id.creator_id.x + x_correction), round(creator_id.creator_id.y + y_correction - creator_id.height_above_ground), argument[3]) = false)
         // action


Method 2: Imaginary collisions.
Check out the script below. I give the solid object a width, length, depth and offset. Next I use this code below to check if an enemy or object is colliding with it. The upside is freedom to create any shape you want without problem. Very usefull if you want to cut down on the number of objects in the room. The downside, I suspect, is (far?) less efficient collision checking.

Code: [Select]
// WITH (object you wish to check other collisions against)

// argument[0] = object to check if it collides with self
// argument[1] = correct self location on the x-axis
// argument[2] = correct self location on the y-axis
// argument[3] = compare depth_scales [true/false] (OPTIONAL)

// SET:
// argument[4] = instance counter
// argument[5] = number of objects to check collision against
// argument[6] = instance you are currently checking collisions against
// argument[7] = indicator to help check for matching depth_scales

// Returns collision object_id or 0

argument[4] = 0;
argument[5] = instance_number(argument[0]);

if (argument[5] > 0)
    // check collisions with all objects of the given sort
    for (argument[4]=0; argument[4]<argument[5]; argument[4]+=1)
        argument[6] = instance_find(argument[0], argument[4]);
        argument[7] = false;

        if (argument[3] = true)
            if (depth_scale = argument[6].depth_scale) {argument[7] = true};
            argument[7] = true;

        if (argument[7] = true)
            if (collision_rectangle(argument[6].x + argument[6].border_x - argument[1],
                                    argument[6].y + argument[6].border_y - argument[2],
                                    argument[6].x + argument[6].width - 2*argument[6].border_x - argument[1],
                                    argument[6].y + argument[6].height - 2*argument[6].border_y - argument[2],
                                    id, true, false) > 0)
                return argument[6];

return 0;

Anybody have any thoughts on the matter?
Still working on my Alttp-engine: http://www.zfgc.com/forum/index.php?topic=33701.0
Want to help out in whatever way you are talented? Send me a PM.
Pages: [1]   Go Up


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved

Page created in 0.069 seconds with 23 queries.