I’m currently working on a project in Godot 4 and I need help with dynamically creating obstacles and implementing collision detection for them. I want to be able to detect collisions with these dynamically created obstacles.
Here are the key points of my question:
Dynamically Creating Obstacles:
I want to generate obstacles programmatically during runtime.
What is the best way to create obstacles dynamically in Godot 4?
Collision Detection:
I need to implement collision detection for these dynamically created obstacles.
How can I set up collision detection for dynamically generated objects?
Detecting Collisions:
Once collision detection is set up, how can I detect when other objects collide with these dynamically created obstacles?
Are there any specific methods or signals in Godot 4 that I should use for collision detection?
Code Snippets:
If possible, could someone provide code snippets or examples illustrating how to create obstacles dynamically and implement collision detection in Godot 4?
Any guidance, tips, or code examples would be greatly appreciated. Thank you in advance for your help!
I think it depends on what kind of objects you’re going to generate. If those obstacles are generic standalone objects, it’s entirely possible to create CollisionShape nodes on the go (I’m dealing with a similar situation, but my shapes are a bit more complex so I’m pre-generating them). For, say, a simple circle collision in 2D I’d do something like:
var circle = CircleShape2D.new()
circle.radius = 10.0 # probably your own variable here
var collision = CollisionShape2D.new()
collision.shape = circle
self.add_child(collision) #assuming this gets executed by your parent "obstacle"... it all really depends on your scene tree
Or rather, if there are dozens of similar instantiated rocks, I wouldn’t even create new Collision nodes each time, I’d just assign different shapes to a pre-created placeholder Collision.
From there on, collision detection is the same as with hand-placed CollisionShapes: you either make your “rocks” check for the character or vice versa.
I’m trying to do same kind of thing, i.e. when collision happens , I call a function which tries to create new blobs. Just learning , so not sure what to investigate next…
func createone (root, gap):
print("Creates a thing")
# from: https://forum.godotengine.org/t/dynamically-creating-obstacles-and-collision-detection-in-godot-4/39560/2
var curpos = position
var parent = CharacterBody2D.new()
var circle = CircleShape2D.new()
circle.radius = 10.0 # probably your own variable here
var blob = CollisionShape2D.new()
blob.shape = circle
blob.position.x = curpos.x # 100
blob.position.y = curpos.y - gap
blob.debug_color = Color(0.862745, 0.0784314, 0.235294, 1)
blob.visible = true
parent.add_child(blob)
root.add_child(parent,7)
+EDIT , collision does cause the createone function to run. With debug > Visible collison shapes I can the red circle on correct places, but not the actual thing. Do I have to create the whole sprite/coll shape branch here? Or add texture to the CharacterBody2d?