Jump to content


Photo

Using bounding box in collisions instead of sprite


  • Please log in to reply
4 replies to this topic

#1 Sindarin

Sindarin

    Indie Game Developer

  • New Member
  • 1644 posts
  • Version:GM:HTML5

Posted 30 August 2011 - 06:04 AM

I made this script to check if two objects collide, thing is I want to use the bounding box instead of sprite width/height. I tried with bbox_right and bbox_bottom but couldn't get this done,

collidesWith(object1,object2);
{
	if (instance_exists(argument[0]) && instance_exists(argument[1]))
	{
    	return real(argument[0].x < argument[1].x + argument[1].sprite_width && 
    	argument[0].x + argument[0].sprite_width > argument[1].x && 
    	argument[0].y < argument[1].y + argument[1].sprite_height && 
    	argument[0].y + argument[0].sprite_height > argument[1].y);
   	//return 1 if true, 0 if false
	}
	else
	{
    	return -1; //return -1 if instance doesn't exist
	}
}

  • 0

#2 TheMusicDork

TheMusicDork

    GMC Member

  • GMC Member
  • 305 posts

Posted 30 August 2011 - 06:54 AM

you could use a mask?
or you could use bbox_left, etc, AND NOT USE THE X AND Y VARIABLES! The bounding boxes are updated every step to match the actual position of them instead of the relative position.
=)
TMD

edit: you also don't need the else statement. because the return statement exits the block of code, so wither it exited (because the if was true) or it didn't (and therefore runs the next line of code)

Edited by TheMusicDork, 30 August 2011 - 06:56 AM.

  • 0

#3 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16792 posts
  • Version:GM:Studio

Posted 30 August 2011 - 07:00 AM

Well, your script does work for me, so I wonder if maybe you have done something worng elsewhere? Would it not be easier to do a collision_rectangle() check? EG:
var i;
i=argument[0]; //Just to make it less to write!
if collision_rectangle(i.x, i.y,  i.x+i.sprite_width, i.y+i.sprite_height, argument[1], false, true) return 1 else return -1;

//Or even
return collision_rectangle(i.x, i.y,  i.x+i.sprite_width, i.y+i.sprite_height, argument[1], false, true);
//which will return true or false (actually -4(noone) or the id, but the result is still true or false!);

EDIT: As the post below has pointed out I misread the topic... What didn't work with bboxes? What was your code?

Edited by Nocturne, 30 August 2011 - 07:21 AM.

  • 0

#4 TheMusicDork

TheMusicDork

    GMC Member

  • GMC Member
  • 305 posts

Posted 30 August 2011 - 07:16 AM

he didn't say it didn't work, he just couldn't convert it to use bounding boxes. (and yes, using built-in collision functions would be faster and more practical)
  • 0

#5 Sindarin

Sindarin

    Indie Game Developer

  • New Member
  • 1644 posts
  • Version:GM:HTML5

Posted 30 August 2011 - 05:41 PM

Thanks people, I seem to have gotten it right,

collidesWith(object1,object2);
{
	if (instance_exists(argument[0]) && instance_exists(argument[1]))
	{
    	return real(argument[0].bbox_left < argument[1].bbox_right &&
    	argument[0].bbox_right > argument[1].bbox_left &&
    	argument[0].bbox_top < argument[1].bbox_bottom &&
    	argument[0].bbox_bottom > argument[1].bbox_top);
	}
}

I had a suspicion bbox_ also matched the position/size of the object.

var i;
i=argument[0]; //Just to make it less to write!
if collision_rectangle(i.x, i.y, i.x+i.sprite_width, i.y+i.sprite_height, argument[1], false, true) return 1 else return -1;

//Or even
return collision_rectangle(i.x, i.y, i.x+i.sprite_width, i.y+i.sprite_height, argument[1], false, true);
//which will return true or false (actually -4(noone) or the id, but the result is still true or false!);


Yes, I'll have to try the above code with bbox.

edit: yes, suppose it will be faster using the native collision_rectangle instead,

{
	if (collision_rectangle(argument[0].bbox_left, argument[0].bbox_top, argument[0].bbox_right, argument[0].bbox_bottom, argument[1], false, true))
	{
    	return true;
	}
	else
	{
    	return false;
	}
}

Edited by Sindarin, 30 August 2011 - 07:34 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users