Astar 알고리즘 시뮬레이션

 

youtu.be/en0QjTzjSd8

 

소스 코드

github.com/gkagm2/cpppractise/blob/master/cpptraining/ch1/Algorithm/AStar/AStar.cpp

 

gkagm2/cpppractise

:orange_book:C++ 연습용, 연구용, 토이 프로그램들을 담은 프로젝트:orange_book: - gkagm2/cpppractise

github.com

 

'게임 개발 > WinApi' 카테고리의 다른 글

WinAPI 2D 총게임 소스코드  (0) 2019.10.23
WinAPI 싱글톤 Singleton  (0) 2019.10.23
WinApi  (0) 2019.10.09

직접 만든 그래픽스 엔진으로 뿌려보자

 

폴리곤 완성~ 사각형 만들어서 회전시켜보기

 

 

 

 

큐브도 만들었다.

프로젝션 행렬을 적용해서 입체적으로 보이게 했다.

 

 

(게임은 아님)

숫자 패드의 번호를 입력하면 0번 숫자가 눌렀던 방향으로 움직인다.

 

4(오른쪽), 2(아래) 숫자 키를 눌러서 숫자 0이 2행2열로 감.

 

소스 코드

github.com/gkagm2/cpppractise/blob/master/cpptraining/ch1/Program/MoveCountInMatrix_MBCS_WBCS/MoveCountInMatrix_MBCS_WBCS.cpp

 

실행파일

github.com/gkagm2/cpppractise/blob/master/cpptraining/ch1/Program/MoveCountInMatrix_MBCS_WBCS/MoveCountInMatrix_MBCS_WBCS.exe

 

빙고게임 연습.

 

 

5번 빙고되면 승리 출력 후 종료

 

소스 코드

github.com/gkagm2/cpppractise/blob/master/cpptraining/ch1/Game/Binggo_MBCS_WBCS/Binggo_MBCS_WBCS.cpp

 

실행 파일

github.com/gkagm2/cpppractise/blob/master/cpptraining/ch1/Game/Binggo_MBCS_WBCS/Binggo_MBCS_WBCS.exe

프로젝션 행렬을 이용한 구현.

public class Test : MonoBehaviour
{
    Matrix4x4 originalProjectionMat;

    private void Start()
    {
        originalProjectionMat = Camera.main.projectionMatrix;
    }

    private void CameraShake()
    {
        Matrix4x4 pMat = originalProjectionMat;

        // roll
        pMat.m01 += Mathf.Sin(Time.time * 1.25f) * 0.4f;
        pMat.m10 += Mathf.Sin(Time.time * 1.5f) * 0.4f;

        // x
        pMat.m00 += Mathf.Sin(Time.time * 1.5f) * 0.1f;
        pMat.m11 += Mathf.Sin(Time.time * 1.5f) * 0.1f;

        Camera.main.projectionMatrix = pMat;
    }
    
    private void Update()
    {
        CameraShake();
    }
}

매트릭스 Inverse 직접 구현

 

회전행렬의 역행렬 * 이동행렬의 역행렬

    public Matrix4x4 Inverse(Matrix4x4 matrix)
    {
        // Rotate Scale Inverse
        Matrix4x4 invRot = Matrix4x4.identity;
        for(int i=0; i < 3; ++i)
        {
            Vector4 v = matrix.GetColumn(i);
            invRot.SetRow(i, v.normalized / v.magnitude);
        }

        // Translate Inverse
        Matrix4x4 invTrans = Matrix4x4.identity;
        invTrans.m03 = -matrix.m03;
        invTrans.m13 = -matrix.m13;
        invTrans.m23 = -matrix.m23;

        // Combine
        return invRot * invTrans;
    }
public float SetPixelSize(float _pixelRate)
    {
        cam= GetComponent<Camera>();
        Vector3 p1 = new Vector3(1, 0, 0);
        Vector3 p2 = new Vector3(2, 0, 0);

        p1 = cam.ScreenToWorldPoint(p1);
        p2 = cam.ScreenToWorldPoint(p2);

        float pixelSize = p2.x - p1.x;
        pixelSize *= _pixelRate;
        
        return pixelSize;
    }

 

#include <iostream>
#include <math.h>
using namespace std;

struct vec2 {
public:
	float x;
	float y;

	vec2() {

	}
	vec2(float x, float y) {
		this->x = x;
		this->y = y;
	}
};

//      (시작점)--------------------------------------->(끝점)

int main() {
	cin.tie(NULL);
	ios::sync_with_stdio(false);
	
	vec2 posStart(0,0);// 시작점
	vec2 posEnd(10,10);// 끝점

	vec2 arrow1(0, 0);// 화살표 1
	vec2 arrow2(0, 0);// 화살표 2

	float width = 1;  // 너비
	float length = 3; // 길이

	float distance = sqrtf(powf(posEnd.x - posStart.x, 2) + powf(posEnd.y - posStart.y, 2));
	cout << "distance : " << distance << '\n';

	//float th = atan2f(pend.x - pstart.x, pend.y - pstart.y) * (180 / 3.14f);
	float th = atan2f(posEnd.y - posStart.y, posEnd.x - posStart.x);
	cout << "th : " << th << '\n';

	arrow1.x = (distance - length) * cosf(th) - (width / 2.f) * sinf(th) + posStart.x;
	arrow1.y = (distance - length) * sinf(th) + (width / 2.f) * cosf(th) + posStart.y;

	arrow2.x = (distance - length) * cosf(th) + (width / 2.f) * sinf(th) + posStart.x;
	arrow2.y = (distance - length) * sinf(th) - (width / 2.f) * cosf(th) + posStart.y;

	cout << "arrow1 x : " << arrow1.x << ", y : " << arrow1.y << '\n';
	cout << "arrow2 x : " << arrow2.x << ", y : " << arrow2.y << '\n';

	return 0;
}

'게임 개발 > C++ console' 카테고리의 다른 글

배열 안에서 숫자 움직이기 (MBCS, WBCS 연습 겸)  (0) 2021.01.13
빙고게임2 (MBCS,WBCS 연습겸)  (0) 2021.01.13
미디 키보드 건반 프로그램  (0) 2020.01.14
마리오게임  (0) 2019.12.03
TextRPG  (0) 2019.11.12

+ Recent posts