xとy軸のみを使い3Dの立方体を9マスに移動するようにしてみたいため
z軸を定数0で固定しました。
ですが手前に移動すると立方体が空中に浮いたような感じになります。
どのように工夫すれば浮いたようにではなく出来るでしょうか?
こちらはキャラクターの画像と立方体の3Dデータです。
キャラクターの画像と立方体の3Dデータ
#include "DxLib.h"
int Key[256];
int gpUpdateKey() {
char tmpKey[256];
GetHitKeyStateAll(tmpKey);
for (int i = 0; i < 256; i++)
(tmpKey[i] == 0) ? (Key[i] = 0) : Key[i]++;//キーのフレームの習得
return 0;
}
int stage[4][7][2]; // 盤上のマス
int pos[3][6][2]; // 描画座標
int i = 0;
int j = 0;
void init_stage() // stage と pos を初期化する
{
for (j = 0; j < 7; j++) {
int w = (j - 3) * 230, h = 280;//ステージの下の位置で決めた左を固定したままここで大きさを変える(左右へ伸びて大きくなる。)、高さは大きさを変える
for (i = 4; --i >= 0; ) {
stage[i][j][0] = w + 750, stage[i][j][1] = h + 230;//ステージの位置を変える、高さは位置を変える
w = w * 8 / 10, h = h * 9 / 10;
}
}
//ステージ上の中心の座標を表している。
for (int i = 0; i < 3; i++)
for (int j = 0; j < 6; j++) {
pos[i][j][0] = (stage[i][j][0] + stage[i + 1][j + 1][0]) / 2 - 25;
pos[i][j][1] = (stage[i][j][1] + stage[i + 1][j + 1][1]) / 2 - 65;
}
}
int WINAPI WinMain(HINSTANCE hi, HINSTANCE hp, LPSTR cl, int cs) {
SetGraphMode(780, 680, 32); // ウィンドウの大きさを指定
ChangeWindowMode(TRUE); // 全画面ではなくウインドウを使用
if (DxLib_Init() == -1) return -1; // DXライブラリ初期化処理
SetDrawScreen(DX_SCREEN_BACK); // 裏画面を使用する設定
init_stage();
int enemyX = 4, enemyY = 1; // 敵の位置
int playerX = 1, playerY = 1; // 俺の位置
int enemyMove = 0; // 敵の移動状態
int playerMove = 0; // 俺の移動状態
int enemyGHandle[12]; // 敵のグラフィックハンドル格納用配列 int[12]型の変数を定義(基礎より)
int playerGHandle[12]; // 俺のグラフィックハンドル格納用配列
// 5:正面、7:右向き、2:左向き、4:上向き、3:下向き、9:移動不可
int enemyImage = enemyGHandle[11];
int playerImage = playerGHandle[5];//LoadDivGraphよりplayerX = 2, playerY = 2の時のplayerGHandle[5]がDrawGraphのplayerImageに代入される。★最初はここに情報だけ書いておいて、その情報が画面クリア後の関数DrawGrapに渡され描画される。
int ModelHandle = 0;
const int MOVE_INTERVAL = 1000; // 何ミリ秒ごとに移動処理をするか
int nextMoveTime = GetNowCount() + MOVE_INTERVAL; // 次回移動処理をする時刻
int stopCount = 0; // 動かないのがあと何回か
while (ProcessMessage() == 0) {
gpUpdateKey(); // キーの入力状態を取得
{ // 俺の移動
if (Key[KEY_INPUT_RIGHT] == 1)
if (playerX < 2) {
playerMove = 1;
playerX = playerX + 1; //移動
playerImage = playerGHandle[5];
}
if (Key[KEY_INPUT_LEFT] == 1)
if (playerX > 0) {
playerMove = 1;
playerX = playerX - 1; //移動
playerImage = playerGHandle[11];
}
if (Key[KEY_INPUT_UP] == 1)
if (playerY > 0) {
playerMove = 1;
playerY = playerY - 1; //移動
playerImage = playerGHandle[2];
}
if (Key[KEY_INPUT_DOWN] == 1)
if (playerY < 2) {
playerMove = 1;
playerY = playerY + 1; //移動
playerImage = playerGHandle[8];
}
if (playerMove > 0 && ++playerMove > 20) {
playerMove = 0;
playerImage = playerGHandle[5]; // 右を向く
}
}
ClearDrawScreen(); // 裏画面をクリア
// 盤面の描画
int boxColor = GetColor(160, 64, 64);
for (int i = 0; i < 4; i++)
DrawLine(stage[i][0][0], stage[i][0][1],
stage[i][6][0], stage[i][6][1], boxColor);
for (int j = 0; j < 7; j++)
DrawLine(stage[0][j][0], stage[0][j][1],
stage[3][j][0], stage[3][j][1], boxColor);
ModelHandle = MV1LoadModel("untitled3.mv1");
// 画面に映る位置に3Dモデルを移動
MV1SetPosition(ModelHandle, VGet(pos[playerY][playerX][0], pos[playerY][playerX][1], 0.f));
//第二引数の回転角度をセット
MV1SetRotationXYZ(ModelHandle, VGet(0.5, 0, 00.0f));
// 3Dモデルの描画
MV1DrawModel(ModelHandle);
// 敵キャラの描画
DrawGraph(pos[enemyY][enemyX][0],
pos[enemyY][enemyX][1],
enemyImage, true);
// 俺キャラの描画
DrawGraph(pos[playerY][playerX][0],
pos[playerY][playerX][1],
playerImage, true);
ScreenFlip(); // 裏画面を表画面に反映
}
DxLib_End(); // DXライブラリ使用の終了処理
return 0; // ソフトの終了
}