Dynamic collision creation

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By erasmo85
:warning: Old Version Published before Godot 3 was released.

Hi everybody,
recently I started to use Godot Engine, which I like it a lot, so I dived myself completely into tutorials, demo projects and snippets. I was following the SpaceBlaster84 tut, and I got to the 11th chapter, where I decided to create the rocks dynamically, ie having just one scene to create the various rocks instead of having three with just different sizes.

My Rock scene is set like this:
Rock_root (Type: Node)
–RigidBody2D
----Sprite
----CollisionShape2D

I have this piece of code from the script of the RigidBody2D:

func setRock(size):
	var circleShape = CircleShape2D.new()
	if size == 1: #Large
		durability = 3
		points = 30
		get_node("Sprite").set_texture(rockL)
		circleShape.set_radius(80)
		circleShape.set_custom_solver_bias(0.0)
		get_node("CollisionShape2D").set_shape(circleShape)
	elif size == 2: #Medium
		durability = 2
		points = 20
		get_node("Sprite").set_texture(rockM)
		get_node("Sprite").set_scale(Vector2(1.5,1.5))
		circleShape.set_radius(45)
		circleShape.set_custom_solver_bias(0.0)
		get_node("CollisionShape2D").set_shape(circleShape)
		get_node("CollisionShape2D").set_pos(Vector2(4,-0.5))
	elif size == 3: #Small
		durability = 1
		points = 10
		get_node("Sprite").set_texture(rockS)
		get_node("Sprite").set_scale(Vector2(1.5,1.5))
		circleShape.set_radius(23)
		circleShape.set_custom_solver_bias(0.0)
		get_node("CollisionShape2D").set_shape(circleShape)

which works just fine with the images but doesn’t with the collisions.
Setting the shape (say, a circle one) for the CollisionShape2D element works fine, but what if I want a rectangular shape or anything?
So the question is: how can I make the collision (or the shape, or whatever) work in order to… destroy those pesky rocks (or let the rocks destroy the ship)? :smiley:

Thanks in advance for the help!

Format correctly the code snippet would help readability. Please use the { } icon with the selected code piece. Thanks

genete | 2016-05-30 22:13

in that episode, FOS365 says “let’s put everything together”… is your script assigned to the correct node and everything? what’s the thing isn’t working for you?

shackra | 2016-05-31 05:13

Yep, the script is assigned to the RigidBody2D element, everything works fine except for the collision between the rocks and the laser or the ship.

erasmo85 | 2016-05-31 13:50

:bust_in_silhouette: Reply From: batmanasb

The way you are using the CollisionShape2D node is a little weird. That node is a helper node and doesn’t exist in the game. Basically nodes like RigidBody2D have a list of shapes, and all the helper node does is add one at runtime, it’s basically a visual way of adding a shape.

So instead of getting the CollisionShape2D node and using .set_shape(), I’d recommend just getting the RigidBody2D node and using .add_shape(). Then remove the CollisionShape2D node.

And if you want different types of shapes, simply create them and set their properties. An example for making a box shaped asteroid would be like:

var shape = RectangleShape2D.new()
shape.set_extents(Vector2(64,64))
get_node("RigidBody2D").add_shape(shape)
#note that the box shape is now the 0th index in the list
#using .remove_shape(0) will remove it

Use the “Search Help” section in the script tab to find different kinds of shapes and what properties they have.

Later I will give it a try and I’ll tell you, thanks for the answer!

erasmo85 | 2016-05-31 13:53

It worked flawlessly, thanks a lot!! :smiley:

erasmo85 | 2016-06-01 00:06

:bust_in_silhouette: Reply From: genete

So the question is: how can I make the collision (or the shape, or
whatever) work in order to… destroy those pesky rocks (or let the
rocks destroy the ship)? :smiley:

Please take a look the code of this project. Specially the laser_body.gd and the scripts of the enemies.

It wasn’t exactly what I was looking for, but thanks anyway for the help :slight_smile:

erasmo85 | 2016-06-01 13:59