❔ Godot - Collision isn't detected

Hi Asteroid.cs:
using System;
using Godot;
using Technospace.Extensions;

namespace Technospace;

public partial class Asteroid : Area2D
{
private static readonly string[] Textures = new string[]
{
"asteroid1.png",
"asteroid2.png",
"asteroid3.png",
"asteroid4.png",
"asteroid5.png"
};

// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
var sprite = new Sprite2D();
sprite.Texture = GD.Load<Texture2D>($"res://sprites/{Textures.PickRandom()}");
AddChild(sprite);
var collision = new AsteroidCollision();
AddChild(collision);
BodyEntered += body => GD.Print(body); // Event is not fired on collision
var dim = GetNode<Root>("/root/Root").ScreenDimensions;
var random = new Random();
var t = Transform;
t.Origin = new Vector2(random.Next((int)-(dim.X / 2), (int)(dim.X / 2)), -(dim.Y / 2));
Transform = t;
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
var t = Transform;
t.Origin = new Vector2(t.Origin.X, t.Origin.Y + (float)delta * 500);
Transform = t;
}
}
using System;
using Godot;
using Technospace.Extensions;

namespace Technospace;

public partial class Asteroid : Area2D
{
private static readonly string[] Textures = new string[]
{
"asteroid1.png",
"asteroid2.png",
"asteroid3.png",
"asteroid4.png",
"asteroid5.png"
};

// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
var sprite = new Sprite2D();
sprite.Texture = GD.Load<Texture2D>($"res://sprites/{Textures.PickRandom()}");
AddChild(sprite);
var collision = new AsteroidCollision();
AddChild(collision);
BodyEntered += body => GD.Print(body); // Event is not fired on collision
var dim = GetNode<Root>("/root/Root").ScreenDimensions;
var random = new Random();
var t = Transform;
t.Origin = new Vector2(random.Next((int)-(dim.X / 2), (int)(dim.X / 2)), -(dim.Y / 2));
Transform = t;
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
var t = Transform;
t.Origin = new Vector2(t.Origin.X, t.Origin.Y + (float)delta * 500);
Transform = t;
}
}
AsteroidCollision.cs:
using Godot;

namespace Technospace;

public partial class AsteroidCollision : CollisionShape2D
{
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
var shape = new RectangleShape2D();
shape.Size = new Vector2(32, 32);
Shape = shape;
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
}
using Godot;

namespace Technospace;

public partial class AsteroidCollision : CollisionShape2D
{
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
var shape = new RectangleShape2D();
shape.Size = new Vector2(32, 32);
Shape = shape;
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
}
Asteroids are generated through script, but unfortunetly no BodyEntered event is fired.
5 Replies
Murten
Murten9mo ago
You'll probably be better off asking this in a Godot focused discord server.
Thinker
Thinker9mo ago
$godot
Murten
Murten9mo ago
I don't have a PhD in discord bottics but this ig ^
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.