Skip to content

Instantly share code, notes, and snippets.

@hexagit
Created October 17, 2018 08:11
Show Gist options
  • Select an option

  • Save hexagit/1e9e6fb843a7bc0d907655fd78267945 to your computer and use it in GitHub Desktop.

Select an option

Save hexagit/1e9e6fb843a7bc0d907655fd78267945 to your computer and use it in GitHub Desktop.
D3DXVECTOR3 bulletPos; //弾丸の現在位置
D3DXVECTOR3 preBulletPos; //1フレーム前の弾丸の位置
D3DXVECTOR3 moveDirection; //弾丸の移動方向
float bulletHorizontalSpeed = 100.0f; //弾丸水平方向の速度
float bulletVerticalSpeed = 50.0f; //弾丸の垂直方向の速度
float gravity = 9.8f; //重力
/// <summary>
/// 弾丸の移動処理
/// </summary>
void Bullet::Move()
{
//弾丸のX軸、Y軸、Z軸
D3DXVECTOR3 xAxis, yAxis, zAxis;
//ワールド行列の定義と初期化
D3DXMATRIX worldMatrix;
D3DXMatrixIdentity(&worldMatrix);
//デルタタイムの取得
float deltaTime = GetDeltaTime();
//平面移動量と垂直移動量の計算
bulletPos.z += moveDirection.z * bulletHorizontalSpeed * deltaTime;
bulletPos.x += moveDirection.x * bulletHorizontalSpeed * deltaTime;
bulletPos.y += bulletVerticalSpeed * deltaTime;
//以下回転処理
//仮のY軸を定義
yAxis = { 0.0f, 1.0f, 0.0f };
//現在のZ軸を取得し正規化
zAxis = preBulletPos - bulletPos;
D3DXVec3Normalize(&zAxis, &zAxis);
//仮のY軸とZ軸で外積の計算を行い、現在のX軸を算出
D3DXVec3Cross(&xAxis, &yAxis, &zAxis);
D3DXVec3Normalize(&xAxis, &xAxis);
//X軸とZ軸で外積の計算を行い、現在のY軸を算出
D3DXVec3Cross(&yAxis, &xAxis, &zAxis);
D3DXVec3Normalize(&yAxis, &yAxis);
//求めた三軸をワールド行列にセット
memcpy(worldMatrix.m[0], &xAxis, sizeof(D3DXVECTOR3));
memcpy(worldMatrix.m[1], &yAxis, sizeof(D3DXVECTOR3));
memcpy(worldMatrix.m[2], &zAxis, sizeof(D3DXVECTOR3));
//新しい弾丸の座標をワールド行列にセット
worldMatrix._41 = bulletPos.x;
worldMatrix._42 = bulletPos.y;
worldMatrix._43 = bulletPos.z;
//ワールド行列を描画クラスにセット
SetMatrix(worldMatrix);
//Y軸方向の速度を重力の値だけ減少させる
bulletVerticalSpeed -= gravity * deltaTime;
//弾丸の位置を記録
preBulletPos = bulletPos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment