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

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

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

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

Q&A

0回答

685閲覧

「ゲーム開発」中心が(0,0)の座標系で画面の左側から右に見て200ピクセルの座標を算出する方法が知りたい

退会済みユーザー

退会済みユーザー

総合スコア0

OpenGL

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

0グッド

0クリップ

投稿2022/02/04 11:12

編集2022/02/04 11:21

提示コードの///コメント部内部のコードですが中心が座標系(0,0)で画面の右から左に見て200ピクセルの座標というのは算出できたのですが逆の左から右に見て200ピクセルの座標はどういった計算式で算出できるのでしょうか?計算式が知りたいです。

cpp

1#include "../header/Player.hpp" 2 3#define SPEED (int)4; 4#define WEAPON_ANIMATION_SPEED ((int)10) 5#define WALK_ANIMATION_SPEED ((int)20) 6#define THROW_SPEED ((int)5) 7#define THROW_MOVE_SPEED ((int)150) 8#define THROW_DISTANCE ((int)300) 9Player::Player() : Actor() 10{ 11 position = glm::ivec2(0,0); 12 throwPosition = glm::ivec2(0,0); 13 throwDistance = 0; 14 speed = SPEED; 15 moveVector = glm::vec2(1,0); 16 17 up_sprite = FrameWork::LoadTexture("Asset/texture/up.png"); 18 down_sprite = FrameWork::LoadTexture("Asset/texture/down.png"); 19 left_sprite = FrameWork::LoadTexture("Asset/texture/left.png"); 20 right_sprite = FrameWork::LoadTexture("Asset/texture/right.png"); 21 22 sword_right_sprite[0] = FrameWork::LoadTexture("Asset/texture/sword_right_0.png"); 23 sword_right_sprite[1] = FrameWork::LoadTexture("Asset/texture/sword_right_1.png"); 24 sword_right_sprite[2] = FrameWork::LoadTexture("Asset/texture/sword_right_2.png"); 25 sword_right_sprite[3] = FrameWork::LoadTexture("Asset/texture/sword_right_3.png"); 26 27 sword_left_sprite[0] = FrameWork::LoadTexture("Asset/texture/sword_left_0.png"); 28 sword_left_sprite[1] = FrameWork::LoadTexture("Asset/texture/sword_left_1.png"); 29 sword_left_sprite[2] = FrameWork::LoadTexture("Asset/texture/sword_left_2.png"); 30 sword_left_sprite[3] = FrameWork::LoadTexture("Asset/texture/sword_left_3.png"); 31 32 sword_up_sprite[0] = FrameWork::LoadTexture("Asset/texture/sword_up_0.png"); 33 sword_up_sprite[1] = FrameWork::LoadTexture("Asset/texture/sword_up_1.png"); 34 sword_up_sprite[2] = FrameWork::LoadTexture("Asset/texture/sword_up_2.png"); 35 sword_up_sprite[3] = FrameWork::LoadTexture("Asset/texture/sword_up_3.png"); 36 37 sword_down_sprite[0] = FrameWork::LoadTexture("Asset/texture/sword_down_0.png"); 38 sword_down_sprite[1] = FrameWork::LoadTexture("Asset/texture/sword_down_1.png"); 39 sword_down_sprite[2] = FrameWork::LoadTexture("Asset/texture/sword_down_2.png"); 40 sword_down_sprite[3] = FrameWork::LoadTexture("Asset/texture/sword_down_3.png"); 41 42 weapon_right_sprite = FrameWork::LoadTexture("Asset/texture/sword_right.png"); 43 weapon_left_sprite = FrameWork::LoadTexture("Asset/texture/sword_left.png"); 44 weapon_up_sprite = FrameWork::LoadTexture("Asset/texture/sword_up.png"); 45 weapon_down_sprite = FrameWork::LoadTexture("Asset/texture/sword_down.png"); 46 47 48 isThrow = false; 49 50 state = State::Wait; 51 52 cameraPosition; 53 54 55 animation_Attack_Time = 0; 56 animation_Attack_Clip = 1; 57 58 animation_Time = 0; 59 animation = false; 60 isMove = false; 61 isAttack = false; 62} 63 64void Player::setCamera(std::shared_ptr<FrameWork::Camera> c) 65{ 66 camera = c; 67 cameraPosition = camera->getPosition(); 68} 69 70 71void Player::Update() 72{ 73 74 75 76 if ((state != State::Attack) && (state != State::Shift)) 77 { 78 speed = SPEED; 79 80 if(FrameWork::GetKeyInput(GLFW_KEY_UP) > 0) 81 { 82 moveVector.y = 1; 83 moveVector.x = 0; 84 85 if(state == State::Wait) 86 { 87 animation = true; 88 } 89 state = State::Walk; 90 } 91 else if(FrameWork::GetKeyInput(GLFW_KEY_DOWN) > 0) 92 { 93 94 95 moveVector.x = 0; 96 moveVector.y = -1; 97 98 if(state == State::Wait) 99 { 100 animation = true; 101 } 102 103 state = State::Walk; 104 105 } 106 else if(FrameWork::GetKeyInput(GLFW_KEY_LEFT) > 0) 107 { 108 moveVector.y = 0; 109 moveVector.x = -1; 110 111 if(state == State::Wait) 112 { 113 animation = true; 114 } 115 state = State::Walk; 116 117 } 118 else if(FrameWork::GetKeyInput(GLFW_KEY_RIGHT) > 0) 119 { 120 moveVector.y = 0; 121 moveVector.x = 1; 122 123 124 if(state == State::Wait) 125 { 126 animation = true; 127 } 128 state = State::Walk; 129 130 } 131 else 132 { 133 speed = 0; 134 state = State::Wait; 135 136 } 137 138 } 139 140 141 if(FrameWork::GetKeyInput(GLFW_KEY_SPACE) == 1) 142 { 143 state = State::Attack; 144 speed = 0; 145 146 } 147 else if(FrameWork::GetKeyInput(GLFW_KEY_LEFT_SHIFT) == 1) 148 { 149 state = State::Shift; 150 speed = 0; 151 152 } 153 154 155 156 157 158 159 //cameraPosition.x += 1; 160 //printf("x %d\n",cameraPosition.x); 161 162 163 //歩行アニメーション 164 if(state == State::Walk) 165 { 166 animation_Time += FrameWork::GetDeltaTime(); 167 if(animation_Time > WALK_ANIMATION_SPEED) 168 { 169 animation_Time = 0; 170 animation = !animation; 171 } 172 } 173 174 175 176 //攻撃アニメーション 177 if( state == State::Attack) 178 { 179 animation_Attack_Time += FrameWork::GetDeltaTime(); 180 if(animation_Attack_Time > WEAPON_ANIMATION_SPEED) 181 { 182 animation_Attack_Time = 0; 183 184 animation_Attack_Clip += 1; 185 if(animation_Attack_Clip > 3 ) 186 { 187 animation_Attack_Clip = 0; 188 189 state = State::Wait; 190 } 191 } 192 } 193 194 195 196 //攻撃アニメーション 197 if( (state == State::Shift) && (isThrow == false) ) 198 { 199 animation_Attack_Time += FrameWork::GetDeltaTime(); 200 if(animation_Attack_Time > WEAPON_ANIMATION_SPEED) 201 { 202 animation_Attack_Time = 0; 203 204 animation_Attack_Clip += 1; 205 if(animation_Attack_Clip > 3 ) 206 { 207 animation_Attack_Clip = 0; 208 209 210 //武器を飛ばすときの初期座標 211 if(moveVector.x > 0) 212 { 213 throwPosition = position + glm::ivec2(20,-3); 214 } 215 else if(moveVector.x < 0) 216 { 217 throwPosition = position + glm::ivec2(-20,-3); 218 } 219 else if(moveVector.y > 0) 220 { 221 throwPosition = position + glm::ivec2(-3,32); 222 } 223 else if(moveVector.y < 0) 224 { 225 throwPosition = position + glm::ivec2(1,-32); 226 } 227 228 229 isThrow = true; 230 } 231 } 232 } 233 234 235 //武器を飛ばす 236 if(isThrow == true) 237 { 238 239 if(throwDistance > THROW_DISTANCE) 240 { 241 242 if(moveVector.x > 0) 243 { 244 position.x += THROW_MOVE_SPEED; 245 if(throwPosition.x < position.x) 246 { 247 position.x = throwPosition.x; 248 cameraPosition.x = position.x; 249 250 throwDistance = 0; 251 isThrow = false; 252 state = State::Wait; 253 254 } 255 } 256 else if(moveVector.x < 0) 257 { 258 position.x += -THROW_MOVE_SPEED; 259 if(throwPosition.x > position.x) 260 { 261 position.x = throwPosition.x; 262 throwDistance = 0; 263 isThrow = false; 264 state = State::Wait; 265 266 } 267 } 268 else if(moveVector.y > 0) 269 { 270 position.y += THROW_MOVE_SPEED; 271 if(throwPosition.y < position.y) 272 { 273 throwPosition.y = position.y; 274 275 throwDistance = 0; 276 isThrow = false; 277 state = State::Wait; 278 279 } 280 } 281 else if(moveVector.y < 0) 282 { 283 position.y += -THROW_MOVE_SPEED; 284 if(throwPosition.y > position.y) 285 { 286 throwPosition.y = position.y; 287 288 throwDistance = 0; 289 isThrow = false; 290 state = State::Wait; 291 292 } 293 } 294 } 295 else 296 { 297 throwPosition += moveVector * (float)THROW_SPEED * FrameWork::GetDeltaTime(); 298 throwDistance += (float)THROW_SPEED * FrameWork::GetDeltaTime(); 299 300 } 301 302 } 303 304 305// printf("x: %d\n",(int)FrameWork::GetWindowSize().x); 306// printf("y: %d\n",(int)FrameWork::GetWindowSize().y); 307// printf("player x: %d\n",(int)position.x); 308// printf("player y: %d\n\n",(int)position.y); 309 310printf("x %d\n",((float)camera->getPosition().x) ); 311 312 if((camera->getPosition().x + 200) < position.x) 313 { 314 printf("right\n"); 315 if(state == State::Walk) 316 { 317 cameraPosition.x += speed; 318 speed = 0; 319 } 320 else if(state == State::Shift) 321 { 322 cameraPosition.x += THROW_MOVE_SPEED; 323 } 324 325 326 } 327 //////////////////////////////////////////////////////////////////////////////////////// 328 else if(camera->getPosition().x - 200 > position.x) 329 ///////////////////////////////////////////////////////////////////////////////////////// 330 { 331 printf("left\n"); 332 cameraPosition.x += -speed; 333 speed = 0; 334 } 335 336 camera->setPosition(glm::ivec3(cameraPosition,1)); 337 338 position += moveVector * (float)FrameWork::GetDeltaTime() * speed; 339 340// printf("x: %f\n",(moveVector * (float)FrameWork::GetDeltaTime() * speed).x); 341// printf("y: %f\n\n",(moveVector * (float)FrameWork::GetDeltaTime() * speed).y); 342 343 344} 345void Player::Renderer(const glm::mat4 view)const 346{ 347 348} 349 350Player::~Player() 351{ 352 353} 354

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

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

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

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

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

vann_2921

2022/02/06 09:21

「画面の右から左に見て200ピクセル」が何基準なのか(原点?, 画面端?)分からないので修正してください。 もし、中心からの座標ということであれば+Xが画面右方向の座標系なら-が左方向です。 右端(左端)からの距離ということであれば中心から右端(左端)までの距離が必要になると思います。 camera->getPosition().x - 200 > position.x には具体的にどんな数字が入りますか?
fana

2022/02/07 02:09

右から左 はできるけども 左から右 はできない というのは,一体どういう話なのか想像もつかない. 前者に関して,何らかの理屈に基づいた計算を行っているのであれば, (ふつーに考えれば)後者側も全く同じ理屈で計算すれば良いだけではないのか? と思うのだが…?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問