0 votes

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)? :D

Thanks in advance for the help!

in Engine by (27 points)
edited by

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

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?

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.

2 Answers

+3 votes
Best answer

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.

by (846 points)
selected by

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

It worked flawlessly, thanks a lot!! :D

0 votes

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)? :D

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

by (682 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.