반응형

캐릭터에 이동에 관성을 적용해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
// Get the input direction and handle the movement/deceleration.
// As good practice, you should replace UI actions with custom gameplay actions.
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);
}
 

 

CharacterBody2D를 상속한 스크립트의 _PhysicsProcess()에는 위와 같은 키 입력 처리 코드가 있다.

 

1
2
3
4
5
6
7
8
9
10
11
// Get the input direction and handle the movement/deceleration.
// As good practice, you should replace UI actions with custom gameplay actions.
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 * (float)delta);
}
 

 

위와 같이 수정한다.

 

방향키를 떼는 순간 한번에 정지하지 않고 관성이 적용된다.

 

하지만 위 코드는 빠르게 방향을 바꾸는 경우엔 관성이 적용되지 않는다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
// Get the input direction and handle the movement/deceleration.
// As good practice, you should replace UI actions with custom gameplay actions.
Vector2 direction = Input.GetVector("ui_left""ui_right""ui_up""ui_down");
if (direction != Vector2.Zero)
{
    //velocity.X = direction.X * Speed;
    velocity.X = Mathf.MoveToward(Velocity.X, direction.X * Speed, Speed * (float)delta);
}
else
{
    velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed * (float)delta);
}
 

 

위와 같이 수정한다.

좀 더 빠른(느린) 방향 전환이 필요하다면 MoveToward()의 Speed * (float)delta에 적당한 수치를 곱한다.

 

빠르게 방향을 바꾸어도 관성이 적용되어 슈퍼 마리오 같은 움직임을 보인다.

 

※ 참고

Using CharacterBody2D/3D

 

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

Detects motions in the vision and saves an image of it as a PNG file. Adjust the sensitivity value if needed.


카메라 영상의 움직임을 감시하고 움직임이 감지된 순간의 영상을 저장한다. sensitivity 값으로 감도를 조절한다.


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
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main(int argc, char** argv)
{
    VideoCapture cap(0);
 
    Mat frameNew;
    Mat frameOld;
    Mat frameDiff;
 
    double min, max;
    int sensitivity = 100;
    int detectionCount = 0;
 
    cap >> frameOld;
 
    while (true)
    {
        cap >> frameNew;
        if (frameNew.empty())
            break;
 
        // Calculates the per-element absolute difference
        // between two arrays or between an array and a scalar.
        absdiff(frameNew, frameOld, frameDiff);
        cvtColor(frameDiff, frameDiff, COLOR_BGR2GRAY);
        minMaxLoc(frameDiff, &min, &max);
 
        if (max > sensitivity)
        {
            cout << "Motion detected. (Max: " << max << ")" << endl;
 
            // For PNG, it can be the compression level from 0 to 9.
            // A higher value means a smaller size and longer compression time.
            vector<int> compression_params;
            compression_params.push_back(IMWRITE_PNG_COMPRESSION);
            compression_params.push_back(3);
            if (imwrite(format("detection_%03d.png", detectionCount++), frameNew, compression_params))
                cout << "Image saved." << endl;
            else
                cout << "Image not saved." << endl;
        }
 
        imshow("Motion Detectoion", frameDiff);
        
        frameNew.copyTo(frameOld);
 
        if (waitKey(10== 27)
            break;
    }
 
    return 0;
}
cs



<frameDiff>


<Detection_XXX.png>




반응형
Posted by J-sean
: