본문 바로가기

DirectX11 3D/과제

<DirectX11 3D 과제> 39 - Model(Bone + Mesh + Material) 탱크 움직이기

과제 내용


- 탱크 이동과 바퀴 회전시켜보기

 

-> 탱크 이동은 tank 모델 자체의 transform을 움직여서 이동시켰습니다.

-> 탱크 바퀴는 S * R * T (스자이공부) 개념을 생각하며 코드를 작성하였습니다. 회전을 할시에 중간 들어가는 자전(R)을 내가 정의한 R로 바꿔줘서 연산해야 합니다.

 

- UpdateTransform() 함수를 수정하여 True일 시에 인자로 들어간 Matrix로 해당 Bone의 Matrix가 바뀌도록 설정하였습니다.

 

코드


void ModelDemo::Update()
{
	sky->Update();
	grid->Update();

	if (airPlane != nullptr) airPlane->Update();
	if (tower != nullptr) tower->Update();
	if (tank != nullptr)
	{
		// 디버깅 모드로 보면 10번 본은 포탑이다.
		ModelBone* bone = tank->GetModel()->BoneByIndex(10);

		// 회전 시키기 위해
		Transform transform;

		// 180 만큼 돌기 위해 PI 곱해줌
		float rotation = sinf(Time::Get()->Running() + 100) * Math::PI * Time::Delta();

		transform.Rotation(0, rotation, 0);

		tank->UpdateTransform(bone, transform.World());

		// Wheel(4개)
		for (int i = 0; i < 4; i++)
		{
			bone = tank->GetModel()->BoneByIndex(3 + (i * 2));
			rotation = sinf(Time::Get()->Running()) * Math::PI * Time::Delta();

			Matrix mat = bone->Transform();
			Vector3 scale;
			Vector3 null;
			Vector3 position;
			Math::MatrixDecompose(mat, scale, null, position);

			Matrix S, R, T;	
			D3DXMatrixScaling(&S, scale.x, scale.y, scale.z);
			D3DXMatrixTranslation(&T, position.x, position.y, position.z);

			Matrix new_R;
			D3DXMatrixRotationYawPitchRoll(&new_R, 0, rotation * 1000, 0);
			tank->UpdateTransform(bone, (S * new_R * T), true);
		}

		// forward move
		float position = sinf(Time::Get()->Running()) / 100;
		Vector3 tank_position;
		tank->GetTransform()->Position(&tank_position);
		tank_position.z += position;
		tank->GetTransform()->Position(tank_position);

		tank->Update();
	}
	if (kachujin != nullptr) kachujin->Update();
}

 

결과