トップページ > 記事閲覧
画像との当たり判定
名前:C++プログラミング歴 約1か月 日時: 2018/02/16 18:42

dxライブラリでRPGを作ろうとしているところなのですが、何故か常に触れているという処理になってしまいます。 下のコードをどのようにすればよろしいでしょうか? mapchip.h------------------------------ #pragma once class mapchip { public: int mapx; int mapy; int block[2]; int map[15][20]; int atari[15][20]; int mapgraph(); mapchip(); ~mapchip(); }; mapchip.cpp-------------------- #include "mapchip.h" #include "DxLib.h" int mapchip::mapgraph() { LoadDivGraph("画像/test.png", 2, 2, 1, 32, 32, block); int map[15][20] = { { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, { 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1 }, { 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1 }, { 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1 }, { 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1 }, { 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1 }, { 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1 }, { 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1 }, { 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1 }, { 1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1 }, { 1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1 }, { 1,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1 }, { 1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,0,0,1,1,1 }, { 1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1 }, { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, }; int atari[15][20] = { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,0 }, { 0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,0 }, { 0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,0 }, { 0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0,0 }, { 0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0 }, { 0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0 }, { 0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0 }, { 0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0 }, { 0,0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0 }, { 0,0,0,0,255,255,255,255,255,255,255,255,255,255,255,255,255,0,0,0 }, { 0,0,0,0,255,255,255,255,255,255,255,0,0,0,255,255,255,0,0,0 }, { 0,0,0,0,255,255,255,255,255,255,0,0,0,0,0,255,255,0,0,0 }, { 0,0,0,0,255,255,255,255,255,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, }; for (int mapy = 0; mapy < 15; mapy++) { for (int mapx = 0; mapx < 20; mapx++) { DrawGraph(mapx * 32, mapy * 32, block[map[mapy][mapx]], TRUE); } } return 0; } mapchip::mapchip() { } mapchip::~mapchip() { } character.h---------------------- #pragma once #include "mapchip.h" class character : public mapchip { public: int px; int py; int p; int muki; int wsp; int flag; int anim[16]; int reset(); int idou(); int tikei(); int animation(); int ALL(); character(); ~character(); }; character.cpp----------------------- #include "character.h" #include "DxLib.h" int character::reset() { px = 160; py = 320; wsp = 4; muki = 0; flag = 1; LoadDivGraph("画像/キャラクタ1.png", 16, 4, 4, 32, 32, anim); return 0; } int character::tikei() { if (muki == 0) { if (atari[(py) / 32 - 1][(px) / 32] == 0) { return 1; } } if (muki == 1) { if (atari[(py) / 32][(px) / 32 - 1] == 0) { return 1; } } if (muki == 2) { if (atari[(py) / 32 + 1][(px) / 32] == 0) { return 1; } } if (muki == 3) { if (atari[(py) / 32][(px) / 32 + 1] == 0) { return 1; } } return 0; } int character::idou() { if (px % 32 == 0 && py % 32 == 0) { flag = 1; if (CheckHitKey(KEY_INPUT_UP) == 1) { muki = 0; } if (CheckHitKey(KEY_INPUT_LEFT) == 1) { muki = 1; } if (CheckHitKey(KEY_INPUT_DOWN) == 1) { muki = 2; } if (CheckHitKey(KEY_INPUT_RIGHT) == 1) { muki = 3; } if (CheckHitKey(KEY_INPUT_UP) == 0 && CheckHitKey(KEY_INPUT_LEFT) == 0 && CheckHitKey(KEY_INPUT_DOWN) == 0 && CheckHitKey(KEY_INPUT_RIGHT) == 0) { flag = 0; } if (flag == 1) { if (tikei()==1) { flag = 0; } } } if (flag == 1) { if (muki == 0) { py--; } if (muki == 1) { px--; } if (muki == 2) { py++; } if (muki == 3) { px++; } } if (px % 32 != 0) { px++; } if (py % 32 != 0) { py++; } return 0; } int character::animation() { p = anim[(px % 32 + py % 32) / 8 + muki * 4]; DrawBox(px, py, px + 32, py + 32, GetColor(0, 255, 0), TRUE); DrawFormatString(0, 0, GetColor(0, 255, 0), "%d,%d", py / 32, px / 32); DrawFormatString(0, 15, GetColor(0, 255, 0), "%d", tikei()); DrawFormatString(0, 30, GetColor(0, 255, 0), "%d", muki); DrawGraph(px, py, p, TRUE); return 0; } int character::ALL() { tikei(); idou(); animation(); return 0; } character::character() { } character::~character() { } main.cpp------------------------------ #include "DxLib.h" #include "mapchip.h" #include "character.h" mapchip map_1; character chara_1; int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen(DX_SCREEN_BACK); //ウィンドウモード変更と初期化と裏画面設定 chara_1.reset(); while (ScreenFlip() == 0 && ProcessMessage() == 0 && ClearDrawScreen() == 0) { map_1.mapgraph(); chara_1.ALL(); } DxLib_End(); // DXライブラリ終了処理 return 0; }
メンテ

Page: 1 |

Re: 画像との当たり判定 ( No.1 )
名前:774 日時:2018/02/16 20:05

手を離しても1マス(32x32px)ぶんだけスイーっと動く…という動作ですよね? int character::idou() の中の if (px % 32 != 0) { px++; } if (py % 32 != 0) { py++; } を削除すればよいのではないでしょうか。(ここがあるので上と左に行かなくなっています) ちなみに int mapchip::mapgraph() の中で LoadDivGraph() を呼んでいますが、これでは ループのたびにデータを読み込むので避けた方がよいと思います。
メンテ
Re: 画像との当たり判定 ( No.2 )
名前:C++プログラミング歴 約1か月 日時:2018/02/16 21:29

すみません、変更したけれども移動できませんでした 原因は今のところは不明です もし、原因となるとすればidou()かtikei()のコードだと思っているので、また見直していきたいです
メンテ
Re: 画像との当たり判定 ( No.3 )
名前:C++プログラミング歴 約1か月 日時:2018/02/16 21:45

mapchipクラスからcharacterクラスをはずし、characterクラスに直接atari[15][20]の部分を書いたことでしっかりと動くようになりました 774さんありがとうございました 解決しました
メンテ
Re: 画像との当たり判定 ( No.4 )
名前:C++プログラミング歴 約1か月(解決) 日時:2018/08/06 13:53

つけ忘れておりました
メンテ

Page: 1 |

題名
名前
コメント
パスワード (記事メンテ時に使用)

   クッキー保存