ブロック崩しの続きとして,バーに移動できない範囲を設定しようと思っています。
例えば,元の移動できる範囲から
□□□□□□□□□□□□□□□
□□□□□□□□□■■□□□□
□□□□□□□□□■■□□□□
□□□□□□□□□□□□□□□
上の黒い四角の範囲に移動できなくなったりするように考えています。
そこで,バーと四角の当たり判定を考えて,
バーが四角の左側にあたったときバーの位置のx座標が四角の左側になるようにしました。
最初は
if(HitRectAndRect(hit,bar->hit)== TRUE){
//左側
if(bar->hit.rb.x >= hit.lt.x &&
bar->hit.lt.x <= hit.lt.x){
bar->x = hit.lt.x-12;
}
//右側
if(bar->hit.lt.x <= hit.rb.x &&
bar->hit.rb.x >= hit.rb.x){
bar->x = hit.rb.x +50;
}
}
if(bar->hit.lt.x > hit.lt.x - bar->hit.width+2 &&
bar->hit.rb.y < hit.rb.y + bar->hit.width-2){
//上側
if(bar->hit.rb.y >= hit.lt.y &&
bar->hit.lt.y <= hit.lt.y){
bar->y = hit.lt.y - 5;
}
//下側
if(bar->hit.lt.y <= hit.rb.y &&
bar->hit.rb.y >= hit.rb.y){
bar->y = hit.rb.y + 20;
}
}
例えば,バーを上側からあてたとき
上と左の条件を満たしてしまい,バーのy座標だけでなく,x座標も変わってしまいました。
そこで,今度は
横からあたるとき バーの上辺のx<四角の上辺のx - バーの高さ かつ バーの下辺のx>四角の下辺のx +バーの高さ
縦からあたるとき バーの左辺のx<四角の左辺のx - バーの幅 かつ バーの右辺のy>四角の右辺のy + バーの幅
if(HitRectAndRect(hit,bar->hit)== TRUE){
if(bar->hit.lt.y > hit.lt.y - bar->hit.height+2 &&
bar->hit.rb.y < hit.rb.y + bar->hit.height-2){
//左側
if(bar->hit.rb.x >= hit.lt.x &&
bar->hit.lt.x <= hit.lt.x){
bar->x = hit.lt.x-12;
}
//右側
if(bar->hit.lt.x <= hit.rb.x &&
bar->hit.rb.x >= hit.rb.x){
bar->x = hit.rb.x +50;
}
}
if(bar->hit.lt.x > hit.lt.x - bar->hit.width+2 &&
bar->hit.rb.y < hit.rb.y + bar->hit.width-2){
//上側
if(bar->hit.rb.y >= hit.lt.y &&
bar->hit.lt.y <= hit.lt.y){
bar->y = hit.lt.y - 5;
}
//下側
if(bar->hit.lt.y <= hit.rb.y &&
bar->hit.rb.y >= hit.rb.y){
bar->y = hit.rb.y + 20;
}
}
}
なにがいけなかったのでしょうか?
また,これよりいい方法があるという場合,どうか教えてください。お願いします。