반응형

 

2023.10.18 - [Godot] - [Godot] 2D Splash Dynamic Wave 자연스러운 물결 파동 2. 구현

 

위 링크를 참고해 만들어진 물에 돌을 떨어뜨려 보자.

 

Stone씬을 만든다.

Stone(RigidBody2D) - Script(stone.cs)를 추가한다.

Sprite2D - Texture를 추가한다.

CollisionShape2D - Shape을 추가한다.

 

1
2
3
4
5
6
7
8
9
10
using Godot;
 
public partial class stone : RigidBody2D
{
    public override async void _Process(double delta)
    {
        await ToSignal(GetTree().CreateTimer(2.0), SceneTreeTimer.SignalName.Timeout);
        QueueFree();
    }
}
 

 

생성되고 2초 후 삭제되는 스크립트(stone.cs)를 작성한다.

 

Main씬에 스크립트(Main.cs)를 추가한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using Godot;
 
public partial class Main : Node2D
{
    public PackedScene Scene;
 
    public override void _Ready()
    {
        Scene = ResourceLoader.Load<PackedScene>("res://stone.tscn");
    }
 
    public override void _Input(InputEvent @event)
    {
        if (@event is InputEventMouseButton button)
            if (button.Pressed && button.ButtonIndex == MouseButton.Left)
            {
                RigidBody2D stone = Scene.Instantiate<RigidBody2D>();
                stone.Position = GetViewport().GetMousePosition();
                AddChild(stone);
            }
    }
}
 

 

왼쪽 마우스 버튼을 클릭하면 돌을 생성하는 스크립트(Main.cs)를 작성한다.

 

 

마우스를 클릭하면 돌이 생성되고 아래로 떨어진다.

 

spring씬에 Area2D와 CollisionShape2D를 추가한다.

CollisionShape2D - RectangleShape2D를 추가한다.

 

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;
 
public partial class spring : Node2D
{
    public float velocity;
    public float force;
    public float height;
    public float target_height;
 
    public CollisionShape2D collision;
 
    public void Initialize(int x_position)
    {
        Position = new Vector2(x_position, 100);
        height = Position.Y;
        target_height = Position.Y;
        velocity = 0;
 
        collision = GetNode<CollisionShape2D>("Area2D/CollisionShape2D");
    }
 
    public void water_update(float spring_constant, float dampening)
    {
        height = Position.Y;
        float x = height - target_height;
        float resist = -dampening * velocity;
        force = -spring_constant * x + resist;
 
        velocity += force;
        Position += new Vector2(0, velocity);
    }
 
    public void set_collision_width(int distance)
    {
        ((RectangleShape2D)collision.Shape).Size = new Vector2(distance,
            collision.Shape.GetRect().Size.Y);
    }
}
 

 

spring.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
public override void _Ready()
{
    k = 0.015f;
    d = 0.05f;
    spread = 0.0002f;
    passes = 8;
    depth = 1000;
    bottom = (int)GlobalPosition.Y + depth;
    water_polygon = GetNode<Polygon2D>("Polygon2D");
 
    springs = new List<spring>();
    spring_number = 10;
    distance_between_springs = 100;
    scene = ResourceLoader.Load<PackedScene>("res://spring.tscn");
    for (int i = 0; i < spring_number; i++)
    {
        Node2D s = scene.Instantiate<Node2D>();
        AddChild(s);
        springs.Add(s as spring);
 
        int x_position = distance_between_springs * i + 100;
        (s as spring).Initialize(x_position);
        (s as spring).set_collision_width(distance_between_springs);
    }
 
    wave_position = new Curve2D();
}
 

 

WaterBody.cs는 set_collision_width() 호출 코드만 추가하면 된다.

 

 

실행 시 Collision Shape이 보이도록 설정한다.

 

Collision Shape이 생성되고 표시된다.

 

Spring - Area2D - body_entered 시그널을 Spring 스크립트와 연결한다.

 

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Godot;
 
public partial class spring : Node2D
{
    public float velocity;
    public float force;
    public float height;
    public float target_height;
 
    public CollisionShape2D collision;
 
    public int index;
    public float stone_velocity_factor;
    [Signal]
    public delegate void SplashEventHandler(int index, float velocity);
    // index, stone_velocity_factor, Splash 시그널 추가
 
    public void Initialize(int x_position, int id)
    {
        Position = new Vector2(x_position, 100);
        height = Position.Y;
        target_height = Position.Y;
        velocity = 0;
 
        index = id;
        stone_velocity_factor = 0.02f;
        // index, stone_velocity_factor 초기화
 
        collision = GetNode<CollisionShape2D>("Area2D/CollisionShape2D");
    }
 
    public void water_update(float spring_constant, float dampening)
    {
        height = Position.Y;
        float x = height - target_height;
        float resist = -dampening * velocity;
        force = -spring_constant * x + resist;
 
        velocity += force;
        Position += new Vector2(0, velocity);
    }
 
    public void set_collision_width(int distance)
    {
        ((RectangleShape2D)collision.Shape).Size = new Vector2(distance,
            collision.Shape.GetRect().Size.Y);
    }
 
    public void OnArea2dBodyEntered(Node2D body)
    {
        float speed = (body as RigidBody2D).LinearVelocity.Y * stone_velocity_factor;
        // We need to disable the spring's collision so that we don't trigger the body enter
        // signal more than once.
        // Must be deferred as we can't change physics properties on a physics callback.        
        body.SetDeferred(CollisionShape2D.PropertyName.Disabled, true);
        // Signal
        EmitSignal(SignalName.Splash, index, speed);
    }
}
 

 

spring.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using Godot;
using System.Collections.Generic;
 
public partial class WaterBody : Node2D
{
    public float k;
    public float d;
    public float spread;
    public int passes;
    public List<spring> springs;
    public int spring_number;
    public int distance_between_springs;
    public int depth;
    public int target_height;
    public int bottom;
    public PackedScene scene;
    public Polygon2D water_polygon;
    public Curve2D wave_position;
 
    public override void _Ready()
    {
        k = 0.015f;
        d = 0.05f;
        spread = 0.0002f;
        passes = 8;
        depth = 1000;
        bottom = (int)GlobalPosition.Y + depth;
        water_polygon = GetNode<Polygon2D>("Polygon2D");
 
        springs = new List<spring>();
        spring_number = 10;
        distance_between_springs = 100;
        scene = ResourceLoader.Load<PackedScene>("res://spring.tscn");
        for (int i = 0; i < spring_number; i++)
        {
            Node2D s = scene.Instantiate<Node2D>();
            AddChild(s);
            springs.Add(s as spring);
 
            int x_position = distance_between_springs * i + 100;
            (s as spring).Initialize(x_position, i);
            // Index 추가
            (s as spring).set_collision_width(distance_between_springs);
            (s as spring).Splash += splash;
            // Signal 추가
        }
 
        wave_position = new Curve2D();
    }
 
    public override void _PhysicsProcess(double delta)
    {
        //if (Input.IsActionJustPressed("ui_accept"))
        //    splash(5, 20);
        // 테스트용 splash() 호출 삭제
 
        foreach (spring item in springs)
        {
            item.water_update(k, d);
        }
 
        List<float> left_deltas = new List<float>();
        List<float> right_deltas = new List<float>();
 
        for (int i = 0; i < springs.Count; i++)
        {
            left_deltas.Add(0.0f);
            right_deltas.Add(0.0f);
        }
 
        for (int p = 0; p < passes; p++)
        {
            for (int i = 0; i < springs.Count; i++)
            {
                if (i > 0)
                {
                    left_deltas[i] = spread * (springs[i].height - springs[i - 1].height);
                    springs[i - 1].velocity += left_deltas[i];
                }
                if (i < springs.Count - 1)
                {
                    right_deltas[i] = spread * (springs[i].height - springs[i + 1].height);
                    springs[i + 1].velocity += right_deltas[i];
                }
            }
        }
 
        draw_wave();
        draw_waterbody();
    }
 
    public void splash(int index, float speed)
    {
        if (index >= 0 && index < springs.Count)
        {
            springs[index].velocity += speed;
        }
    }
 
    public void draw_waterbody()
    {
        List<Vector2> surface_points = new List<Vector2>();
        Vector2[] baked_points = wave_position.GetBakedPoints();
        for (int i = 0; i < baked_points.Length; i++)
        {
            surface_points.Add(baked_points[i]);
        }
 
        int first_index = 0;
        int last_index = surface_points.Count - 1;
 
        surface_points.Add(new Vector2(surface_points[last_index].X, bottom));
        surface_points.Add(new Vector2(surface_points[first_index].X, bottom));
 
        water_polygon.Polygon = surface_points.ToArray();
    }
 
    public override void _Draw()
    {
        DrawPolyline(wave_position.Tessellate(), Colors.Blue, 5);
    }
 
    public void draw_wave()
    {
        wave_position.ClearPoints();
 
        Vector2 control0 = new Vector2(-500);
        Vector2 control1 = new Vector2(500);
 
        foreach (spring item in springs)
        {
            wave_position.AddPoint(item.Position, control0, control1);
        }
 
        QueueRedraw();
    }
}
 

 

WaterBody.cs를 위와 같이 수정한다.

 

마우스를 클릭하면 돌이 떨어지고 물결이 자연스럽게 파동친다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public Node2D collided_with = null;
 
public void OnArea2dBodyEntered(Node2D body)
{
    float speed = (body as RigidBody2D).LinearVelocity.Y * stone_velocity_factor;
 
    // We need to disable the spring's collision so that we don't trigger the body enter
    // signal more than once.
    // Must be deferred as we can't change physics properties on a physics callback.
    //body.SetDeferred(CollisionShape2D.PropertyName.Disabled, true);
    if (body == collided_with)
        return;
    else
        collided_with = body;
 
    // Signal
    EmitSignal(SignalName.Splash, index, speed);
}
 

 

spring.cs의 OnArea2dBodyEntered()를 위와 같이 바꾸면 SetDeferred()를 사용할 때 보다 훨씬 반응이 빠르고 자연스러운 파동이 만들어진다.

 

 

물의 높이는 spring.cs - Initialize() - Position 변수에서 간단히 변경할 수 있다.

 

Water.zip
10.27MB

전체 프로젝트 파일

 

※ 참고

Make a Splash With Dynamic 2D Water Effects

 

반응형
Posted by J-sean
:
반응형

프로젝트 세팅 창을 열려고 하면 작동을 멈추는 버그가 발생할 수 있다.

 

Project Settings... 를 클릭하면 그 다음부터 아무 동작도 하지 않는다.

 

프로젝트 폴더 - .godot 폴더를 삭제한다.

 

다른 문제가 발생할지 모르니 프로젝트 폴더는 미리 백업해 둔다.

작업중인 프로젝트 폴더의 .godot 폴더를 삭제하고 다시 실행한다. 그러면 Godot 엔진이 .godot 폴더를 다시 빌드하고 문제가 해결된다.

 

반응형
Posted by J-sean
:
반응형

베지어 스플라인 곡선을 그려보자.

 

Node2D를 생성하고 스크립트를 추가한다.

 

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
39
40
41
42
43
44
45
46
47
48
using Godot;
 
public partial class test : Node2D
{    
    private Curve2D pos;
    
    private Vector2 position0;
    private Vector2 position1;
    private Vector2 position2;
    private Vector2 position3;
    private Vector2 position4;
 
    private Vector2 control0;
    private Vector2 control1;
 
    public override void _Ready()
    {
        pos = new Curve2D();
 
        position0 = new Vector2(200200);
        position1 = new Vector2(400400);
        position2 = new Vector2(600200);
        position3 = new Vector2(800400);
        position4 = new Vector2(1000200);
 
        control0 = new Vector2(-1000);
        control1 = new Vector2(1000);
 
        BezierSpline(position0, position1, position2, position3,
            position4, control0, control1);
    }
 
    public override void _Draw()
    {
        //DrawPolyline(pos.GetBakedPoints(), Colors.Red, 5);
        DrawPolyline(pos.Tessellate(), Colors.Red, 5);
    }
 
    private void BezierSpline(Vector2 p0, Vector2 p1, Vector2 p2,
        Vector2 p3, Vector2 p4, Vector2 c0, Vector2 c1)
    {
        pos.AddPoint(p0, c0, c1);
        pos.AddPoint(p1, c0, c1);
        pos.AddPoint(p2, c0, c1);
        pos.AddPoint(p3, c0, c1);
        pos.AddPoint(p4, c0, c1);
    }
}
 

 

 

위와 같이 코드를 작성하고 실행한다.

_Draw()는 처음 한 번만 호출된다. 스플라인이 변경되거나 _Draw()를 다시 호출할 필요가 있다면 QueueRedraw()를 사용한다.

 

각 포인트에서 컨트롤이 적용된 스플라인이 표시된다.

 

※ 참고

Custom drawing in 2D

Beziers, curves, and paths

About Spline Interpolation

 

반응형
Posted by J-sean
:
반응형

 

2023.10.14 - [Godot] - [Godot] 2D Splash Dynamic Wave 자연스러운 물결 파동 1. 준비

 

위 링크를 참고해 준비가 끝난 씬에 Polygon2D를 이용해 물 모양을 추가해 보자.

 

WaterBody에 Polygon2D를 추가한다.

 

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using Godot;
using System.Collections.Generic;
 
public partial class WaterBody : Node2D
{
    public float k;
    public float d;
    public float spread;
    public int passes;
    public List<spring> springs;
    public int spring_number;
    public int distance_between_springs;
    public int depth;
    public int target_height;
    public int bottom;
    public PackedScene scene;
    public Polygon2D water_polygon;
 
    public override void _Ready()
    {
        k = 0.015f;
        d = 0.05f;
        spread = 0.0002f;
        passes = 8;
        depth = 1000;
        // 물의 깊이
        bottom = (int)GlobalPosition.Y + depth;
        // 물의 바닥 위치
        water_polygon = GetNode<Polygon2D>("Polygon2D");
        // Polygon2D 노드를 가져온다.
 
        springs = new List<spring>();
        spring_number = 10;
        distance_between_springs = 100;
        scene = ResourceLoader.Load<PackedScene>("res://spring.tscn");
        for (int i = 0; i < spring_number; i++)
        {            
            Node2D s = scene.Instantiate<Node2D>();            
            AddChild(s);            
            springs.Add(s as spring);
 
            int x_position = distance_between_springs * i + 100;
            (s as spring).Initialize(x_position);
        }
    }
 
    public override void _PhysicsProcess(double delta)
    {
        if (Input.IsActionJustPressed("ui_accept"))
            splash(520);
 
        foreach (spring item in springs)
        {
            item.water_update(k, d);
        }
 
        List<float> left_deltas = new List<float>();
        List<float> right_deltas = new List<float>();
 
        for(int i = 0; i < springs.Count; i++)
        {
            left_deltas.Add(0.0f);
            right_deltas.Add(0.0f);
        }
 
        for (int p = 0; p < passes; p++)
        {
            for (int i = 0; i < springs.Count; i++)
            {
                if (i > 0)
                {
                    left_deltas[i] = spread * (springs[i].height - springs[i-1].height);
                    springs[i-1].velocity += left_deltas[i];
                }
                if (i < springs.Count - 1)
                {
                    right_deltas[i] = spread * (springs[i].height - springs[i+1].height);
                    springs[i+1].velocity += right_deltas[i];
                }
            }
        }
 
        draw_waterbody();
        // 물 그리는 함수 호출.
    }
 
    public void splash(int index, float speed)
    {
        if(index >= 0 && index < springs.Count)
        {
            springs[index].velocity += speed;
        }
    }
 
    public void draw_waterbody()
    {
        List<Vector2> surface_points = new List<Vector2>();
        foreach (spring item in springs)
        {
            surface_points.Add(item.Position);
        }
        // 모든 스프링의 위치를 리스트에 추가한다.
 
        int first_index = 0;
        int last_index = surface_points.Count - 1;
 
        surface_points.Add(new Vector2(surface_points[last_index].X, bottom));
        surface_points.Add(new Vector2(surface_points[first_index].X, bottom));
        // 폴리곤을 사각형으로 만들기 위해 리스트 마지막에 사각형 밑변을 구성할 포인트를
        // 두 개 추가한다.
 
        water_polygon.Polygon = surface_points.ToArray();
        // 폴리곤을 구성하는 포인트 배열을 지정한다.
    }
}
 

 

 

WaterBody.cs 파일은 위와 같이 수정한다.

 

게임을 실행하고 엔터키를 누르면 폴리곤이 그려지고 스프링과 함께 물결처럼 운동한다.

 

좀 더 부드러운 물결을 만들어 보자.

 

WaterBody 씬에서 Polygon2D - Inspector - Visibility - Show Behind Parent 옵션을 체크한다.

 

 

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Godot;
using System.Collections.Generic;
 
public partial class WaterBody : Node2D
{
    public float k;
    public float d;
    public float spread;
    public int passes;
    public List<spring> springs;
    public int spring_number;
    public int distance_between_springs;
    public int depth;
    public int target_height;
    public int bottom;
    public PackedScene scene;
    public Polygon2D water_polygon;
    public Curve2D wave_position;
    // 부드러운 물결 곡선의 포인트를 저장할 Curve2D 변수
 
    public override void _Ready()
    {
        k = 0.015f;
        d = 0.05f;
        spread = 0.0002f;
        passes = 8;
        depth = 1000;
        bottom = (int)GlobalPosition.Y + depth;
        water_polygon = GetNode<Polygon2D>("Polygon2D");
 
        springs= new List<spring>();
        spring_number = 10;
        distance_between_springs = 100;
        scene = ResourceLoader.Load<PackedScene>("res://spring.tscn");
        for (int i = 0; i < spring_number; i++)
        {            
            Node2D s = scene.Instantiate<Node2D>();            
            AddChild(s);            
            springs.Add(s as spring);
 
            int x_position = distance_between_springs * i + 100;
            (s as spring).Initialize(x_position);
        }
 
        wave_position = new Curve2D();
        // 물결 곡선 Curve2D 초기화
    }
 
    public override void _PhysicsProcess(double delta)
    {
        if (Input.IsActionJustPressed("ui_accept"))
            splash(520);
 
        foreach (spring item in springs)
        {
            item.water_update(k, d);
        }
 
        List<float> left_deltas = new List<float>();
        List<float> right_deltas = new List<float>();
 
        for(int i = 0; i < springs.Count; i++)
        {
            left_deltas.Add(0.0f);
            right_deltas.Add(0.0f);
        }
 
        for (int p = 0; p < passes; p++)
        {
            for (int i = 0; i < springs.Count; i++)
            {
                if (i > 0)
                {
                    left_deltas[i] = spread * (springs[i].height - springs[i-1].height);
                    springs[i-1].velocity += left_deltas[i];
                }
                if (i < springs.Count - 1)
                {
                    right_deltas[i] = spread * (springs[i].height - springs[i+1].height);
                    springs[i+1].velocity += right_deltas[i];
                }
            }
        }
 
        draw_waterbody();
        draw_wave();
        // 부드러운 물결 그리기 함수 호출.
    }
 
    public void splash(int index, float speed)
    {
        if(index >= 0 && index < springs.Count)
        {
            springs[index].velocity += speed;
        }
    }
 
    public void draw_waterbody()
    {
        List<Vector2> surface_points = new List<Vector2>();
        foreach (spring item in springs)
        {
            surface_points.Add(item.Position);
        }
 
        int first_index = 0;
        int last_index = surface_points.Count - 1;
 
        surface_points.Add(new Vector2(surface_points[last_index].X, bottom));
        surface_points.Add(new Vector2(surface_points[first_index].X, bottom));
        
        water_polygon.Polygon = surface_points.ToArray();
    }
 
    public override void _Draw()
    {
        DrawPolyline(wave_position.Tessellate(), Colors.Blue, 5);
        // 물결 곡선을 그린다.
    }
 
    public void draw_wave()
    {
        wave_position.ClearPoints();
        // 매번 곡선의 포인트를 초기화 한다.
 
        Vector2 control0 = new Vector2(-500);
        Vector2 control1 = new Vector2(500);
        // 수평 컨트롤
 
        foreach (spring item in springs)
        {
            wave_position.AddPoint(item.Position, control0, control1);
            // 스프링 위치와 컨트롤을 곡선 포인트로 추가한다.
        }
 
        QueueRedraw();
        // _Draw() 호출
    }
}
 

 

 

WaterBody.cs 는 위와같이 변경한다.

 

spring 씬에서 Sprite2D - Inspector - Visibility - Visible 옵션을 해제한다.

 

Main 씬에서 실행한다.

 

폴리곤 위에 파란 물결이 부드러운 곡선으로 표시된다.

 

 

하지만 자세히 보면 폴리곤은 여전히 거칠다.

 

폴리곤을 부드럽게 바꿔보자.

 

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using Godot;
using System.Collections.Generic;
 
public partial class WaterBody : Node2D
{
    public float k;
    public float d;
    public float spread;
    public int passes;
    public List<spring> springs;
    public int spring_number;
    public int distance_between_springs;
    public int depth;
    public int target_height;
    public int bottom;
    public PackedScene scene;
    public Polygon2D water_polygon;
    public Curve2D wave_position;
 
    public override void _Ready()
    {
        k = 0.015f;
        d = 0.05f;
        spread = 0.0002f;
        passes = 8;
        depth = 1000;
        bottom = (int)GlobalPosition.Y + depth;
        water_polygon = GetNode<Polygon2D>("Polygon2D");
 
        springs= new List<spring>();
        spring_number = 10;
        distance_between_springs = 100;
        scene = ResourceLoader.Load<PackedScene>("res://spring.tscn");
        for (int i = 0; i < spring_number; i++)
        {            
            Node2D s = scene.Instantiate<Node2D>();            
            AddChild(s);            
            springs.Add(s as spring);
 
            int x_position = distance_between_springs * i + 100;
            (s as spring).Initialize(x_position);
        }
 
        wave_position = new Curve2D();
    }
 
    public override void _PhysicsProcess(double delta)
    {
        if (Input.IsActionJustPressed("ui_accept"))
            splash(520);
 
        foreach (spring item in springs)
        {
            item.water_update(k, d);
        }
 
        List<float> left_deltas = new List<float>();
        List<float> right_deltas = new List<float>();
 
        for(int i = 0; i < springs.Count; i++)
        {
            left_deltas.Add(0.0f);
            right_deltas.Add(0.0f);
        }
 
        for (int p = 0; p < passes; p++)
        {
            for (int i = 0; i < springs.Count; i++)
            {
                if (i > 0)
                {
                    left_deltas[i] = spread * (springs[i].height - springs[i-1].height);
                    springs[i-1].velocity += left_deltas[i];
                }
                if (i < springs.Count - 1)
                {
                    right_deltas[i] = spread * (springs[i].height - springs[i+1].height);
                    springs[i+1].velocity += right_deltas[i];
                }
            }
        }
 
        draw_wave();
        draw_waterbody();
        // draw_wave()가 먼저 호출되어야 한다.
    }
 
    public void splash(int index, float speed)
    {
        if(index >= 0 && index < springs.Count)
        {
            springs[index].velocity += speed;
        }
    }
 
    public void draw_waterbody()
    {
        List<Vector2> surface_points = new List<Vector2>();
        //foreach (spring item in springs)
        //{
        //    surface_points.Add(item.Position);
        //}
        Vector2[] baked_points = wave_position.GetBakedPoints();
        for (int i = 0; i < baked_points.Length; i++)
        {
            surface_points.Add(baked_points[i]);
        }
        // wave_position으로 만든 곡선의 포인트를 폴리곤 포인트로 사용하기 위해
        // 위와 같이 수정한다.
 
        int first_index = 0;
        int last_index = surface_points.Count - 1;
 
        surface_points.Add(new Vector2(surface_points[last_index].X, bottom));
        surface_points.Add(new Vector2(surface_points[first_index].X, bottom));
        
        water_polygon.Polygon = surface_points.ToArray();
    }
 
    public override void _Draw()
    {
        DrawPolyline(wave_position.Tessellate(), Colors.Blue, 5);
    }
 
    public void draw_wave()
    {
        wave_position.ClearPoints();
 
        Vector2 control0 = new Vector2(-500);
        Vector2 control1 = new Vector2(500);
 
        foreach (spring item in springs)
        {
            wave_position.AddPoint(item.Position, control0, control1);
        }
 
        QueueRedraw();
    }
}
 

 

 

WaterBody.cs를 위와같이 변경한다.

 

폴리곤도 부드럽게 바뀌었다.

 

2024.02.11 - [Godot] - [Godot] 2D Splash Dynamic Wave 자연스러운 물결 파동 3. 응용

 

※ 참고

Beziers, curves, and paths

Make a Splash With Dynamic 2D Water Effects

 

반응형
Posted by J-sean
:

[Godot] Wall Jump 벽 점프

Godot 2023. 10. 2. 16:53 |
반응형

벽을 타고 점프해 보자.

 

바닥, 벽, 캐릭터를 적당히 배치한다. 캐릭터에는 스크립트를 추가하자.

 

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
using Godot;
 
public partial class CharacterBody2D : Godot.CharacterBody2D
{
    public const float Speed = 300.0f;
    public const float JumpVelocity = -400.0f;
 
    public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle();
 
    public override void _PhysicsProcess(double delta)
    {
        Vector2 velocity = Velocity;
 
        if (!IsOnFloor())
            velocity.Y += gravity * (float)delta;
        
        if (Input.IsActionJustPressed("ui_accept"&& IsOnFloor())
            velocity.Y = JumpVelocity;
 
        if (Input.IsActionJustPressed("ui_accept"&& IsOnWallOnly())
            velocity.Y = JumpVelocity;
 
        Vector2 direction = Input.GetVector("ui_left""ui_right""ui_up""ui_down");
        if (direction != Vector2.Zero)
        {
            velocity.X = direction.X * Speed;
        }
        else
        {
            velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
        }
 
        Velocity = velocity;
        MoveAndSlide();
    }
}
 

 

 

IsOnWallOnly() 함수를 사용한 스크립트를 작성한다. 

 

벽을 타고 wall jump가 가능하다.

 

※ 참고

CharacterBody2D

Using CharacterBody2D/3D

 

반응형
Posted by J-sean
:
반응형

Instantiate로 생성한 씬을 반복 재사용해 보자.

 

스프라이트 하나를 생성하고 씬으로 저장(Player.tscn)한다.

 

res:// 에 스크립트를 생성하고 Autoload에 추가한다.

 

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
39
40
41
42
43
44
using Godot;
 
public partial class Control : Node
{
    public PackedScene Scene;
    public Timer timer;
    public Sprite2D Sprite;
 
    public override void _Ready()
    {
        // C# has no preload, so you have to always use ResourceLoader.Load<PackedScene>().
        Scene = ResourceLoader.Load<PackedScene>("res://Player.tscn");
        Sprite = Scene.Instantiate<Sprite2D>();
 
        timer = new Timer();
        timer.Connect("timeout", Callable.From(OnTimeOut));
        timer.WaitTime = 1.0;
        AddChild(timer);
        timer.Start();        
    }
    
    public override void _Process(double delta)
    {        
    }
 
    public void OnTimeOut()
    {
 
        Sprite.Position = new Vector2(GD.RandRange(0, (int)GetViewport().GetVisibleRect().Size.X),
            GD.RandRange(0, (int)GetViewport().GetVisibleRect().Size.Y));
        if (Sprite.GetParent() == null )
        {
            AddChild(Sprite);
            // Adds a child node. Nodes can have any number of children, but every child must have
            // a unique name. Child nodes are automatically deleted when the parent node is deleted,
            // so an entire scene can be removed by deleting its topmost node.
        }
        else
        {
            RemoveChild(Sprite);
            // Removes a child node. The node is NOT deleted and must be deleted manually.
        }
    }
}
 

 

 

RemoveChild()로 제거한 자식 노드는 삭제되지는 않는다. AddChild()로 다시 추가 할 수 있다.

 

게임을 실행하면 씬에 스프라이트가 반복해서 나타났다 사라진다.

 

게임이 실행된 상태에서 Remote 탭을 확인하면 GlobalControl의 자식 노드로 Sprite2D가 반복적으로 추가됐다 삭제 된다.

 

반응형
Posted by J-sean
:
반응형

코드로 캐릭터를 생성해 보자.

 

스프라이트를 준비하고 스크립트(Player.cs)를 추가한다.

 

타이머를 자식노드로 추가한다. Autostart를 체크하고 Wait Time은 5초로 설정한다.

 

timeout 시그널을 스프라이트에 연결한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using Godot;
 
public partial class Player : Sprite2D
{
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
    }
 
    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
    }
 
    public void OnTimerTimeout()
    {
        QueueFree();
    }
}
 

 

 

타임아웃 시그널이 발생하면 삭제 큐에 노드를 추가하는(QueuFree) Player.cs 스크립트를 작성한다.

 

 

res:// 에 스크립트(Controls.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
using Godot;
 
public partial class Control : Node
{
    public PackedScene Scene;
    public Timer timer;
 
    public override void _Ready()
    {
        // C# has no preload, so you have to always use ResourceLoader.Load<PackedScene>().
        Scene = ResourceLoader.Load<PackedScene>("res://Player.tscn");
        
        timer = new Timer();
        timer.Connect("timeout", Callable.From(OnTimeOut));
        timer.WaitTime = 1.0;
        AddChild(timer);
        timer.Start();
    }
    
    public override void _Process(double delta)
    {        
    }
 
    public void OnTimeOut()
    {
        Sprite2D Sprite = Scene.Instantiate<Sprite2D>();
        Sprite.Position = new Vector2(GD.RandRange(0, (int)GetViewport().GetVisibleRect().Size.X),
            GD.RandRange(0, (int)GetViewport().GetVisibleRect().Size.Y));
        AddChild(Sprite);
    }
}
 

 

 

Control.cs를 작성한다. _Ready()에서 타이머를 코드로 생성하고 타임아웃 시그널이 발생하면 씬을 생성한다.

 

Project Settings - Autoload 에 Controls.cs를 추가한다.

 

게임을 실행하면 캐릭터가 생성되고 잠시 후 사라진다.

※ 참고

Nodes and scenes

Callable

 

반응형
Posted by J-sean
:
반응형

게임 전체에서 간단히 사용할 수 있는 전역 클래스 멤버 변수를 만들어 보자.

 

전역 클래스 멤버 변수를 저장할 스크립트(Global.cs)를 생성한다. Template: Object: Empty

 

1
2
3
4
5
6
using Godot;
 
public partial class Global : Node
{
    public static int a = 10;
}
 

 

 

어떤 씬에도 소속되지 않은 스크립트(Global.cs)가 생성 된다.

 

노드를 하나 생성하고 전역 변수를 사용할 스크립트를 추가한다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using Godot;
 
public partial class Script : Sprite2D
{
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        GD.Print(Global.a);        
    }
 
    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
    }
}
 

 

 

Project Settings - Autoload 에 Global.cs 스크립트를 추가한다.

Autoload에 추가되는 스크립트나 씬은 게임이 시작되면 자동으로 로드된다.

 

게임을 실행하면 Output 창에 Global.a 값이 출력된다.

 

게임이 실행 중인 상태에서 Remote 탭을 확인하면 Global 스크립트가 root 노드 아래 로드되어 있다.

 

※ 참고

Singletons (Autoload)

 

반응형
Posted by J-sean
: