使っている画像の大きさが144*96のサイズなので、横に56移動させた位置から、32*32の当たり判定を3つ縦に並べて作っています。
またmove[]は0が上でそこから時計回りに3までのフラグになっており
coll_pos[]は、左上から時計回りに、位置を設定しています。
実行した場合、振動しているような状態になります。
また、あたいがずれる点があり、それは重力を画像の座標へ代入するときで、前後でprintfをとり確認しました
これを改善する場合どうすべきでしょうか?
c++とcはある程度できるようにはなりましたが、深くまではまだ理解していません。
関連があるとおもわれるコードを乗せます。
回答よろしくお願いします。
不備、状況がわからない等ございましたら、申し訳有りません。
//Collision.cpp
#include "AP_Collision.h"
Collision::Collision(){
}
void Collision::Bottom_Collision(Player &player,Maps &maps){
//当たり判定位置生成
for(int i = 0;i < 6; i++) coll_pos[i] = player.Point_Coll(i);
player_pos = player.Point_Player();
if(maps.checkObjct(coll_pos[3].x,coll_pos[3].y+32) == 1 || maps.checkObjct(coll_pos[4].x,coll_pos[4].y+32) == 1){
player_pos.y = player_pos.y/32*32;
player.MoveFlagsSet(false,2);
player.SetFloatFlag(false);
}else{
player.MoveFlagsSet(true,2);
player.SetFloatFlag(false);
}
player.PlayerPointSet(player_pos.x,player_pos.y);
}
#include "AP_Chara.h"
#include "stdio.h"
Player::Player(){
//初期位置
player_pos.x = 0;
player_pos.y = 0;
//初期あたり判定位置
LColl_head.x = LColl_center.x = LColl_bottom.x = player_pos.x+56;
LColl_head.y = RColl_head.y = player_pos.y;
LColl_center.y = RColl_center.y = player_pos.y+32;
LColl_bottom.y = RColl_bottom.y = player_pos.y+64;
RColl_head.x = RColl_center.x = RColl_bottom.x = LColl_head.x+31;
//グラフィック
LoadDivGraph("Fighter_1.png",16,4,4,144,96,image) ;
LoadDivGraph("Atack.png",16,4,4,144,96,Atack_image);
//カウンタ初期化
gravity_count=0;
cnt=0;
index=0;
Atack_Part=0;
//初期記憶
for(int i=0;i<9;i++){
px[i]=py[i]=-1;
}
//初期HP値
hp=3;
g=2;
py[0]=player_pos.y;
//機能変数初期値
direct=true;
jump=false;
floating=false;
move_flag=true;
dead=false;
gravity=true;
for(int i=0; i < 4; i++){
move[i] = true;
}
}
void Player::draw(){
//円の描画
if(direct){
if(gravity){
if(Key[KEY_INPUT_LEFT]>0||Key[KEY_INPUT_RIGHT]>0)
DrawTurnGraph(player_pos.x,player_pos.y,image[8+index],TRUE);
if(Key[KEY_INPUT_LEFT]==0&&Key[KEY_INPUT_RIGHT]==0)
DrawTurnGraph(player_pos.x,player_pos.y,image[index],TRUE);
}else{
if(Key[KEY_INPUT_LEFT]>0||Key[KEY_INPUT_RIGHT]>0)
DrawRotaGraph(player_pos.x+72,player_pos.y+48,1.0,PI,image[8+index],TRUE,FALSE);
if(Key[KEY_INPUT_LEFT]==0&&Key[KEY_INPUT_RIGHT]==0)
DrawRotaGraph(player_pos.x+72,player_pos.y+48,1.0,PI,image[index],TRUE,FALSE);
}
}else{
if(gravity){
if(Key[KEY_INPUT_LEFT]>0||Key[KEY_INPUT_RIGHT]>0)
DrawGraph(player_pos.x,player_pos.y,image[8+index],TRUE);
if(Key[KEY_INPUT_LEFT]==0&&Key[KEY_INPUT_RIGHT]==0)
DrawGraph(player_pos.x,player_pos.y,image[index],TRUE);
}else{
if(Key[KEY_INPUT_LEFT]>0||Key[KEY_INPUT_RIGHT]>0)
DrawRotaGraph(player_pos.x+72,player_pos.y+48,1.0,PI,image[8+index],TRUE,TRUE);
if(Key[KEY_INPUT_LEFT]==0&&Key[KEY_INPUT_RIGHT]==0)
DrawRotaGraph(player_pos.x+72,player_pos.y+48,1.0,PI,image[index],TRUE,TRUE);
}
}
DrawFormatString(10,10,GetColor(255,255,255),"x:%d,y:%d",player_pos.x,player_pos.y);
DrawFormatString(10,25,GetColor(255,255,255),"HP:%d",hp);
DrawFormatString(10,40,GetColor(255,255,255),"gravity:%d",gravity);
}
//操作関数
void Player::KeyInputFunc(){
//移動
if(Key[KEY_INPUT_LEFT]>0){
if(move[3]) player_pos.x-=4;
direct=false;
}
//移動
if(Key[KEY_INPUT_RIGHT]>0){
if(move[1]) player_pos.x+=4;
direct=true;
}
//重力転換
if(Key[KEY_INPUT_G]){
if(gravity){
if(gravity_count <= 0){
gravity_count = 30;
gravity=false;
}
}else{
if(gravity_count <= 0){
gravity_count = 30;
gravity=true;
}
}
}
//ジャンプ
if(Key[KEY_INPUT_SPACE]==1&&!floating){
if(gravity){
g=-15;
floating = true;
}else{
g=15;
floating = true;
}
}
//インデックス
if(Key[KEY_INPUT_LEFT]>0||Key[KEY_INPUT_RIGHT]>0){
// ダッシュ時
if(cnt%10==1){
if(index == 3){ index = 0;
}else{ index += 1; }
}
}else{
// 待機時
if(cnt%15==1){
if(index == 3){ index = 0;
}else{ index += 1; }
}
}
}
//マップ変更関数
void Player::AreaJump(int area,int px,int py){
player_pos.x=px;
player_pos.y=py;
}
//重力
void Player::Gravty_Updata(){
//重力設定
y_move=(player_pos.y-py[0])+g;
//移動量制限
if(y_move>31&&gravity){
y_move=31;
}else if(y_move<-31&&!gravity){
y_move=-31;
}
//前回座標を記憶
py[0]=player_pos.y; //ここで位置がずれる
//反映
player_pos.y+=y_move; /*ここで値がずれる*/
//gを戻す
if(gravity){
g=1;
}else{
g=-1;
}
}
//フレーム更新
void Player::update(){
Gravty_Updata();
cnt+=1;
gravity_count -= 1;
//重力
Gravty_Updata();
//操作関数
KeyInputFunc();
PrintData();
LColl_head.x = LColl_center.x = LColl_bottom.x = player_pos.x+56;
LColl_head.y = RColl_head.y = player_pos.y;
LColl_center.y = RColl_center.y = player_pos.y+32;
LColl_bottom.y = RColl_bottom.y = player_pos.y+64;
RColl_head.x = RColl_center.x = RColl_bottom.x = LColl_head.x+31;
}
bool Player::Gravity(){
return gravity;
}
t_Pos Player::Point_Player(){
return player_pos;
}
t_Pos Player::Point_Coll(int num){
switch(num){
case 0:
return LColl_head;
break;
case 1:
return RColl_head;
break;
case 2:
return RColl_center;
break;
case 3:
return RColl_bottom;
break;
case 4:
return LColl_bottom;
break;
case 5:
return LColl_center;
break;
}
ErrorMesBox("エラーが発生しました(Chara.cpp/229行目)",END);
return RColl_head;
}
void Player::PlayerPointSet(int x,int y){
player_pos.x = x;
player_pos.y = y;
}
void Player::MoveFlagsSet(bool flags,int num){
if(num<4) move[num] = flags;
return ;
}
void Player::SetFloatFlag(bool flag){
floating = flag;
}
void Player::PrintData(){
printf("-------------------------------------------------------\n");
printf("x:%d y:%d\nGravity:%d direct:%d\n",player_pos.x,player_pos.y,gravity,direct);
printf("MoveFlag [0] = %d,[1] = %d,[2] = %d,[3] = %d",move[0],move[1],move[2],move[3]);
}
#include "AP_Maps.h"
Maps::Maps(){
for(int i=0;i<14;i++){
for(int j=0;j<20;j++){
maps[i][j]=0;
}
}
for(int j=0;j<20;j++){
maps[14][j]=1;
maps[0][j]=2;
}
for(int i=0;i<14;i++){
maps[i][ 0]=1;
maps[i][19]=1;
}
LoadDivGraph("map.png",152,8,19,32,32,chips) ;
}
int Maps::checkPos_x(int mx){
return mx/32;
}
int Maps::checkPos_y(int my){
return my/32;
}
int Maps::checkObjct(int mx,int my){
return maps[checkPos_y(my)][checkPos_x(mx)];
}
void Maps::draw(){
for(int i=0;i<15;i++){
for(int j=0;j<20;j++){
if(maps[i][j]==1){
DrawGraph(j*32,i*32,chips[9],TRUE);
}else if(maps[i][j]==2){
DrawRotaGraph(j*32+16,i*32+16,1.0,PI,chips[9],TRUE,FALSE);
}
}
}
}