Monday, December 30, 2019

shared_ptr


#include <iostream>
#include <vector>

using namespace std;

class MyClass
{
public:
        MyClass()
        {
               cout << "MyClass is created" << endl;
        }

        ~MyClass()
        {
               cout << "MyClass is destroyed" << endl;
        }
};

class MyParent
{
public:
        shared_ptr<char> data;
        //char* data;

        MyParent()
        {
               //data = new char[100];
               data = make_shared<char>(100);
               strcpy(data.get(), "haha");
        }

        ~MyParent()
        {
               data.reset();
               //delete data;
        }
};

void* operator new(std::size_t size)
{
        void* storage = malloc(size);
        return storage;
}

void operator delete(void* data)
{
        free(data);
}

int main() {   
        vector<MyParent> vec;

        {
               MyParent parent;                      
               vec.push_back(parent);
        }                     
        return 1;
}



when I use shared_ptr then memory variable 'data' is still alive when we are at 'return 1;'
instead raw pointer (char* data) will be deleted.

{ MyParent parent; vec.push_back(parent); }

because char* data of local variable of parent is trying to free char* data.
Personally I prefer to use raw pointer and try to avoid use value type of MyParent.
If I use a pointer type then

vector<MyParent*> vec;

unique_ptr

unique_ptr<MyClass> a = make_unique<MyClass>();
unique_ptr<MyClass> b = move(a);

Thursday, December 19, 2019

Motion Matching prototype

Recently I'm implementing a motion matching animation tech. I think use correct animation data is more important than the algorithm (of course!) 


Game AI 3rd edition... :'(

I translated game ai 2nd edition and below section got removed from the 3rd edition. :&

렌더링 하드웨어에서 작업하기
오래된 콘솔에서 작업하는데 있어 가장 큰 문제는 그래픽스를 위한 최적화 작업이다. 그래픽스는 보통 게임의 뒷편에 있는 첨단 기술의 핵심이며 한정된 리소스를 사용해 최신의 그래픽 기술을 제공하는 것을 콘솔 제작자가 강조하는 것이 당연하다.

오리지널 Xbox 설계는 최초로 PC와 같은 콘솔 아키텍처를 가진 점에 있어서 새로웠다. PC와 같이 메인 프로세서가 있고 이해 가능한 그래픽 버스, 친숙한 그래픽 칩셋을 갖추고 있었다. 다른 한쪽에서 PS2의 경우 뻔뻔하게도 그래픽스 렌더링에 최적화 되어 있었다. 하드웨어의 성능을 최대한으로 사용하려면 최대한 렌더링을 병렬로 돌아가게 만들어야 했고 이로 인해 생겨나는 동기화, 통신 이슈가 만만치 않게 어려운 작업이었다. 몇몇 개발자는 간단하게 포기하는 단계에 이르렀고 게임에 간단한 AI를 넣는 수준에서 멈췄다. 콘솔 개발을 하게 되면 AI 개발자에게 있어 눈에 가시인 크로스 플랫폼 타이틀 개발이 계속 된다. 다행히 PS3에는 멀티 코어가 있고 빠른 AI 처리를 매우 쉽게 달성할 수 있다.

렌더링 하드웨어는 파이프라인 모델로 동작한다. 데이터는 파이프의 끝에 들어가고 다양한 프로그램에 의해 계산 및 처리된다. 파이프라인의 끝에서 데이터는 화면에 그려질 준비를 마친다. 데이터는 파이프라인으로 다시 되돌릴 수 없고 데이터의 양은 보통 매우 작다. 입력 데이터를 전달 하는 것을 제외하고는 간단하고 데이터 플로우가 논리적이기 때문에 하드웨어는 이 파이프라인을 매우 효율적으로 동작하게 할 수 있다.

AI는 이 파이프라인 모델에 적합하지 않다. AI는 시간과 상관없이 다양한 종류의 코드가 동작하며 본질적으로 분기가 많다. 하나의 연산 결과가 많은 다른 객체의 입력 값이 되며 그것들의 결과 값들이 다시 처음의 입력 값으로 된다. 그리고 이것이 계속 반복된다.

캐릭터가 계속해서 움직일 때 충돌이 발생할 지점을 찾아낸다 거나 하는 간단한 AI 요청이라도 모든 지형을 검사해야 한다면 구현하는 것이 어려울 수 있다. 오래된 그래픽스 하드웨어는 충돌 처리를 제공하는 경우도 있지만 충돌을 예측하는 것은 여전히 AI 코드가 담당한다. 더 복잡한 AI는 필연적으로 CPU에서 동작하는데, 이 칩은 마지막 세대 콘솔에 비해 파워가 부족하고 그로 인해 인공 지능은 5~10년 전 PC에 제한되어 있다.

역사적으로 볼 때 동일한 처리 능력을 갖춘 PC와 비교할 때 콘솔에서 처리되는 AI의 양을 제한하는 경향이 있다.  가장 흥미로운 부분은 최근 18개월 동안 현세대 콘솔에서 PC와 같은 AI 실행이 가능하게 기능들을 제공하고 그 가능성을 보인다는 점이다.

Sunday, December 15, 2019

Representation of linear transformation of matrix

Let say there is a 3x3 matrix

[m11 m12 m13]
[m21 m22 m23]
[m31 m32 m33]

[m11 m12 m13] is a row vector

[m21 m22 m23] is a row vector

[m31 m32 m33] is a row vector

and which of above are basis vectors in 3D space. i, j, k in standard form.

[m11 m12 m13] is a row vector and i

[m21 m22 m23] is a row vector and j

[m31 m32 m33] is a row vector and k

using linear transformation we can write a vector v like down below.

v = vx*i + vy*j + vz*k

if we apply some transform(matrix) into vector V and we can visualize it.

for instance we have 

[1 0 0]
[0 1 0] == matrix A
[0 0 1]

it is an identity matrix and if we multiply vector v with matrix.

vA

we will get v because A is an identity matrix.

if matrix A is like down below.

[0.75 0.75 0]
[0 1 0]
[0 0 1]

vector v or shape of model will be scaled, shrink.



Using this representation, we can guess what the shape will look like when we apply some transformations. (Sorry! I'm not good at drawing)

Representation of vector and multiplication.

There are two ways to represent of vector.

row vector and column vector.

[x y z] is a row vector

[x]
[y]
[z]

is a column vector.

When we multiple a vector with matrix, should be careful.

For instance we want to apply matrix A, B then C in order and there is a vector 'v'
depending on the representation of vector, we use it differently.

if the v is a row vector then we should use vABC.
if the v is a column vector then we should use CBAv.

Many books and game engine uses differently. so you should be careful when you use it.

1. Check representation of vector.
2. Check an order of matrix and then apply it to the vector carefully.

Wednesday, December 4, 2019

about vector interpolation

Many of you guys already know that vector interpolation might have different result than you think.

As you can see the below image, red arrow is a forward vector and green arrow is a right vector of the character.

If you interpolate forward vector to right vector, you will get below image.


This is because I didn't account of angle of vector. If I account of angle of vector then I'll get different result like down below.


In unreal engine 4, There are related functions exist.

FMath::RInterpTo is for linear interpolation.
FQuat::Slerp is for spherical linear interpolation.

Task in UnrealEngine

 https://www.youtube.com/watch?v=1lBadANnJaw