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

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

新規登録して質問してみよう
ただいま回答率
85.50%
OpenGL

OpenGLは、プラットフォームから独立した、デスクトップやワークステーション、モバイルサービスで使用可能な映像処理用のAPIです。

C++

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

Q&A

0回答

593閲覧

当たり判定がバグる原因が知りたい大きさを変えるとバグる github全ソースあり。

退会済みユーザー

退会済みユーザー

総合スコア0

OpenGL

OpenGLは、プラットフォームから独立した、デスクトップやワークステーション、モバイルサービスで使用可能な映像処理用のAPIです。

C++

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

0グッド

1クリップ

投稿2021/02/07 04:25

編集2022/01/12 10:55

提示コードですがCol.size()関数で当たり判定のサイズを指定してしてCol.setPosition();関数でブロックの座標を指定してそれを衝突するときに取得して当たり判定を行って居るのですがなぜか CELL /2 , CELL / 2 と当たり判定をサイズを指定した時だけ当たり判定が画像のような場所で当たり判定が起きてしまいます。どうすればいいのでしょうか? CELL , CELL の時は正常に当たり判定ができます。これはどうしたらいいのでしょうか?原因が知りたいです。

Block.hpp Player.hpp Scene.hpp部です。
Github 全ソース: https://github.com/Shigurechan/Tank_Game
使っているライブラリ
glm
openGL

#define CELL ((int)48);

イメージ説明

cpp

1// 矩形同士の交差判定 2bool Intersect_Rect_2D(Box_Collision_2D& a,Box_Collision_2D& b) 3{ 4 if (a.getEnable() == true && b.getEnable() == true) 5 { 6 /* 7 printf("a.x %d\n", a.getSize().x); 8 printf("a.y %d\n", a.getSize().y); 9 printf("\n\n"); 10 printf("b.x %d\n", b.getSize().x); 11 printf("b.y %d\n", b.getSize().y); 12 */ 13 14 15 16 // 矩形の衝突判定 17 if (((a.getPosition().x + a.getSize().x > b.getPosition().x) && (a.getPosition().x < b.getPosition().x + b.getSize().x)) 18 && ((a.getPosition().y + a.getSize().y > b.getPosition().y) && (a.getPosition().y < b.getPosition().y + b.getSize().y))) 19 { 20 return true; 21 } 22 else 23 { 24 return false; 25 } 26 } 27 else 28 { 29 return false; 30 } 31} 32

cpp

1 2 3// #################################################################### 4Test::Test(Entry *e) 5{ 6 7 // 8 9 glm::ivec2 pos = glm::ivec2(0,0); 10 ObjectType type = ObjectType::Brick; 11 UV.start = glm::vec2(0, 0); 12 UV.end = glm::vec2(CELL /2,CELL /2); 13 14 position = glm::ivec2(0,0); 15 16 Col.setEnable(true); 17 Col.setMove(glm::ivec2(0, 0)); 18 Col.setSize(glm::ivec2(CELL / 2 ,CELL / 2)); 19 Col.setPosition(pos); 20 21 auto data = LoadTexture("Assets/BlockChip.png"); 22 23 mSprite = std::make_unique<Sprite>(e,data); 24 25 26} 27 28Test::~Test() 29{ 30 31} 32 33 34void Test::Update() 35{ 36 37} 38 39void Test::Draw() 40{ 41 mSprite->DrawGraph(position,UV.start,UV.end); 42}

hpp

1 2class Test 3{ 4public: 5 Test(Entry *e); 6 ~Test(); 7 8 9 void Update(); 10 void Draw(); 11 12 13 Box_Collision_2D Col; 14 class Entry* Owner; 15private: 16 17 std::unique_ptr<Sprite> mSprite; 18 TextureUV UV; 19 glm::ivec2 position; 20 21}; 22

cpp

1 2void Scene::Update() 3{ 4 stage->Update(); 5 player->Update(); 6 7 // プレイヤーとブロックの当たり判定 8 9 for (auto& block : stage->getBlock()) 10 { 11 for (auto& chip : block.getMapChip()) 12 { 13 14 // printf("size.x : %d\n", chip.mCol.getSize().x); 15 // printf("size.y : %d\n",chip.mCol.getSize().y); 16// 17 18 //if (Intersect_Rect_2D(player->mCol, chip.mCol) == true) 19 if (false) 20 { 21 // player->Fix(chip.getPostion(),chip.getObjectType()); 22 //printf("Collision ! %d\n"); 23 } 24 } 25 } 26 27 if (Intersect_Rect_2D(player->mCol, test->Col) == true) 28 { 29 player->Fix(test->Col.getPosition(),ObjectType::Block); 30 printf("Collision ! \n"); 31 32 } 33}

cpp

1 2//描画 3void Sprite::DrawGraph(glm::ivec2 pos, glm::vec2 start, glm::vec2 end) 4{ 5 6 glm::vec2 mSize; 7 mSize = end - start; //画像のサイズを取得 8 if (mSize.x < 0 || mSize.y < 0) 9 { 10 printf("UV指定が不正です。\n"); 11 } 12 Transform_2D::UpdateTransform(); //トランスフォームを更新 13 glm::ivec2 s; 14 //pos.x += s.x = mSize.x / 2; 15 //pos.y -= s.y = mSize.y / 2; 16 Transform_2D::setTransform_Move(pos); //移動 17 18 shader->Enable(); //シェーダーを有効にする 19 shader->SetFloatUniform_3m("uViewMatrix", getViewMatrix()); 20 shader->SetFloatUniform_3m("uWorldMatrix", getWorldMatrix()); 21 22 23// printf("mSize.x %f\n", mSize.x); 24//` printf("start.x. x%f\n", end.x); 25 26 27 28 29 30 31 32 vertex[0] = VertexAttribute_2D{ -mSize.x / 2.0f,mSize.y / 2.0f, start.x / mPicSize.x ,start.y / mPicSize.y }; 33 vertex[1] = VertexAttribute_2D{ -mSize.x / 2.0f,-mSize.y / 2.0f, start.x / mPicSize.x ,end.y / mPicSize.y }; 34 vertex[2] = VertexAttribute_2D{ mSize.x / 2.0f,-mSize.y / 2.0f, end.x / mPicSize.x ,end.y / mPicSize.y }; 35 vertex[3] = VertexAttribute_2D{ -mSize.x / 2.0f,mSize.y / 2.0f, start.x / mPicSize.x ,start.y / mPicSize.y }; 36 vertex[4] = VertexAttribute_2D{ mSize.x / 2.0f,-mSize.y / 2.0f, end.x / mPicSize.x ,end.y / mPicSize.y }; 37 vertex[5] = VertexAttribute_2D{ mSize.x / 2.0f,mSize.y / 2.0f, end.x / mPicSize.x ,start.y / mPicSize.y }; 38 39 40 41 //VAO 42 glBindVertexArray(VAO); 43 44 //VBO 45 glBindBuffer(GL_ARRAY_BUFFER, VBO); 46 glBufferData(GL_ARRAY_BUFFER, (sizeof(vertex) / sizeof(vertex[0])) * sizeof(VertexAttribute_2D), vertex, GL_STATIC_DRAW); 47 48 49 //頂点座標 50 glEnableVertexAttribArray(0); 51 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(VertexAttribute_2D), NULL); 52 53 //UV座標 54 glEnableVertexAttribArray(1); 55 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(VertexAttribute_2D), (void*)(sizeof(float) * 2)); 56 57 glBindVertexArray(VAO); 58 glBindTexture(GL_TEXTURE_2D, TextureID); 59 glDrawArrays(GL_TRIANGLES, 0, (GLsizei)std::size(vertex)); 60 61} 62 63 64

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

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

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

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

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

y_waiwai

2021/02/07 04:30

判定がずれるとは具体的にどうなるんでしょうか。そこらへん詳しく説明しましょう
退会済みユーザー

退会済みユーザー

2021/02/07 04:35

文章修正しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問