質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

1831閲覧

Dxlib ジャンプと重力落下と地面から落ちた時の処理が上手く噛み合わない シンプルに作るにはどうすればいいのか

退会済みユーザー

退会済みユーザー

総合スコア0

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

1クリップ

投稿2019/08/15 08:56

ジャンプしてできるときは地面に着地してるときでジャンプ中はジャンプできない ジャンプの上昇がおわったら落下し始めるという処理、それと左右に進んでブロックのない足場につくと落下する処理 などが上手く噛み合わずbool型の条件まみれになってしまいどうすればいいのかわかりません。

Player.cpp

1#include <iostream> 2#include <fstream> 3#include "Input.h" 4#include "Player.h" 5#include "DxLib.h" 6#include "Map.h" 7#include "Fps.h" 8 9#define MOVE_SPD 5 10 11/*コンストラクタ*/ 12Player::Player(const char* str, int xx, int yy) 13{ 14 15 //LoadDivGraph(str,7,7,1,64,64,g_handle); 16 AnimeClip = new Animation(anime_s::ewait,str); 17 pos = new Position(); 18 frame = new Fps(); 19 20} 21 22/*計算更新*/ 23void Player::Update() 24{ 25 frame->Update(); 26 27 input_key(); 28 jump_up(); 29 gravity(); 30 31 32 AnimeClip->Update(); 33 34 35 DrawFormatString(100, 200, GetColor(255, 255, 255), "isGround %d", isGround); 36 DrawFormatString(100, 280, GetColor(255, 255, 255), "isJump %d", isJump); 37 38 39} 40 41 42/*描画更新*/ 43void Player::Draw_Update() 44{ 45 46 47 48 if ( key == -1) 49 { 50 //DrawTurnGraph(gPos_x(), gPos_y(), g_handle[0], true); 51 /////// 52 DrawTurnGraph(pos->gPos_x(), pos->gPos_y(), AnimeClip->draw_setClip(), true); 53 54 } 55 56 /*右→*/ 57 if (key == 1) 58 { /// 59 DrawGraph(pos->gPos_x(), pos->gPos_y(), AnimeClip->draw_setClip(), true); 60 } 61 62 AnimeClip->Draw_Update(); 63 64 65 frame->Wait(); 66} 67 68/*キー入力*/ 69void Player::input_key() 70{ 71 if (keybord(KEY_INPUT_LEFT) > 0)//← 72 { 73 74 key = -1; 75 AnimeClip->Update_changeAnime(anime_s::ewalk,key); 76 pos->sPos_x(-MOVE_SPD); 77 78 79 }else if (keybord(KEY_INPUT_RIGHT) > 0)//→ 80 { 81 82 key = 1; 83 AnimeClip->Update_changeAnime(anime_s::ewalk,key); 84 85 pos->sPos_x(+MOVE_SPD); 86 87 }else if (keybord(KEY_INPUT_UP) > 0)//上 88 { 89 pos->sPos_y(+MOVE_SPD); 90 91 }else if (keybord(KEY_INPUT_DOWN) > 0)//下 92 { 93 pos->sPos_y(-MOVE_SPD); 94 } 95 96 else if (keybord(KEY_INPUT_SPACE) > 0)//ジャンプ 97 { 98 //if(Map::Collision::under_col(pos->refgPos_x(), pos->refgPos_y()) == false) 99 if (isGround == true) { 100 isJump = true; 101 isGround = false; 102 } 103 104 AnimeClip->Update_changeAnime(anime_s::ejump); 105 106 } 107 else//何も押していない時 108 { 109 AnimeClip->Update_changeAnime(anime_s::ewait); 110 111 } 112 113 114 115 116} 117 118/*************************************描画 アニメーション管理**************************************/ 119 120void Player::Animation::Update() 121{ 122 f->Update(); 123 124#define ANI_SPD 15 125 if (state == anime_s::ewalk) {//走ってる場合 126 127 if (f->gframe() % ANI_SPD == 0) 128 { 129 Clip = walk_ani[cc]; 130 131 cc++; 132 } 133 134 if (cc >= 3) { cc = 0; } 135 136 } 137 else { cc = 0; } 138 139 if (state == anime_s::ewait) {//待機場合 140 Clip = *wait_ani; 141 } 142 143 144 145 146#undef ANIME_SPD 147} 148 149void Player::Animation::Draw_Update() 150{ 151 f->Wait(); 152} 153 154void Player::Animation::Update_changeAnime(anime_s clip, int key)//keyは向き -1が左側 155{ 156 state = clip; 157 isReverse = key; 158} 159 160void Player::Animation::Update_changeAnime(anime_s clip)//keyは向き -1が左側 161{ 162 state = clip; 163} 164 165 166int Player::Animation::draw_setClip() 167{ 168 return Clip; 169} 170 171 172 173/****************************************************************************************/ 174 175 176float jf = 0; 177 178/************************ジャンプ ***********************/ 179void Player::jump_up() 180{ 181 if (isJump == true && isGround == false) 182 { 183 184 //isGround = false; 185 jf += ((jf + 13) / 2); 186 pos->sPos_y(jf); 187 188 if (jf >= 100) 189 { 190 //pos->sPos_y(jf); 191 jf = 0; 192 193 194 195 isJump = false; 196 197 } 198 199 200 } 201 else { 202 203 } 204 205 206} 207/*****************************************************/ 208 209 210/***********************重力*******************************/ 211void Player::gravity() 212{ 213 214 //落下 215 if (Map::Collision::under_col(pos->refgPos_x(), pos->refgPos_y()) == false && isJump == false && isGround == false) 216 { 217 // if (frame.gframe() % 20 == 0) { 218 g = ((g - 2) / 2); 219 if( g >= -10) 220 { 221 g = -10; 222 } 223 // d = -10; 224 //} 225 226 pos->sPos_y(&g); 227 } 228 else { 229 isGround = false; 230 } 231 232 233 234 if (Map::Collision::under_col(pos->refgPos_x(), pos->refgPos_y()) == false) 235 { 236 DrawFormatString(100,100,GetColor(255,255,255),"false"); 237 //pos->sPos_y(1); 238 } 239 240 241 if (Map::Collision::under_col(pos->refgPos_x(), pos->refgPos_y()) == true) 242 { 243 DrawFormatString(100, 100, GetColor(255, 255, 255), "True"); 244 //pos->sPos_y(1); 245 } 246} 247/**********************************************************/ 248 249 250/*座標の取得と設定 Y軸反転済み (y) はマイナス処理 つまり*/ 251void Player::Position::sPos_x(int *xx)//加算 252{ 253 x += *xx; 254} 255 256void Player::Position::sPos_y(int *yy)//加算 257{ 258 //int py = -*yy; 259 260 y += -*yy; 261 262} 263 264void Player::Position::sPos_x(int xx) 265{ 266 x += xx; 267 268} 269 270void Player::Position::sPos_y(int yy) 271{ 272 y += -yy; 273 274} 275 276int Player::Position::gPos_x() 277{ 278 return x; 279} 280 281int Player::Position::gPos_y() 282{ 283 return y; 284} 285 286int& Player::Position::refgPos_x() 287{ 288 return x; 289} 290int& Player::Position::refgPos_y() 291{ 292 return y; 293} 294 295

Map.h

1#include "Map.h" 2#include <fstream> 3 4#define CELL 64 5 6int Map::map_set[11][20] = { 7 8 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 9 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 10 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 11 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 12 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 13 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 14 {-1,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 15 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 16 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, 17 {-1,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,-1,-1,-1,-1,-1,-1,-1,-1}, 18 {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1}, 19 20 21}; 22 23 24Map::Map(const char* str) 25{ 26 LoadDivGraph("mario_resource/Block_64px.png ", 40, 10, 4, 64, 64, ghandle); 27 //std::fstream ofs("Log.txt", std::ios::app); 28 //std::fstream ofs("Log.txt", std::ios::app); 29 30} 31 32/*マップセル取得*/ 33int Map::draw_mapCell(int& x, int& y) 34{ 35 return (int)map_set[y][x]; 36} 37 38int Map::get_mapCell(int& x, int& y) 39{ 40 return (int)map_set[y][x]; 41 42} 43 44int ay; 45int by; 46 47int ax; 48int bx; 49 50/*描画 更新*/ 51void Map::Draw_Update() 52{ 53 for (int y = 0; y < 11; y++) 54 { 55 for (int x = 0; x < 20; x++) 56 { 57 DrawGraph(x * 64, y * 64, ghandle[draw_mapCell(x, y)], true); 58 } 59 } 60 /* 61 DrawFormatString(0, 80, GetColor(255, 255, 255), "ay: %d", ay); 62 DrawFormatString(0, 80 * 2, GetColor(255, 255, 255), "by: %d", by); 63 64 DrawFormatString(0, 80 * 3, GetColor(255, 255, 255), "ax: %d", ax); 65 DrawFormatString(0, 80 * 4, GetColor(255, 255, 255), "bx: %d", bx); 66 */ 67} 68 69 70/*計算 更新*/ 71void Map::Update() 72{ 73 74} 75 76 77bool Map::Collision::under_col(int &px, int &py)//下の当たり判定 78{ 79 80 for (int y = 0; y < 11; y++) 81 { 82 for (int x = 0; x < 20; x++) 83 { 84 if (get_mapCell(x, y) == 0) 85 { 86 if (py < (y * CELL) && (py + CELL) > (y * CELL)) 87 { 88 for (int dx = 1; dx <= 63; dx++) 89 { 90 if ((px + dx) >= (x * CELL) && (x * CELL) + CELL >= (px + dx) || 91 (px + dx) <= (x * CELL) && (x * CELL) <= (px + dx) ) 92 { 93 return true; 94 } 95 } 96 } 97 //if (( py < (y * CELL) && (py + CELL ) > (y * CELL) || py > (y * CELL) && py < CELL + (y * CELL)) 98 99 //&& ( (x * CELL) >= px && (px + CELL) >= (x * CELL)|| (( x * CELL) <= px) && ((x * CELL) + CELL >= px ) )) 100 101 } 102 } 103 } 104 return false; 105} 106

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ざっとみると、地面に足がついているかだけを判定した、isGroundだけあればOKです
isJumpは特に必要ないと思います

ジャンプの下降処理は普通に垂直方向の速度を減算していけば、
そのうちマイナスになるので特にフラグは必要ありません

投稿2019/08/15 10:20

izmktr

総合スコア2856

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2019/08/15 10:27

足場から落ちるときの処理でブロックにめり込んでしまう現象を解撤したいのですがどうすればピッタリブロックの上にキャラクタの画像描画されるようコードを組めばいいのでしょか?
yumetodo

2019/08/15 11:08

接触判定のときに最近傍の座標返せばいいのでは、std::optionalとか使って
退会済みユーザー

退会済みユーザー

2019/08/15 11:12

どういうことでしょうか?初学者なの初歩的ことですが解説してもらたいです
izmktr

2019/08/15 14:25

まず、プログラムで地面にめり込んでいる、というif文は書けますか? その後、y座標を上にあげていって、めり込んでいない、という判定になればそこがめり込んでいない位置です
yumetodo

2019/08/16 03:59

それでもいいですけど、当たり判定を取るとき、めり込んでいるならまさにめり込んでいるオブジェクトが最近傍のオブジェクトですよね?当たり判定を取るときにそのオブジェクトの座標は知ることができるはずです。当たり判定の関数がその座標を返せばいいのではないでしょうか?ということでした。ただしあたっていないときに座標を返すのは不自然なので、std::optional<Coordinate>みたいな戻り値の型にすればいいんじゃないですかね、という話でした。 https://cpprefjp.github.io/reference/optional/optional.html
退会済みユーザー

退会済みユーザー

2019/08/16 07:42

質問ですが「ジャンプの下降処理は普通に垂直方向の速度を減算していけば...」という処理の説明なのすが これはどのようなことなのでしょうか?自分が作りたい処理はジャンプで上昇したあと下に重力落下していくとうマリ◯である処理をそのまま作りたいのですがもうすこし詳細に説明がほしいです。
izmktr

2019/08/16 07:57

位置、速度、加速度の概念を理解していますか? 等速直線運動、等加速度直線運動あたりでググって勉強してみてください
退会済みユーザー

退会済みユーザー

2019/08/16 08:04

質問ですが y = (y - 2)でだんだん0近づきそのうちマイナスの値に行くという処理は正しいのでしょうか? そこでブロックに当たった時に補正をするという処理です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問