GML (Game Maker 8)Collision detection for defeating enemies by destroying the solid objects underneath them

Build Man

Member
So here, I have the feature of enemies standing (and walking) on destructible objects. I have made a code that hurts and defeats enemies when said objects are destroyed. The code looks like this:

GML:
var inst_break;
inst_break = instance_place(x, y-1, Elder_Rattleshell);
if (inst_break != noone)
 {
 with (inst_break) event_user(1);
 }
And event_user(1) of Elder Ratteshell, whom I have taken as an example, looks like this:

Code:
sprite_index = Enemy_RattleElder_Damaged
vspeed = -9
hspeed = 0
enemy_health -= 20
They have a health of 10, so they are going to be beat anyway. The problem being, the former piece of code activates if it detects an enemy standing just by the single pixel of their collision mask, which looks like this:

CollisionMask.png

What I want is for the code to activate not just by the single pixel, but only if an enemy in question stands on it by the half of it's collision mask or more. Please do not suggest change of collision masks themselves, they are used for multitude other behaviours.
 

Build Man

Member
I might want an instructions regarding bounding boxes, because I could not do it correctly. It either did not work, or it worked wrong.
 

Build Man

Member
Well, what did you do? I can't very well tell you what you did wrong if I don't know what you did at all.
Sorry, I erased the results, because I definitely did not like them. Let us just say, I have absolutely no idea how to do it correctly.
 
Ok, so what you want to do is check to see if the horizontal bounding box of the rattlesnake is entirely within the horizontal bounding box of the block (or rather, this is one outcome that you might want). So, looking at the manual, we can see the bounding box variables are bbox_left, bbox_top, bbox_righand bbox_bottom. We only care about the bbox_left and bbox_right, since you are already doing a place check to make sure the rattlesnake is above the block. So what we want to do is check to see if the bbox_left of the rattlesnake is greater than the bbox_left of the block, and the bbox_right of the rattlesnake is less than the bbox_right of the block. If both of those things are true, we know the rattlesnake is standing within the bounds of the block (rather than only having a pixel over the block, or standing half on the block and half off):
Code:
var inst_break = instance_place(x, y-1, Elder_Rattleshell);
with (inst_break) {
   if (bbox_left > other.bbox_left && bbox_right < other.bbox_right) {
      event_user(1);
   }
}
 

TheouAegis

Member
Code:
var inst_break = instance_place(x, y-1, Elder_Rattleshell);
with (inst_break) {
   if abs(bbox_left - other.bbox_left) < (bbox_left - bbox_right)/2{
      event_user(1);
   }
}
 

Build Man

Member
I have been trying your examples of code, and they appear to make my code that hurts and defeats enemies upon floor destruction stop working entirely. The enemy just falls harmlessly on another solid surface.

@RefresherTowel, you have made an assumption that my enemy needs to be entirely within horizontal space of a destructible object to get hurt when said object gets destroyed. However, that is unnecessary, and even undesirable, as I may say, because an object in question has the same horizontal size as an enemy, meaning that the code suggested is impossible to activate. The fact that the enemy is constantly in motion makes it even less possible.

I do not want an enemy to be affected if it is just by a pixel within a horizontal space of an object, but I do want to check if it is at least by a half or more within it, and not entirely within. I have been trying to modify codes, but I could not modify them into something that works properly.

If anyone is willing to spare time, I can provide a prototype for you to research, assuming you have a copy of Game Maker 8. Otherwise, I can look at your code examples.
 

Nidoking

Member
I do not want an enemy to be affected if it is just by a pixel within a horizontal space of an object, but I do want to check if it is at least by a half or more within it, and not entirely within. I have been trying to modify codes, but I could not modify them into something that works properly.
Could you maybe explain this more clearly by writing it as a sequence of mathematical statements, in terms of sprite_width, bbox_left, bbox_right etc. for the various instances involved?

And then put those statements into if expressions, possibly with else ifs and &&/|| operators to connect them so that you get the effect you want?

And then put that in the game?
 

Build Man

Member
Could you maybe explain this more clearly by writing it as a sequence of mathematical statements, in terms of sprite_width, bbox_left, bbox_right etc. for the various instances involved?
This sounds like some advanced mathematics. Unfortunately, I am not that keen on mathematics.
 

Nidoking

Member
It's literally no more advanced than greater than and less than. I learned those in kindergarten, where we drew little Pac-Mans that always wanted to eat whichever number was larger.

And this statement:
I am not that keen on mathematics.
is not something that anyone who wants to make games should say. Math is essential for anything you want to do. If you're finding if statements and Boolean logic too complex for you, then I'm afraid you're not going to have much success in programming of any kind. You need to learn these concepts. This is not a suggestion. It is a requirement.
 
Top