Removal of overlaping items

Aloha,

I am modifying a code that has drops items at a random x position. Occasionally 2 items will overlap and since I want to use physics that follow the shape of the image, these overlapping items sometimes get fused together. I know why this happens and that I would need to use a circle as my physics but I don’t want that type of physics.

So, I want to be able to remove one or both of the items that are overlapped. Maybe over a zone at the top of the screen.

Does anyone have any ideas on how to do this?

Mahalo,

Kaleo

When you spawn one object at a random x, can’t you just check that it’s sufficiently far from from the previous random object? And if it’s not, just pick another random x?

Hi Kaleo,

If you are using physics, you could use collision detection to check if the new object overlapped another. If this was true, you could pick another location, check for a collision on the next time step, and repeat until the object was “clear”. Does that help at all?

Best regards,

Brent

Since I’m new to Corona could you point me in the direction of tutorials or guide to do either idea? I have not worked with collisions yet and though I have been reading up on it I haven’t been able to grasp what I need to do.

The code I am working with is one I bought and am re-skining so even though I’ve been able to learn a lot from it and edit most of what I need this one issue is killing me. I have found out that 35 items need to be on the screen at all times so if you have 38 items and remove 9 then 6 item will drop immediately.  At the same time one item is always dropping and as that item hits the bottom then another spawns. The higher the level the faster that happens. This is were the problem is. When the 35 spawn the constant single spaws is still happening and thus 2 items can spawn in the same spot. I think if I stop the constant single spawn while the 35 drop then I won’t have a problem but I can’t figure out how to do this within the original code. Below is what I understand to be the code that drives the items spawning.

function Board:enterFrame(event)

    if self.runningScore < self.score then
        self.runningScore = self.runningScore + (self.score - self.runningScore) / 17
        if self.runningScore + 5 > self.score then
            self.runningScore = self.score
        end
        self.scoreText.text = math.floor(self.runningScore)
    end
   
if ((event.time - self.lastTime < self.speed) and (self.numOfItems > 35)) or (event.time - self.lastTime < 50) then
        return
end

    self.lastTime = event.time
    local id = math.random(1, self.numberItem)
    self:addItem(“item” … id)
end

function Board:addItems(number)
    for i = 1, number do
        local id = math.random(1, self.numberItem)
        self:addItem(“item” … id, 500)
    end
end

function Board:addItem(itemTitle, y)
    local x = math.random(106, 706)
    
    if y == nil then y = -100 end

    local item = Item({
        parent = self.itemLayer,
        board = self,
        title = itemTitle,
        id = self.itemCount,
        x = x,
        y = y,
    })
    self.items[item.id] = item
    self.itemCount = self.itemCount + 1
    self.numOfItems = self.numOfItems + 1
    print(self.itemCount)
    -------Continue to search for answers on spawning--------
    if itemTitle == self.holdTitle and self.mysticStickActive then
        item:active()
    end
end

I like both idea posted but I’m not sure how to approach doing them. Any guidance on executing those ideas or any new ideas based on the code provided?

Mahalo for your help,

Kaleo

If you’re not using physics you can use this method of collision detection to see when two objects are overlapping:
 

https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Rob

This code does use physics. In fact it uses PhysicsEditor to setup the objects. The original code had simple circles as it’s physics for each object so when 2 objects would spawn in the same area they would simply push away from each other. Since I need more contour shapes the original spawn style doesn’t work for me because the vectors occasionally locks them together. I already checked with PhysicsEditor and they said there’s nothing I can do about the physics as that is what they would expect for that to happen in this case.

Rob do you have a different method for me to do since this project need physics?

Mahalo,

Kaleo

I lowered the objects spawn y so that it happens on screen to see and better understand where the objects are spawning. They actually overlap majority of the time during spawn and push off each other most of the time. Now that I know this I tried removing as much vertex as I could with keeping the shapes I need and the locking happens a lot less now.

But since it still happens I still want to find a better solution.

@ Brent, I can now see that to use your idea I would need to detect collision in a zone below the spawn y so that the objects have a chance to push a way from each other and only the locked objects are dealt with.  Could someone explain or show me an example of how to do this?

@ Hasty, I think your idea can work too but I don’t know how to try that either. Could you explain how? The x range I have to work with is 106 to 706. Not a lot of room for spawn multiple objects at around 100 each in size but I’d still want to learn how to do this even if it’s not best for this one.

Kaleo

Hi Kaleo,

Are these physics objects of the “sensor” type? Sensor types should never push away from each other physically… they should overlap, but at the same time trigger a collision (assuming you don’t have collision filters set up to prevent that).

Brent

Hi Brent,

No they are not sensor type. That box was not checked on the PhysicsEditor file.

After writing the last post I tried setting the spawn y to a random value spread over 300 pixels so there is more room for spawning. I also swapped the object (a peanut) with the most complex shape from the initial spawn set with one that starts spawning at level 3. This way it isn’t part of the first 35 that spawn together, lowering the amount of times it spawns with other object. It’s still possible that the locking will happen but it is a whole lot less now.

Kaleo

Since they only drop one at a time, couldn’t you first spawn them somewhere high above the screen, like y = - 500, and then when you tell one to drop, first set its y value to - 100?

When you spawn one object at a random x, can’t you just check that it’s sufficiently far from from the previous random object? And if it’s not, just pick another random x?

Hi Kaleo,

If you are using physics, you could use collision detection to check if the new object overlapped another. If this was true, you could pick another location, check for a collision on the next time step, and repeat until the object was “clear”. Does that help at all?

Best regards,

Brent

Since I’m new to Corona could you point me in the direction of tutorials or guide to do either idea? I have not worked with collisions yet and though I have been reading up on it I haven’t been able to grasp what I need to do.

The code I am working with is one I bought and am re-skining so even though I’ve been able to learn a lot from it and edit most of what I need this one issue is killing me. I have found out that 35 items need to be on the screen at all times so if you have 38 items and remove 9 then 6 item will drop immediately.  At the same time one item is always dropping and as that item hits the bottom then another spawns. The higher the level the faster that happens. This is were the problem is. When the 35 spawn the constant single spaws is still happening and thus 2 items can spawn in the same spot. I think if I stop the constant single spawn while the 35 drop then I won’t have a problem but I can’t figure out how to do this within the original code. Below is what I understand to be the code that drives the items spawning.

function Board:enterFrame(event)

    if self.runningScore < self.score then
        self.runningScore = self.runningScore + (self.score - self.runningScore) / 17
        if self.runningScore + 5 > self.score then
            self.runningScore = self.score
        end
        self.scoreText.text = math.floor(self.runningScore)
    end
   
if ((event.time - self.lastTime < self.speed) and (self.numOfItems > 35)) or (event.time - self.lastTime < 50) then
        return
end

    self.lastTime = event.time
    local id = math.random(1, self.numberItem)
    self:addItem(“item” … id)
end

function Board:addItems(number)
    for i = 1, number do
        local id = math.random(1, self.numberItem)
        self:addItem(“item” … id, 500)
    end
end

function Board:addItem(itemTitle, y)
    local x = math.random(106, 706)
    
    if y == nil then y = -100 end

    local item = Item({
        parent = self.itemLayer,
        board = self,
        title = itemTitle,
        id = self.itemCount,
        x = x,
        y = y,
    })
    self.items[item.id] = item
    self.itemCount = self.itemCount + 1
    self.numOfItems = self.numOfItems + 1
    print(self.itemCount)
    -------Continue to search for answers on spawning--------
    if itemTitle == self.holdTitle and self.mysticStickActive then
        item:active()
    end
end

I like both idea posted but I’m not sure how to approach doing them. Any guidance on executing those ideas or any new ideas based on the code provided?

Mahalo for your help,

Kaleo

If you’re not using physics you can use this method of collision detection to see when two objects are overlapping:
 

https://coronalabs.com/blog/2013/07/23/tutorial-non-physics-collision-detection/

Rob

This code does use physics. In fact it uses PhysicsEditor to setup the objects. The original code had simple circles as it’s physics for each object so when 2 objects would spawn in the same area they would simply push away from each other. Since I need more contour shapes the original spawn style doesn’t work for me because the vectors occasionally locks them together. I already checked with PhysicsEditor and they said there’s nothing I can do about the physics as that is what they would expect for that to happen in this case.

Rob do you have a different method for me to do since this project need physics?

Mahalo,

Kaleo

I lowered the objects spawn y so that it happens on screen to see and better understand where the objects are spawning. They actually overlap majority of the time during spawn and push off each other most of the time. Now that I know this I tried removing as much vertex as I could with keeping the shapes I need and the locking happens a lot less now.

But since it still happens I still want to find a better solution.

@ Brent, I can now see that to use your idea I would need to detect collision in a zone below the spawn y so that the objects have a chance to push a way from each other and only the locked objects are dealt with.  Could someone explain or show me an example of how to do this?

@ Hasty, I think your idea can work too but I don’t know how to try that either. Could you explain how? The x range I have to work with is 106 to 706. Not a lot of room for spawn multiple objects at around 100 each in size but I’d still want to learn how to do this even if it’s not best for this one.

Kaleo

Hi Kaleo,

Are these physics objects of the “sensor” type? Sensor types should never push away from each other physically… they should overlap, but at the same time trigger a collision (assuming you don’t have collision filters set up to prevent that).

Brent

Hi Brent,

No they are not sensor type. That box was not checked on the PhysicsEditor file.

After writing the last post I tried setting the spawn y to a random value spread over 300 pixels so there is more room for spawning. I also swapped the object (a peanut) with the most complex shape from the initial spawn set with one that starts spawning at level 3. This way it isn’t part of the first 35 that spawn together, lowering the amount of times it spawns with other object. It’s still possible that the locking will happen but it is a whole lot less now.

Kaleo

Since they only drop one at a time, couldn’t you first spawn them somewhere high above the screen, like y = - 500, and then when you tell one to drop, first set its y value to - 100?