반응형

유니티 2D Sprite 및 Tile Palette의 각 tile마다 물리적 충돌과 그림자 적용을 위한 Physics Shape을 설정할 수 있다.

 

스프라이트를 임포트하면 Inspector - Sprite Mode - Generate Physics Shape 설정을 체크해서 Physics Shape 설정을 하지 않은 스프라이트에 대해 기본 Physics Shape을 생성하게 할 수 있다. 하지만 이 설정은 shape이 임의로 정해진다.

 

원하는 Physics Shape 설정을 위해 Sprite에서 Open Sprite Editor 클릭 - Sprite Editor를 Custom Physics Shape으로 바꾼다

 

아래 Outline Tool에서 Generate 클릭. (아니면 원하는 Tile에서 클릭&드래그로 Physics Shape을 만들 수 있다)

 

원하는 형태로 shape을 변경한다. shape은 여러 개 만들 수 있다. 여기서 만드는 Physics Shape은 그림자 뿐만 아니라 충돌 처리를 위한 경계로도 사용된다.

 

위 그림처럼 필요에 맞게 변경한다.

 

타일맵은 Tilemap Collider 2D를 추가하고 Composite Operation을 Merge로 바꾼다. Composite Collider 2D를 추가하고, 같이 추가되는 Rigidbody 2D에서 Body Type을 Static으로 바꾼다. (Body Type이 Dynamic으로 되어 있으면 게임 실행 시 밑으로 떨어진다)

 

Shadow Caster 2D를 추가하고 Casting Source를 Composite Collider 2D로 바꾼다. (Composite Collider 2D 관련 과정은 충돌 계산 및 그림자 효과를 효율적으로 만들기 위한 것이다. 그냥 Tilemap Collider 2D와 Shadow Caster 2D만 추가해서 Shadow Caster 2D의 Casting Source를 Tilemap Collider 2D로 바꾸고 사용해도 문제는 없다)

 

만약 위 과정을 진행 하고나서 나중에 Sprite의 Physics Shape을 바꾸면 이미 만들어져 있는 Tilemap에서는 바뀐 Physics Shape이 바로 적용되지 않는다. 이때는 Tilemap Collider 2D를 Reset한다. (그러면 Composite Operation을 다시 Merge로 바꿔줘야 한다)

 

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

기본적인 2D 캐릭터 컨트롤러. 2D 테스트에 활용 할 수 있다.

빈 오브젝트에 스프라이트 렌더러 정도만 추가하고 사용하면된다.

 

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
using UnityEngine;
 
[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(CapsuleCollider2D))]
[RequireComponent(typeof(PlatformEffector2D))]
 
public class CharacterController2D : MonoBehaviour
{
    public float maxSpeed = 3.0f;
    public float jumpHeight = 6.0f;
    public float gravityScale = 1.5f;
 
    float moveDirection = 0.0f;
    float collisionCheckRadius = 0.0f;
    bool facingRight = true;
    bool isGrounded = false;
    bool isJumping = false;
 
    Rigidbody2D rigidBody;
    CapsuleCollider2D mainCollider;
    PlatformEffector2D platformEffector;
 
    Vector3 cameraPos;
    public Camera mainCamera;
 
    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D>();
        mainCollider = GetComponent<CapsuleCollider2D>();
        platformEffector = GetComponent<PlatformEffector2D>();
 
        rigidBody.freezeRotation = true;
        // Freeze rotation: Prevents the Rigidbody2D from rotating due to collisions or physics forces.
        rigidBody.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
        // Collision Detection Mode: Continuous - Rigidbody2D will use continuous collision detection
        // to prevent fast-moving objects from passing through colliders.
        rigidBody.gravityScale = gravityScale;
 
        mainCollider.usedByEffector = true;
        platformEffector.useOneWay = false;
        platformEffector.useSideFriction = false;
        // PlatformEffector없이 Collider만 사용하면 캐릭터가 점프 후 벽에 붙는 효과가 발생한다. 마찰력 때문이다.
        // platformEffector.useSideFriction을 false로 설정하면 벽에 붙는 효과를 제거할 수 있다.
 
        facingRight = transform.localScale.x > 0.0f;
        collisionCheckRadius = mainCollider.size.x * 0.6f * Mathf.Abs(transform.localScale.x);
 
        if (mainCamera)
        {
            cameraPos = mainCamera.transform.position;
        }
    }
 
    void Update()
    {
        //if ((Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) && (isGrounded || Mathf.Abs(rigidBody.linearVelocity.x) > 0.01f))        
        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)) // 위 조건문으로 변경하면, 점프만 했을때 좌우로 움직이지 않게 된다.
        {
            moveDirection = Input.GetKey(KeyCode.A) ? -1 : 1;
        }
        else if (isGrounded || rigidBody.linearVelocity.magnitude < 0.01f)
        {
            moveDirection = 0.0f;
        }
 
        // Jumping logic. 키 입력 확인이 FixedUpdate()에서 처리되면 제대로 감지되지 않을때가 있다.
        if (Input.GetKeyDown(KeyCode.W) && isGrounded)
        {
            isJumping = true;
        }
 
        // Change facing direction
        if (moveDirection != 0)
        {
            if (moveDirection > 0 && !facingRight)
            {
                facingRight = true;
                transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
            }
            else if (moveDirection < 0 && facingRight)
            {
                facingRight = false;
                transform.localScale = new Vector3(-Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
            }
        }
    }
 
    void FixedUpdate()
    {
        Vector3 groundCheckPos = mainCollider.bounds.min + new Vector3(mainCollider.size.x * 0.5f, mainCollider.size.x * 0.5f, 0.0f);
 
        // Check if player is grounded. OverlapCircleAll(): Get a list of all Colliders that fall within a circular area.
        Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheckPos, collisionCheckRadius);
 
        isGrounded = false;
 
        // 플레이어 콜라이더가 아니면 벽 또는 바닥 콜라이더라 가정하고 점프 할 수 있도록 충돌체크한다.
        // 벽을 타고 멀티 점프가 가능하다.
        if (colliders.Length > 0)
        {
            for (int i = 0; i < colliders.Length; i++)
            {
                if (colliders[i] != mainCollider)
                {
                    isGrounded = true;
                    break;
                }
            }
        }
 
        if (isJumping)
        {
            // Apply jump force            
            rigidBody.linearVelocity = new Vector2(rigidBody.linearVelocity.x, jumpHeight);
            //rigidBody.AddForce(new Vector2(0.0f, jumpHeight * rigidBody.mass), ForceMode2D.Impulse);
            isJumping = false;
        }
        else
        {
            // Apply movement velocity
            rigidBody.linearVelocity = new Vector2(moveDirection * maxSpeed, rigidBody.linearVelocity.y);
        }
 
        // OverlapCircleAll() 이 커버하는 범위를 시각적으로 표시하기 위해 디버그 라인 그리기.
        Debug.DrawLine(groundCheckPos, groundCheckPos - new Vector3(0.0f, collisionCheckRadius, 0.0f), isGrounded ? Color.green : Color.red);
        Debug.DrawLine(groundCheckPos, groundCheckPos + new Vector3(collisionCheckRadius * (facingRight ? 1 : -1), 0.0f, 0.0f), isGrounded ? Color.green : Color.red);
    }
 
    private void LateUpdate()
    {
        // Ensure the camera follows the player smoothly
        if (mainCamera)
        {
            // 카메라의 y, z 좌표는 고정하고, x 좌표만 플레이어의 x 좌표로 설정.
            mainCamera.transform.position = new Vector3(transform.position.x, cameraPos.y, cameraPos.z);
        }
    }
}
 

 

 

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

2D 안개 효과를 만들어 보자.

 

ParallaxBackground를 추가하고 Layer를 높은 숫자로 바꾼다.

 

ParallaxLayer를 추가하고 Mirroring을 뷰포트와 동일하게 설정한다.

 

ColorRect를 추가하고 Size를 뷰포트와 동일하게 설정한다.

 

ColorRect에 ShaderMaterial을 추가하고 Shader를 생성한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
shader_type canvas_item;
//render_mode unshaded; // optional
 
// Noise texture
uniform sampler2D noise_texture: repeat_enable, filter_nearest;
// Fog density
uniform float density: hint_range(0.01.0= 0.25;
// Fog speed
uniform vec2 speed = vec2(0.020.01);
 
// Called for every pixel the material is visible on
void fragment() {
    // Make the fog slowly move
    vec2 uv = UV + speed * TIME;
    // Sample the noise texture
    float noise = texture(noise_texture, uv).r;
    // Convert the noise from the (0.0, 1.0) range to the (-1.0, 1.0) range
    // and clamp it between 0.0 and 1.0 again
    float fog = clamp(noise * 2.0 - 1.00.01.0);
    // Apply the fog effect
    COLOR.a *= fog * density;
}
 

 

셰이더는 위와 같이 작성한다.

 

Shader Parameters - Noise Texture에 Noise Texture 2D를 추가한다.

 

사이즈는 뷰포트와 동일하게, Seamless 옵션 체크, Noise - FastNoiseLite를 추가한다.

 

적당한 주인공 스프라이트를 추가한다.

 

여러가지 옵션을 변경하며 적당한 안개 효과를 찾는다.

 

float fog = clamp(noise * 2.0 - 1.0, 0.0, 1.0);

필요하다면 셰이더 코드에서 2.0 이라는 수치를 적당히 변경해 보자.

 

※ 참고

2D fog overlay

 

반응형
Posted by J-sean
:

[Godot] Path2D PathFollow2D

Godot 2024. 2. 22. 22:10 |
반응형

정해진 길을 따라 움직이는 오브젝트를 만들어 보자.

 

Path2D를 추가하고 툴 버튼을 이용해 path를 그린다.

 

PathFollow2D, Sprite2D를 추가한다. PathFollow2D는 Path2D의 자식 노드이어야 한다.

 

Node2D에 스크립트를 추가한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using Godot;
 
public partial class Control : Node2D
{
    public PathFollow2D follower;
 
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        follower = GetChild<Path2D>(0).GetChild<PathFollow2D>(0);
    }
 
    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
        follower.ProgressRatio += (float)delta;
    }
}
 

 

위와 같이 코드를 작성한다.

 

실행하면 스프라이트가 Path를 따라 움직인다.

 

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

2D 씬에 3D 오브젝트를 표현해 보자.

 

2D 씬에 스프라이트를 생성하고 Texture는 ViewportTexture를 지정한다.

 

SubViewport를 생성한다.

 

3D 씬에 Torus, Camera, Light를 생성하고 적당히 배치해 저장한다.

 

저장한 3D 씬을 SubViewport 자식 노드로 추가한다.

 

스프라이트 Texture - Viewport Path에 SubviewPort를 지정한다.

 

위와 같이 3D 카메라에 보이는 화면이 텍스쳐로 표현된다.

 

Transparent BG 옵션을 선택하면 배경이 투명해진다.

 

3D 오브젝트에 스크립트를 추가해 움직임을 적용하면 스프라이트에 반영된다.

 

반응형
Posted by J-sean
:

[Godot] Line2D 선 그리기

Godot 2024. 2. 21. 18:02 |
반응형

간단한 선을 그려보자.

 

Line2D 노드 두 개와 스크립트를 추가한다.

 

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
using Godot;
using System.Collections.Generic;
 
public partial class Control : Node2D
{
    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        DrawCircle();
        DrawPentagram();
    }
 
    // 원 그리기
    public async void DrawCircle()
    {
        Line2D line = GetNode<Line2D>("Line2D");
        //line.Closed = true;
        // Closed를 여기서 변경하면 원이 그려지는 방식이 달라진다.
        line.Width = 2;
        line.DefaultColor = Colors.Red;
        line.Position = new Vector2(200200);
 
        float radius = 100;
        float angle;
 
        for (int i = 0; i < 360; i++)
        {
            angle = Mathf.DegToRad(i);
            line.AddPoint(CalcCirclePoint(radius, angle));
            await ToSignal(GetTree().CreateTimer(0.01), SceneTreeTimer.SignalName.Timeout);
        }
 
        line.Closed = true;
    }
 
    // 원 포인트 계산
    public Vector2 CalcCirclePoint(float radius, float angle)
    {
        float x = Mathf.Cos(angle);
        float y = Mathf.Sin(angle);
 
        return new Vector2(x * radius, y * radius);
    }
 
    // 별 그리기
    public async void DrawPentagram()
    {
        Line2D line = GetNode<Line2D>("Line2D2");
        line.Width = 2;
        line.DefaultColor = Colors.Blue;
        line.Position = new Vector2(200200);
 
        List<Vector2> points = new List<Vector2>();
        points.Add(new Vector2(6080));
        points.Add(new Vector2(0-100));
        points.Add(new Vector2(-6080));
        points.Add(new Vector2(95-25));
        points.Add(new Vector2(-95-25));
        //points.Add(new Vector2(60, 80));
        // 마지막에 Closed 프로퍼티에 true를 대입하기 때문에 별의 시작점을 다시 추가할
        // 필요는 없다.
 
        for (int i = 0; i < points.Count; i++)
        {
            line.AddPoint(points[i]);
            await ToSignal(GetTree().CreateTimer(1), SceneTreeTimer.SignalName.Timeout);
        }
 
        line.Closed = true;
    }
}
 

 

위와 같이 코드를 작성한다.

 

 

※ 참고

Line2D

Custom drawing in 2D

 

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

AnimatedSprite나 AnimationPlayer를 사용하지 않고 Sprite만으로도 간단한 애니메이션을 만들 수 있다.

 

Sprite2D를 추가하고 Texture를 지정한다. Hframes는 8, Vframes는 7로 조절한다.

 

Scale을 적절히 조절하고 스크립트도 추가한다.

 

char_blue.png
0.02MB

 

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
using Godot;
 
public partial class Control : Sprite2D
{
    public override async void _Process(double delta)
    {
        if (Input.IsActionJustPressed("ui_accept"))
        {
            // 점프 애니메이션
            for (int i = 32; i < 40; i++)
            {
                await ToSignal(GetTree().CreateTimer(0.2),
                    SceneTreeTimer.SignalName.Timeout);
                Frame = i;
            }
        }
 
        if (Input.IsActionJustPressed("ui_cancel"))
        {
            // 전체 애니메이션
            for (int v = 0; v < Vframes; v++)
                for (int h = 0; h < Hframes; h++)
                {
                    await ToSignal(GetTree().CreateTimer(0.2),
                        SceneTreeTimer.SignalName.Timeout);
                    FrameCoords = new Vector2I(h, v);
                }
        }
    }
}
 

 

위와 같은 코드를 작성한다.

엔터키를 누르면 점프, ESC키를 누르면 전체 애니메이션이 플레이 된다. 애니메이션이 플레이 되는 동안 키를 여러번 동시에 누르면 애니메이션이 겹친다.

 

 

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

 

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
: