【发布时间】:2014-03-17 11:02:56
【问题描述】:
我在碰撞检测方面工作了几天,现在我的代码真的很盲目:/。一切正常,除了玩家位置重置。 我的碰撞检测函数返回相交的矩形和 1,2,5 或 6 1=敌人左上角碰撞,2=敌人左下角碰撞,5=敌人右下角碰撞,6=敌人右上角碰撞。玩家信息如下:
X=Current X Position;
Y=Current Y Position (the higher Y the lower is the Player at the Screen);
X_INIT= the next X Position of the Player;
Y_Init= the next Y Position of the Player;
Player_width= :);
Player_height= :);
在播放器的下一个位置检测到碰撞。 现在,如果玩家从左侧(或右侧)移动,他应该停在敌人处。 如果他是从顶部跌落到他身上,跌落也应该停止。 如果他正在跌倒并向他移动,例如跳,他应该停在左边,或者,如果他足够高,他应该落在上面。
sf::IntRect rect_player(player->initialpos.x, player->initialpos.y, player->size.x, player->size.y);
sf::IntRect rect_tile(pos.x, pos.y, 50, 50);
s_collision_det collision=collide(rect_player,rect_tile);
float x=player->pos.x;
float y=player->pos.y;
float xx=player->initialpos.x;
float yy=player->initialpos.y;
float sx=player->size.x;
float sy=player->size.y;
//float sy=player->size.y;
float stx=collision.colliding_rect.left;
float sty=collision.colliding_rect.top;
float stsx=collision.colliding_rect.width;
//float stsy=collision.colliding_rect.height;
if (collision.collision==1){
cout<<"im in 1++";
cout << stx<<"##"<< xx+sx;
if (y>=yy){offset_up(player,rect_tile);}
//if ((xx+sx>stx)and(sty!=y+sy)){offset_left(player,rect_tile);};
//if (x+sx>=stx and xx+sx>=stx and y > yy ){offset_down(player,rect_tile);};
}else if (collision.collision==2){
cout<<"im in 2++";
if ((x+sx<=stx or xx+sx>stx)and(sty!=y+sy)){offset_left(player,rect_tile);};
//if ((x>=stx+stsx or xx>stx+stsx) and (sty!=y+sy)){offset_right(player,rect_tile);};
//if (x+sx>=stx and xx+sx>=stx and y < yy ){offset_up(player,rect_tile);};
//if (x+sx>=stx and xx+sx>=stx and y > yy ){offset_down(player,rect_tile);};
}else if (collision.collision==5){
cout<<"im in 5++";
if ((x>=stx+stsx or xx>stx+stsx) and (sty!=y+sy)){offset_right(player,rect_tile);};
//if (x<=stx+stsx and xx<=stx+stsx and y < yy ){offset_up(player,rect_tile);};
//if (x<=stx+stsx and xx<=stx+stsx and y > yy ){offset_down(player,rect_tile);};
}else if (collision.collision==6){
cout<<"im in 6++";
if ((x>=stx+stsx or xx>stx+stsx) and (sty!=y+sy)){offset_right(player,rect_tile);};
//if (x<=stx+stsx and xx<=stx+stsx and y < yy ){offset_up(player,rect_tile);};
//if (x<=stx+stsx and xx<=stx+stsx and y > yy ){offset_down(player,rect_tile);};
}
注释的条件是我已经尝试过的一些,但没有奏效。 我的想法有什么错误? :) 感谢您的帮助。 PS:请原谅我的英语有些语法错误,这不是我的母语。 附言: 偏移函数为:
void offset_left(character *player, sf::IntRect tile){
player->initialpos.x=tile.left-player->size.x;
player->x_speed=0;
cout<<"Offset_Left++";
};
void offset_right(character *player, sf::IntRect tile){
player->initialpos.x=tile.left+tile.width;
player->x_speed=0;
cout<<"Offset_Right++";
};
void offset_up(character *player, sf::IntRect tile){
player->initialpos.y=tile.top-player->size.y;
player->collision.bottom=1;
player->y_speed=0;
cout<<"Offset_Up++";
};
void offset_down(character *player, sf::IntRect tile){
player->initialpos.y=tile.top+tile.height;
player->y_speed=0;
cout<<"Offset_Down++";
};
【问题讨论】:
-
我建议你先专注于算法,然后再尝试实现一些东西。您的代码非常难以阅读,因此从头开始重新实现它比将其提交提交更容易。
-
感谢您的建议,我对 c++ 应用程序编程很陌生,这是我的第一个更大的项目,因此我不习惯在类和算法中思考 :),但我真的很努力超出程序编程。