Combining multiple objects but detecting collisions as though it were a single object

R

Richard Horne

Guest
I'm creating dynamically sized objects of varying length. Each object has a head, as many repeated body sections as are required and a tail.

For instance, where ( is the head ---- are the body segments and the ) is the tail.

(------------------)
(-----------)
(--)
(------------------------------------)

I want to be able to detect collisions against the whole of the combined 'object' as though it were in fact a single object.

What sort of approach is best for this?

Is it possible to combine multiple instances of objects into one combined instance?
 

2Dcube

Member
Create an empty object and give it the 3 other objects as children (or make it the parent of each of the 3 objects, same thing).
Now you can check collisions against the parent object.
 
R

Richard Horne

Guest
That seems almost too easy. I'll give it a go tonight. Thanks, 2Dcube. :)
 
R

Richard Horne

Guest
That's not working I'm afraid.

I use the parent object to create instances of the child objects and get an error warning that:

PerformEvent recursion depth failure - check for infinite loops, check objects for parenting
at gml_Object_obj_start_PreCreate_0 (line 1) - event_inherited();
 

NightFrost

Member
Sounds like it migth be a parent loop of some sort - object A is parent of object B, and object B is parent of object A, or something similar. Check through all your parent-child settings.
 

FrostyCat

Redemption Seeker
That's not working I'm afraid.

I use the parent object to create instances of the child objects and get an error warning that:

PerformEvent recursion depth failure - check for infinite loops, check objects for parenting
at gml_Object_obj_start_PreCreate_0 (line 1) - event_inherited();
Don't do that. Parents are an "A is a B" relationship, not an "A uses B" relationship. If you make the parent object create instances of the child, the child would also have that creation code, and that's a classic infinite recursion.

This is the parent structure 2Dcube wanted you to use:
  • pobj_thing
    • obj_head
    • obj_body
    • obj_tail
Notice the following implementation notes:
  • The parent is a completely blank object, and no instance of it ever exists in any room.
  • The head, body and tail are all children of the parent.
  • Place instances of the head into the room, not the body or the tail, and especially not the parent.
  • The head creates subsidiary instances of the body and tail as needed.
  • Collision checks are made against the parent (which automatically also includes its children).
 
R

Richard Horne

Guest
Aaah thank you. That makes a little bit more sense though it doesn't quite seem logical and is certainly not how I'd have expected it to have worked. Let me give it a go now.
 
R

Richard Horne

Guest
OK so that works in terms of a combined collision mask, but now I want to move all of the three objects at the same time as though they were one object.

If I try and move the instance of the head I created, which then created the middle sections and tail, only the head moves.

For this to work with the rest of the project I need to be able to reference the whole thing as a single instance ID, check collisions using that ID, move it all using that single instance ID and destroy it all using that single ID.
 
Last edited by a moderator:

samspade

Member
These are very different questions. The answer to the first one depends entirely upon what you want it to do and move like. For example, move as if they were a large single sprite, move as if they were a snake, and so on.

The second is a little more straight forward but also depends upon what you want. Why, for example, do you want to refer to it by using a single instance id? Why are you checking collisions?

The simplest way that can work for most reasons I can think of is to save some sort of variable, most likely the instance_id of the head since you'll likely need to do that anyway, in each part of the body and tail that the head creates. Then when you do your collision check any portion of the body or tail can easily reference the head.
 
R

Richard Horne

Guest
Ok, see the below screenshot which gives you a bit more context.

https://pasteboard.co/Iu493TR.png

I'm making a rhythm game. Notes/buttons (A / B / X / Y) move towards the centre of the screen along the dotted lines in each of the 5 segments and you have to press the appropriate button when the note hits the blue centre circle. When you play a note or it collides with the red hexagon the note is destroyed.

I'm working on extended/longer notes which have to be held down. They are made up of the head, middle sections and the tail. I need them to move as one towards the centre. I need them to collide as one so I can detect when the button was held and I need them to be destroyed as one when the note is hit or missed.
 
Last edited by a moderator:
R

Richard Horne

Guest
Sorry, I'm almost using this thread as a live brainstorming session.

Thinking about it, I can't destroy extended notes in one go because if it's a really long note - say 2/3 seconds - it will need to be destroyed a little bit at a time rather than all at once.

Animated gif below showing current progress:

https://pasteboard.co/Iu4kvDV.gif
 

FrostyCat

Redemption Seeker
If this is your use case, I would not recommend using multiple objects. Instead I would recommend using one object and setting image_yscale to make the collision mask tall enough, then calling draw_sprite() to draw the head and tail and draw_sprite_ext() to draw the body.
 
Top