こんにちは。今横スクロールゲームを作っているのですが、スクロールを行うと床として置いている画像の位置がずれてしまいます。教えていただけないでしょうか。
コード:
void DrawGameMain(){
//スクロール座標調整
b2Vec2 pos = g_stage.hero->GetPosition();
int hx = (int)(pos.x * WLDSC);
int hy = (int)(pos.y * WLDSC);
if(hx - SCROLL_LIMIT < g_stage.screen_x){
g_stage.screen_x = hx - SCROLL_LIMIT;
}
if(640 - SCROLL_LIMIT < hx - g_stage.screen_x){
g_stage.screen_x = hx + SCROLL_LIMIT - 640;
}
if(hy - SCROLL_LIMIT < g_stage.screen_y){
g_stage.screen_y = hy - SCROLL_LIMIT;
}
if(480 - SCROLL_LIMIT < hy - g_stage.screen_y){
g_stage.screen_y = hy + SCROLL_LIMIT - 480;
}
DrawEnemyAndMap(); //地形描画
//衝突チェック
g_stage.isontheground = false;
Collision();
HeroState hstate = HERO_STANDING;
int key = GetJoypadInputState(DX_INPUT_KEY_PAD1);
//主人公の位置と移動ベクトル
b2Vec2 vec = g_stage.hero->GetLinearVelocity();
float angle = g_stage.hero->GetAngle();
//キーチェック
if(g_stage.isontheground == true){
//左右移動
if(key & PAD_INPUT_LEFT){
vec.x = -PHS(120.0f);
g_stage.hero->SetLinearVelocity(vec);
hstate = HERO_RUNNING;
g_stage.isheroleft = true;
}
if(key & PAD_INPUT_RIGHT){
vec.x = PHS(120.0f);
g_stage.hero->SetLinearVelocity(vec);
hstate = HERO_RUNNING;
g_stage.isheroleft = false;
}
//ジャンプ
if(IsBKeyTrigger(key) == true){
vec.x = 0;
vec.y = -62.0f;
g_stage.hero->ApplyLinearImpulse(vec, pos);
hstate = HERO_JUMP;
g_stage.isontheground = false;
}
}
//ジャンプ姿勢へ
if(g_stage.isontheground == false) hstate = HERO_JUMP;
//転倒対策
if(fabs(angle) > 0.2f) g_stage.hero->SetAngularVelocity(-angle*2);
DrawHero(hstate); //主人公描画
if(IsAKeyTrigger(key) == true) GoGameOver();
}
コード:
b2Body* CreateBox(float x, float y, float w, float h, float angle, bool dynamic){
//ボディ定義
b2BodyDef bodyDef;
if(dynamic) bodyDef.type = b2_dynamicBody; //動的ボディ
else bodyDef.type = b2_staticBody; //静的ボディ
bodyDef.position.Set(x, y);
bodyDef.angle = angle;
//ボディ作成
b2Body* body = g_world.CreateBody(&bodyDef);
//シェイプ作成
b2PolygonShape staticBox;
staticBox.SetAsBox(w, h);
//フィクスチャ定義
b2FixtureDef fixtureDef;
fixtureDef.shape = &staticBox;
fixtureDef.density = 4.0f; //密度
fixtureDef.restitution = 0.2f; //反発力
body->CreateFixture(&fixtureDef);
return body;
}
コード:
//マップの読み込み
int LoadMapData(char *filepath){
int f; //ファイルハンドル
char buf[1024]; //テキスト読み込みバッファ
f = FileRead_open(filepath);
if(f == 0) return -1; //読み込みエラー
//マップサイズと主人公初期位置読み込み
if(FileRead_gets(buf, 1023, f) == -1) return -1;
float w, h, sx, sy;
sscanf_s(buf, "%f, %f, %f, %f", &w, &h, &sx, &sy);
g_stage.mapsize_w = w;
g_stage.mapsize_h = h;
//主人公キャラクター
g_stage.hero = CreateBox(PHS(sx), PHS(sy), PHS(23), PHS(48), 0, true);
//地形オブジェクトの読み込み
if(FileRead_gets(buf, 1023, f) == -1) return -1;
int imax;
sscanf_s(buf, "%d", &imax); //行数取得
float x, y, angle;
int id;
for(int i = 0; i < imax; i++){
if(FileRead_gets(buf, 1023, f) == -1) return -1;
sscanf_s(buf, "%f, %f, %f, %f, %f, %d", &x, &y, &w, &h, &angle, &id);
//ボディ作成
Character ch;
ch.body = CreateBox(PHS(x), PHS(y), PHS(w / 2), PHS(h / 2), angle, false);
if((int)w == 48) ch.ID = WALL_48;
if((int)w == 192) ch.ID = WALL_192;
if((int)w == 284) ch.ID = WALL_284;
if((int)w == 568) ch.ID = WALL_568;
if(id == 1) ch.ID = GOAL_FLAG;
ch.used = true;
g_stage.wall[g_stage.num_mapchara] = ch;
g_stage.num_mapchara++;
}
FileRead_close(f);
return 0;
}
//主人公の描画
void DrawHero(int hstate){
b2Vec2 pos = g_stage.hero->GetPosition();
float angle = g_stage.hero->GetAngle();
//キャラクター描画
int animpat = (g_lasttime / (1000 / 12)) % 4;
switch(hstate){
case HERO_STANDING:
DrawRotaGraph(VIWX(pos.x), VIWY(pos.y), 1, angle, g_images.hero[0], TRUE, g_stage.isheroleft);
break;
case HERO_RUNNING:
DrawRotaGraph(VIWX(pos.x), VIWY(pos.y), 1, angle, g_images.hero[1 + animpat], TRUE, g_stage.isheroleft);
break;
case HERO_JUMP:
DrawRotaGraph(VIWX(pos.x), VIWY(pos.y), 1, angle, g_images.hero[5], TRUE, g_stage.isheroleft);
break;
}
}
//地形と雪だまの描画
void DrawEnemyAndMap(){
//地形の描画
for(int i = 0; i < g_stage.num_mapchara; i++){
if(g_stage.wall[i].used == false) continue;
b2Vec2 pos = g_stage.wall[i].body->GetPosition();
float angle = g_stage.wall[i].body->GetAngle();
int id = g_stage.wall[i].ID;
DrawRotaGraph(VIWX(pos.x), VIWX(pos.y), 1, angle, g_images.wall[id], TRUE);
}
}