Jump to content


Photo
* * * * * 13 votes

What's the difference: collision functions


  • Please log in to reply
25 replies to this topic

#1 torigara

torigara

    GMC Member

  • GMC Member
  • 6507 posts

Posted 26 July 2010 - 04:09 PM

  • Title: What's the difference: place_meeting, position_meeting and so on
  • Description: Describes the difference between collision functions and troubleshoots a common misuse.
  • GM Version: GM 5 and later
  • Registered: no
  • File Type: N/A
  • File Size: N/A
  • File Link: N/A
Additional Info
There are a number of functions to check if there is an object at the specified location. This tutorial describes differences of those functions to let you choose an appropriate one (especially between the common misused two, place_meeting and position_meeting.)

Summary
The table below summarizes the difference of functions:
Function            Checks using   Checks against     Returns
----------------------------------------------------------------
place_free          sprite/mask    solid objects      true/false
place_empty         sprite/mask    any object         true/false
place_meeting       sprite/mask    specified object   true/false
position_empty      point          any object         true/false
position_meeting    point          specified object   true/false
instance_place      sprite/mask    specified object   instance id
instance_position   point          specified object   instance id

Place vs. position
The most important difference is whether the function name contains "place" or "position." Functions those named place always put the current instance that is executing the code at the specified position and checks if its sprite (or mask if assigned) collides with another object.

On the other hand, functions those named position checks the exact point, regardless of the instance that is executing the code.

Posted Image

Here are some consequences:
  • place functions doesn't work at all if the instance executing the code has no sprite or mask.
  • place functions never detects collision with itself, whereas position functions can be used with self.
    // This one doesn't work no matter whether the instance has a sprite.
    if (place_meeting(mouse_x, mouse_y, id)) { ... }
    
    // This one correctly checks if the mouse is on the instance.
    if (position_meeting(mouse_x, mouse_y, id)) { ... }

Meeting vs. instance
When you use a function that has meeting in its name, it just tells whether there is an object and returns either true or false. On the other hand, functions those named instance returns the colliding instance's id, or the special value noone (-4) when there is no collision.

You may sometimes see that the return value of instance_place or instance_position is used as if it is boolean .That works because Game Maker's if statement evaluates a positive number as true and negative number as false. Therefore, "if (A)" is equivalent to "if (A>0)" and "if (!A)" is equivalent to "if (A<=0)". It enables you to use instance_place or instance_position in place of place_meeting or position_meeting. However, note that the opposite doesn't work.
// This checks if there is an instance of object0 under the mouse,
// and store its instance id to the variable inst.
// The result is noone (-4) if there is no collision.
inst = instance_position(mouse_x, mouse_y, object0);
if (inst) { // This is equivalent to saying "if (inst > 0)"
    inst.selected = true; // Manipulate the instance's variable
}

Free vs. empty
As summarized above, the difference of place_free and place_empty is rather trivial. The former checks if there is a solid object, while the latter checks for any object including non-solid ones. Note that they return true when there's not a collision, as opposed to place_meeting and position_meeting. There is position_empty but no corresponding function position_free, though.

Troubleshooting
A common mistake is to forget that a place function uses the sprite of the current instance that is calling the function. See the following example code: it is executed by a controller object and is meant to create an enemy in a random place that doesn't collide with walls. This likely fails to work. Why? A controller object generally has no sprite or mask, so place_free detects no collision for the controller.
// This code is meant to create an enemy in a free place
// but doesn't actually work.
do {
    xx = random(room_width);
    yy = random(room_height);
} until place_free(xx, yy);
instance_create(xx, yy, obj_enemy);

To make it work properly, we have to either give the controller object a sprite, or make the enemy object itself check for collisions.
// Create the instance first (the initial position doesn't matter.)
inst = instance_create(0, 0, obj_enemy);
// Then make it find a free place itself.
with (inst) {
    do {
        x = random(room_width);
        y = random(room_height);
    } until place_free(x, y);
}

  • 46

#2 lasttea999

lasttea999

    GMC Member

  • GMC Member
  • 290 posts

Posted 27 July 2010 - 10:15 PM

Very nice! Such an organized analysis. Thank you for this tutorial!
  • 0

#3 iluvfuz

iluvfuz

    GMC Member

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

Posted 31 July 2010 - 01:24 AM

Very easy to read and very helpful! I had always wondered myself (though after using them for a while, I had deducted how a few of them worked), and this answered everything :) I especially liked the chart comparing the functions against each other.


rep+
  • 0

#4 Nubcake

Nubcake

    Nubcake

  • GMC Member
  • 1320 posts
  • Version:Unknown

Posted 04 August 2010 - 07:36 PM

great tutorial! , i was looking for a bit more though :D
  • 0

#5 Jackount

Jackount

    GMC Member

  • New Member
  • 194 posts

Posted 08 August 2010 - 08:25 PM

THANK you! That really cleared up some things for me ;=) Now i fully understand the main platform engine, and can make it even cooler xD
  • 0

#6 ClassyGoat

ClassyGoat

    Goatee Connoisseur

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

Posted 15 January 2011 - 01:02 AM

Phenomenal! You get a gold star! Posted Image
  • 1

#7 kemlex

kemlex

    GMC Member

  • GMC Member
  • 72 posts

Posted 15 January 2011 - 01:21 AM

so if i wanted to add a basic attack system would it be:

if position_meeting(x+1,y,obj_attack) {
global.hp-=10}

  • 0

#8 Jackount

Jackount

    GMC Member

  • New Member
  • 194 posts

Posted 15 January 2011 - 02:49 AM

so if i wanted to add a basic attack system would it be:

if position_meeting(x+1,y,obj_attack) {
global.hp-=10}


That is very unlikely. I'm quite sure tha for your example, you meant to use place_meeting. I think the drawings explain it quite well.
  • 0

#9 Newly Discovered

Newly Discovered

    Harmonious Genius

  • GMC Member
  • 2475 posts
  • Version:GM8

Posted 16 January 2011 - 10:10 AM

Brilliant tutorial thanks so much!
Very informative and helped me understand so much easier now!

Thank you!
  • 0

#10 jsorgeagames

jsorgeagames

    GMC Member

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

Posted 07 February 2011 - 04:07 AM

This is a very nice tutorial. Thanks for clearing this up for me :)
  • 0

#11 BK239

BK239

    GMC Member

  • New Member
  • 156 posts

Posted 08 February 2011 - 02:13 PM

So how do I use place_meeting and position_meeting in collisions and which is the better in TDS's?
  • 0

#12 hardlylike

hardlylike

    GMC Member

  • New Member
  • 293 posts

Posted 09 February 2011 - 08:05 PM

thanks alot, I saved this link!<3
  • 0

#13 reek

reek

    GMC Member

  • New Member
  • 66 posts

Posted 14 February 2011 - 03:32 AM

Bump
  • 0

#14 chaz13

chaz13

    GMC Member

  • GMC Member
  • 4212 posts
  • Version:Unknown

Posted 16 February 2011 - 09:18 PM

This is a very useful tutorial to link beginners to.
  • 0

#15 whoisit

whoisit

    Always Learning

  • New Member
  • 242 posts
  • Version:GM8

Posted 01 April 2011 - 02:27 PM

Just found this and like all of your writings clearly written and understandable.

Thanks
  • 0

#16 Turell

Turell

    GMC Member

  • New Member
  • 13 posts

Posted 22 May 2011 - 11:22 AM

is it not possible to check whether there is a collision between two specific objects?
  • 0

#17 lasttea999

lasttea999

    GMC Member

  • GMC Member
  • 290 posts

Posted 24 May 2011 - 10:45 PM

is it not possible to check whether there is a collision between two specific objects?

I haven't tested this yet, but how about making a script like this one?

// scr_collision
// argument0 - first instance
// argument1 - second instance
// argument2 - horizontal pixels to shift first object by
// argument3 - vertical pixels to shift second object by
with (argument0) {
  if (place_meeting(argument2, argument3, argument1)) return true;
}
return false;

Edited by lasttea999, 24 May 2011 - 10:48 PM.

  • 0

#18 Figuras

Figuras

    GMC Member

  • New Member
  • 51 posts
  • Version:GM8

Posted 09 March 2012 - 03:34 PM

Oh, thanks a lot for this! That answers many questions I had for years. :D
  • 0

#19 Trinnexx

Trinnexx

    GMC Member

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

Posted 19 March 2012 - 01:07 PM

Thanks for clearing that up.
Makes me curious about the D&D Check Collision behaviour. It's tool tip says 'if relative position'.
  • 0

#20 Fihrilkamal

Fihrilkamal

    GMC Member

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

Posted 24 March 2012 - 08:09 PM

Awesome tutorial! I'm never know that place meeting actually works like that.. Thank you very much!!
  • 0

#21 greyzebra

greyzebra

    GMC Member

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

Posted 15 May 2012 - 11:20 AM

Thank you for the very useful tutorial!

Edited by greyzebra, 15 May 2012 - 11:21 AM.

  • 0

#22 Dragonite

Dragonite

    GMC Member

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

Posted 09 August 2013 - 08:14 PM

Thanks for this. I really needed this.


  • 0

#23 Boemanhabbababba

Boemanhabbababba

    GMC Member

  • GMC Member
  • 26 posts
  • Version:Mac

Posted 19 August 2013 - 02:10 PM

Very helpful. Thanks! :)


  • 0

#24 ZorisX

ZorisX

    GMC Member

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

Posted 14 November 2013 - 03:15 AM

Very useful. I don't think GML likes the UNTIL command though...tried it with both FOR and DO commands...


  • 0

#25 Exevalon

Exevalon

    GMC Member

  • GMC Member
  • 25 posts

Posted 22 July 2014 - 04:25 AM

Excellent. Good to know the minute differences.


  • 0

#26 Mortalo

Mortalo

    GMC Member

  • New Member
  • 5 posts
  • Version:GM:Studio

Posted 18 October 2014 - 03:44 PM

I love you! I was not aware of "check" differences. Thanks a lot, now my Triceratops works just how I wanted to.


  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users