Jump to content


Photo

Collision Check Efficiency Question


  • Please log in to reply
6 replies to this topic

#1 TheMagician

TheMagician

    GMC Member

  • GMC Member
  • 215 posts

Posted 21 May 2012 - 07:48 PM

Hi everybody :smile:

In the game I'm currently developing there are lots of rectangular regions which are used to check whether the player has reached a quicksave area or a pipe entrance and so on. There'll be about 50-100 of these regions in each level (room).

Posted Image

Right now I create all those regions as instances of an object at the beginning of the level (loading the coordinates from an INI file).
Then, every step the player object checks (via instance_position) if it is above one of the instances and reacts accordingly.

I'm now thinking of the alternative to read the data from the INI file into a huge data structure (not exactly sure how, yet). The player object would then parse through the complete data structure every step to find out if it is inside one of the coordinate regions (via collision_rectangle).

Do you think it's worth the hassle (memory- and speed-wise) to re-write the code from using instances to data structures for the collision check?

Thanks a lot in advance,
Stefan
  • 0

#2 TheMagician

TheMagician

    GMC Member

  • GMC Member
  • 215 posts

Posted 22 May 2012 - 06:16 PM

May I kindly bump this topic? ;)
  • 0

#3 jo-thijs

jo-thijs

    GMC Member

  • GMC Member
  • 1452 posts
  • Version:GM8.1

Posted 22 May 2012 - 07:00 PM

I wouldn't know it for sure, but I think it is better to use instance_position.
Or even better would be, if you don't want to know with which block it collides, but only if it it collides, to use place_meeting.
And you could unprecise the collision masks if you want it even more speeded up.
  • 0

#4 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16790 posts
  • Version:GM:Studio

Posted 22 May 2012 - 07:05 PM

You COULD use a ds for this, but that wouldn't play nicely with slopes (you can get a nice tutorial here : http://www.yoyogames.com/tech_blog/7 ). However, instance_place, instance_position, place_meeting and such are all quite fast in general, especially if you don't have "precise" collisions for everything.


PS: Bumps are permitted only after 48hours...Posted Image
  • 0

#5 GStick

GStick

    All Secrets Exposed

  • GMC Member
  • 1571 posts
  • Version:GM:Studio

Posted 22 May 2012 - 07:50 PM

instance_position tends to be a bit faster, but they're all so similar in speed it's all going to come down to your algorithm. You could easily make one that would defeat the entire purpose of using instance_position. You're going to want to try running some different tests.

I'd probably just settle with using something like:

if (instance_position(x, y, QuickSave) != noone)
{
    // do quick save stuff
}

...and change it if it became a bottleneck. (That's also a situation where instance_position is faster to use than collision_rectangle by the way). I find it a good idea to reserve collision_rectangle for rectangles that aren't exactly your bounding box, since instance_position already has that covered.

Edited by GStick, 22 May 2012 - 07:51 PM.

  • 0

#6 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4686 posts
  • Version:GM8

Posted 22 May 2012 - 09:10 PM

Both will check a whole array of points, but use instance_position. Or as Noct said, use a grid or array according to that post he loves. Genrallky, collision_XXX scripts are much slower and you should avoid them if you can. If you want to handle objects like I do in my game, whereby only the lowest 16x16 sprite is handled and anything above that is drawn by that 16x16 sprited object (I won't go into the details about why this is such an awesome idea because it's only awesome in limited situations), then you would need either collision_line or collision_rectangle to check for collisions. Personally, if you want to use collision_XXXX, I am of the opinion these days that you should use a series of collision_line to check for collisions. You should be able to check an entire sprite with just 3 line checks, possibly 4. This is much, much faster than a collision_rectangle I'm sure.
  • 0

#7 TheMagician

TheMagician

    GMC Member

  • GMC Member
  • 215 posts

Posted 22 May 2012 - 09:23 PM

Thanks a lot for your input guys! :)
As nobody thinks that using instance_position is a horrible idea I'll stick to my system for the time being.
Good to know the alternatives, though.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users