Ⰽⰰⱈⰻⰽⱄ
Ⰽⰰⱈⰻⰽⱄ
CC#
Created by Ⰽⰰⱈⰻⰽⱄ on 10/13/2023 in #help
❔ 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.
6 replies
CC#
Created by Ⰽⰰⱈⰻⰽⱄ on 7/20/2023 in #help
❔ How to fix encoding in Razor View?
10 replies