How to avoid overlapping between pickups?

Hi there, I have a function which spawn coins in a “Box Collision” randomly. I am spawning 4 coins and sometimes they overlap each other as shown in attachment below.
How can I avoid this overlapping?

Solution 1
Use Spawn Actor node. Uncheck ‘Spawn even if colliding’. This will prevent spawing the actor if it collides with an existing actor.

Solution 2:
Another solution would be to divide the spawn area into a grid (each grid cell should be able to hold one coin). This division is not physical though :slight_smile:

For instance suppose each of your coin is of size (5x5x5 - size of collision volume). Then suppose the spawn volume is 100x100x100. This means the box volume can hold 20 coins in each direction (So total 202020). But if you only place coin on ground ant not in air, this will reduce to 20*20.

So you will number each grid in each direction (mentally) from 0 to 19 (like a matrix). When you want to spawn a coin, you will randomly pick two indices (X and Y). Then check if you have already placed a coin in it (do a trace). If yes, then find another pair of random indices.

Once you have found pair that leads to an empty cell, place the coin there.

Generalized (assuming you will place coins only on the ground)

Let

cX = X dimention of coin
cY = Y dimension of coin

bX = X dimension of spawn volume
bY = Y dimension of spawn colume

Total coins that can be placed in X direction, nX = bX / cX
Total coins that can be placed in Y direction, nY = bY / cY

Pick a random cell,

rX = rand(0, nX-1)
rY = rand(0, nY-1)

Relative location of random cell (wrt spawn volume origin - probably the center):
x = (rX * cX) - (bX/2) + cX/2
y = (rY * cY) - (bY/2) + cY/2

Global location of new cell
gX = boxvoulme.position.x + x
gY = boxvoulme.position.y + y

Check if there is any coin already placed at gX, gY (either do a box trace around this location. Or you could actualy hold an array that keeps track of where each coin is placed).

If no coins placed, put the coin at (gX, gY)

I am assuming all objects have their origin at their centre. IF that is not the case, you may have to modify the equations slightly.

Solution 3:
No grids. Instead when you have decided a place to put the coin, do a box collision from top at this location (size of box depends on the dimensions of the coin). If it hits another coin, select another location randomly. When you have found a free location, put the coin there. But you must make sure you limit the trails to 10 or 15. Becuase there is possibility that there is no more free spaces or it will take too much iterations to get a free space, if you don’t break after a few trials, this might lead to an infinite loop.

Sorry, not understood. It’s a side scroller (auto run) endless runner game.
Can you please teach me via blueprint?

I’m learning from this link: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

I think I can check after “Random Point in Bounding Box” that if at that location coin is present or not? But how to check it?

Do a box trace toward the random location from a point a few units above it. If it hits, check if the hit actor is a Coin.

To be honest, I’m new to this stuff. I searched on Google about this “Box Trace” but no good tutorial. I did this (shown in pic) but its not working. I know I did something or everything wrong. Can you please temme how to correct it? And I also don’t know about “Object Type”. It was giving me an error so I just stretch the node and there was only this arrey option.

When looking at your blueprint, I see a few problems.
When doing a box trace, you need to do it from above the random point. So add some value to Z axis and use this as start point. Next subtract a few units from Z axis and use this as End
Then you need non-zero Half-Size. Ideally this should be half the dimensions of your coin’s hit volume. Orientation should be same as the rotation of your about-to-be placed coin.

You also need to make use of the ‘Out Hit’ output pin. It is a struct of many important info about the trace. It will tell you which actor hit by the trace. Use this to see if the trace hit a Coin or something else. You will need to break the struct to use these additional info

However I do fell that you need to understand a few things about Blueprints (especially Casting, BP classes, objects…). Please go through the youtube tutorial series about Blueprints, if you haven’t already.

I wish I could show a blueprint, but unfortunately I wont be able to do that - I don’t have a PC with UE4 with me

okay. I watched all 9 videos and learned something new. But there was no information about my this problem. :frowning:

I set all XYZ +50 and -50 as show in pic. And it’s not working. And I don’t know how to get “Half Size” and how to use “Out Hit” node. Can you please help me further?

You can take a look here: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums
I did the same thing as he did. But I just don’t want overlapping.

Is there any simple way where I can check whether at location generated by “Random Point in Bounding Box” coin is present or not?

Trace is the simplest solution. If its not working, change ‘Draw Debug Type’ (on Trace node). This will show you exactly how the trace is done.

This should help you:
https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/UseRaycasts/Blueprints/index.html
https://docs.unrealengine.com/latest/INT/BlueprintAPI/Collision/BoxTracebyChannel/index.html

And this is how you break the Out Hit:

Okay. I selected “Persistent” mode. And now I know how it looks like. But now please understand what I did.

  1. Generate a Random location using “Random Point in Boundary Box”.

  2. +20 for Start and -20 for End along all XYZ axis.

  3. Manually entered the half size i.e. 20 along all axis.

  4. Selected Orientation as my coin is rotating.

And then I got the screen as shown below (Pic 2). But it always return false. Just forget the “Out Hit” for a moment. If there is something already present at that specified location then it should return “True”. But it is always returns false.

And since I’m doing this in a loop (4 times) there are more than 4 “Trace Boxes”.

Ok. I did little mistake in connected nodes. it shows true but only in starting. And these “Trace” box appears only for first ramp as show in pic.

P.S. Please read this and previous comment as well. :slight_smile:

I have two function which randomaly calls Blocker or Coins. I’m using box trace in “Spawn Coins” function.
Even when first ramp is “Blocker”, box trace always starts from first ramp. Since it is working for first ramp only, that’s why I’m not getting my desired result. Now can you please help me where I’m doing mistake?

Edit: Since I’m calling “Spawn Coins” from Construction Script. Might it be a problem here? Since it can spawn coins it should also generate “Trace Box”
Waiting for your reply.

At first look, I think this might be the problem. Your trace start and trace ends are a bit off. Trace start should be 20 units above the random point and trace end should be 5 points below the random point.

ie To get trace start, you should add 20 to the Z axis of random point and to get the end point, subtract 5 from the Z axis of random point. This will make sure the trace is vertical. So do not add anything to X or Y axes.

Unfortunately my Dev PC is suffering from hardware issues. Otherwise I could have shown you the blueprint itself. I hope to get a replacement tomorrow. In that case I will post a screenshot.

Another important thing:

Your SpawnCoin function uses the variable CoinArea to decide where to spawn the coin. From what I can see you have seperate Box volumes for each Ramp. But your function seems to always spawn inside whatever volume is pointed at by ‘CoinArea’. I think you need to change the Volume pointed at by ‘CoinArea’ to spawn inside another volume.

I did as you said, 20 and -5 to Z axes. But no progress.

I have 3 ramps.

  1. Normal Ramp.
  2. Normal Ramp + Upper Ramp
  3. Normal Ramp + Bridge

I created Blueprint for “Normal Ramp” and then used it to create other two ramps. Other two ramps doesn’t contain any “Coin Area”, just added additional ramp and everything else is inherited from “Normal Ramp” blueprint.

Normal ramp randomaly spawn either a blocker or coins, when it spawn coins it should do “Box Trace” but it is also doing box trace for blocker as well.

In simple words, it is “Box Tracing” only for first ramp, doesn’t matter it spawn Blocker or Coins.

And I’m uploading my “Coin Area” pic as well. It is just a simple line. I hope this screenshot might help you to understand and can you just edit my above screenshots in MS Paint and just draw box and text box to help me?

Can you upload images of SpawnCoin and SpawnBlocker functions ?

Here:

41987-unreal2.png

I could not see anything wrong.

Just to be clear, are you saying that the engine is doing box trace when it is spawning a blocker? Because I don’t see how that is possible. Your SpawnBlocker function contains no tracing.

Now we should make sure the code is going exactly like you planned. Add some print string nodes at the start of both functions ‘SpawnCoin’ and ‘SpawnBlocker’ to identify which function gets called and in what order.

Also how did you find out that a box trace was done when a blocker was spawned??

Ok. I just added “Print String” just after “Spawn Coins” & “Spawn Blocker” node. And result is confusing :frowning:

Can you upload your project file somewhere so that I can take a look? Also specify the engine version