-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomColliders.cs
38 lines (35 loc) · 974 Bytes
/
RandomColliders.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using Godot;
using System;
public class RandomColliders : Node2D
{
public override void _Ready()
{
for (int i = 0; i < 10; ++i) {
Vector2 offset = new Vector2(0, -256);
offset += Random2D.PointInCircle(0.0f, 256.0f);
SpawnRandomBody(offset);
}
}
public void SpawnRandomBody(Vector2 offset)
{
RigidBody2D body = new RigidBody2D();
ConvexPolygonShape2D shape = new ConvexPolygonShape2D();
shape.Points = GetRandomPolygon();
Physics2DServer.BodyAddShape(body.GetRid(), shape.GetRid());
VisualShape2D visual = new VisualShape2D();
visual.Shape = shape;
visual.Modulate = Godot.Random.Color;
body.AddChild(visual);
AddChild(body);
body.Position = offset;
}
public Vector2[] GetRandomPolygon()
{
int sides = (int)Godot.Random.Range(3, 10);
Vector2[] polygon = GoostGeometry2D.RegularPolygon(sides, 64.0f);
for (int i = 0; i < polygon.Length; ++i) {
polygon[i] += Random2D.PointInCircle(5.0f, 20.0f);
}
return polygon;
}
}