# Spawning Coin(s) Problem?

4.2.2

### Question

Hi all,

I am spawning coins in my game. Everytime you run over a treasure chest that isn’t locked, or, when you unlock a “locked” treasure chest coins are spawned to whatever value: also a gem.

Gold coins worth 10 points, silver coins 1 point. 100 coins equals extra man. However, there is a problem and it is this, occasionally a coin will be spawned and end up in the terrain where it is ungettable and other times if it ends up on the ledge below it is also ungettable. I can’t figure out why this is, the coins have a mask with the terrain, so, to my thinking they shouldn’t be falling through the terrain, and not being able to pick up coins on the ledge below has me completely baffled as all the other coins are picked up just fine. This is an example of what happens:

If you need to see whatever piece of code - I’ll find and post it.

If you can help, here is a big thankyou in advance.

Cheers.

Yea how are you spawning the coins?

If you spawn a coin near a collision barrier the collision shape will try and expell the coin and could fall to a location that isn’t reachable. You should design your spawn to be away from other collision shapes.

You could do a intersect point physics query to see if something is their. But it would be simpler to just avoid putting them close to other collision objects.

The code for spawning is a little complicated for me, I was going to post a question on it, but, as I was writing the question, I figured the below, but a intersect point query is above where I’m up to.

Anyway, here is the code, please be kind .

``````extends Area2D

@export_category("Chest")
@export var is_open: bool = false;
@export_category( "Loot")
@export_range(1,99) var total_value: int = 1;

#-------------------------------------
@export var silver_coin	:	PackedScene
@export var gold_coin	: 	PackedScene
@export var blue_diamond:	PackedScene
#--------------------------------------

@onready var randomNumber: RandomNumberGenerator = RandomNumberGenerator.new(); #?!?

func plunder():
print("......PLUNDER......")
randomNumber.randf_range(5,10);
#+ Vector2.RIGHT* Globals.ppt * randomNumber.randf_range(-1,1))

while(total_value>10):
print ("greater than ten");
#make gold coin

#@export var particle_fx1:PackedScene
var gold_instance :RigidBody2D = gold_coin.instantiate();
gold_instance.apply_impulse(Vector2.UP * Globals.ppt * randomNumber.randf_range(5,10)
+ Vector2.RIGHT* Globals.ppt * randomNumber.randf_range(-1,1))
gold_instance.global_position = self.position;
#3gold_coin.instantiate();
#subract ten from amount
total_value -=10

while(total_value >0):
print("greater than one");
var silver_instance :RigidBody2D = silver_coin.instantiate();
silver_instance.freeze = false;

silver_instance.apply_impulse(
Vector2.UP * Globals.ppt * randomNumber.randf_range(5,10)
+ Vector2.RIGHT* Globals.ppt * randomNumber.randf_range(-1,1))
silver_instance.global_position = self.position;

#silver_coin.instantiate();
total_value -= 1;
#total value now equal zero - hopefully
``````

I should mention the function plunder is called by the animation tree when is_open is true, and value here is 22, two gold, two silver, it could be any number, though I did limit it to 1-99. The code for the gem is not important it doesn’t move at all, just appears.

Regards.

This should be `self.global_position`

Same here

The self.position could work okay in some cases but could act funny in different scenarios. This could explain some spawning issues.

Local position will be the position from its parent. But is this script the base of the chest object? And is the parent (I assume the level) at the global origin? Just use global position to be safe.

Other than that, I think your force you apply is reasonable and in the right direction.

If my one suggestion doesn’t fix your problem we may need to look at the coin and the ground it collides with. Small collision shapes, and the speed at which they move, could determine if they pass through a collision barrier.

1 Like

Ok, I made those 2 changes your suggested and it looks like the coins are always staying available. However it was at the expense of the impulse pushing the coins up and back down. So, I changed it back again and some of the coins went through the terrain again .

I will change the type of colliders in the original scenes and see if that makes a difference.

Changing the collision shape to square and putting more on the underside seems to have REALLY made a difference - 7 run-thru’s and none dropped through.

Seeing as they are rigidbody2D maybe they were pushing coins underneath through the terrain - but, so, far it has been a marked improvement

Thankyou for the help.

1 Like

Maybe, I wonder what shape you were using before? Changing the shape would potentially change how it calculates a collision.

There are two phases to collisions. One is a broad phase that tests how close objects to each other. This will make lists of potential collisions. I think then there is a near phase where the AABB bounding boxes and margins come into play. After this point and objects are close enough collisions should be checked every frame and potentially every subframe of the physics engine.

Even so, if a body is moving fast enough each step of the physics engine it could “clip” through another collision body. Since these were small coins it could be their center of mass passed far enough into the ground collider to cause it to fully push through.

I bet just increasing the AABB could theoretically fix this as well.

I was originally using a circle collider as close to the outline of the coin as I could, I thought make the collider as small as possible to improve the calculation time. But with your input I decided to make it square it improved it somewhat. But when I increased the size on the underside of the coin it w getting better and better to the point where it hasn’t happened since. And if it does I am much happier with it now, it was occurring too much the way it was - it’s something I can live with.

The game will never be published or see the light of day, so, it’s cool .

Thankyou and Regards.