提示コードですが矩形の当たり判定なのですがタイトル通りなのですが当たり判定で当たっとき、当たっている間、抜けた時を関数一つ単体で動作する
ように実装したいです。現状はisPrevHit
変数の挙動が原因で抜ける時の処理が動作しないのですがそれを修正してもPlayer.cpp
のように3つの関数が並んで配置しないとそれぞれの処理が成立しません(isPrevHit
変数の問題で)
やりたいことは例えば
当たっている間だけの判定を取りたい場合getHitStay()
関数のみを配置することで当たっている時だけを取得できるようにしたいのですがそれを実装するにはどうしたらいいのでしょうか?isPrevHit
変数の値の書き換えタイミング等を知りたいです。
※Player.cppのコードは毎フレーム実行されます。
Playe.cpp
cpp
1 2void Player::Collision(const std::shared_ptr<SDL::GameObject> obj) 3{ 4 if(boxCollision->getHitEnter(obj->boxCollision) == true) 5 { 6 std::cout<<"Enter"<<std::endl; 7 } 8 9 if(boxCollision->getHitStay(obj->boxCollision) == true) 10 { 11 //std::cout<<"Stay"<<std::endl; 12 } 13 14 15 if(boxCollision->getHitExit(obj->boxCollision) == true) 16 { 17 std::cout<<"Exit"<<std::endl; 18 } 19} 20
Collision.cpp
cpp
1 2 3/*################################################# 4# 矩形 当たった瞬間 5###################################################*/ 6bool SDL::BoxCollision::getHitEnter(const std::shared_ptr<BoxCollision> col) 7{ 8 if(isEnable == true) 9 { 10 if (BoxCollision_Hit(position,size,col->position,col->size) == true) 11 { 12 if(isPrevHit == false) 13 { 14 isPrevHit = true; 15 16 return true; 17 } 18 else 19 { 20 return false; 21 } 22 } 23 else 24 { 25 //////////////////////////// 26 isPrevHit = false; 27 //////////////////////////// 28 } 29 } 30 else 31 { 32 isPrevHit = false; 33 } 34 35 return false; 36} 37 38 39/*################################################# 40# 矩形 当たっている時 41###################################################*/ 42bool SDL::BoxCollision::getHitStay(const std::shared_ptr<BoxCollision> col) 43{ 44 if(isEnable == true) 45 { 46 if (BoxCollision_Hit(position,size,col->position,col->size) == true) 47 { 48 if(isPrevHit == true) 49 { 50 return true; 51 } 52 else 53 { 54 isPrevHit = true; 55 return false; 56 } 57 } 58 else 59 { 60 ////////////////////////////// 61 isPrevHit = false; 62 ////////////////////////////// 63 } 64 65 } 66 else 67 { 68 isPrevHit = false; 69 } 70 71 return false; 72} 73 74 75/*################################################# 76# 矩形 抜けた時 77###################################################*/ 78bool SDL::BoxCollision::getHitExit(const std::shared_ptr<BoxCollision> col) 79{ 80 if(isEnable == true) 81 { 82 if(isPrevHit == true) 83 { 84 if (BoxCollision_Hit(position,size,col->position,col->size) == false) 85 { 86 isPrevHit = false; 87 return true; 88 } 89 } 90 } 91 else 92 { 93 isPrevHit = false; 94 } 95 96 return false; 97} 98
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2022/08/22 02:31