[英]How to convert from 2d world coordinates to 2d screen coordinates?
我正在使用 SDL 設計一個基於 2d 平台游戲圖塊的游戲。 我在從世界坐標轉換為屏幕坐標時遇到問題。 我將 (0, 0) 定義為屏幕和世界的左上角,將 (x, y) 定義為右下角。
目前,我像這樣存儲地圖數據:
"." is an air tile
"#" is a grass tile
"i" is an iron tile
"p" is a wooden plank tile
"c" is the top layer of the ice
"b" is the bottom layers of the ice
...iiiiii...pppppp..cccccc...........................................
...iiiiii...pppppp..b....b...........................................
...iiiiii...p....p..b....b...........................................
...iiiiii...p....p..b....b...........................................
...iiiiii...p....p..b....b...........................................
...iiiiii...p....p..b....b...........................................
...iiiiii...p....p..bbbbbb...........................................
...iiiiii...pppppp..bbbbbb...........................................
...iiiiii...pppppp..bbbbbb...........................................
...iiiiii...pppppp..bbbbbb...........................................
...iiiiii...pppppp..bbbbbb...........................................
...iiiiii...pppppp..bbbbbb...........................................
#####################################################################
.....................................................................
.....................................................................
.....................................................................
.....................................................................
.....................................................................
.....................................................................
.....................................................................
.....................................................................
.....................................................................
.....................................................................
.....................................................................
....................................................................#
我像這樣加載地圖,其中左上角是 (0, 0):
typedef struct Tile {
char texture_id;
int x;
int y;
} Tile;
Map::Map(std::string raw_map_data) {
// Initialize private variables
this->unparsed_map_data= raw_map_data;
// Split the map data into a std::vector<std::string> based off of newlines
std::vector<std::string> map_data_split = split_string(raw_map_data, "\n");
// Next, convert the map data to a vector Tile structs
int cur_x = 0;
int cur_y = 0;
std::vector<Tile> tiles_processed;
Tile tile;
for (size_t i = 0; i < map_data_split.size(); i++) {
for (size_t j = 0; j < map_data_split[i].size(); j++) {
tile.x = cur_x;
tile.y = cur_y;
tile.texture_id = map_data_split[i][j];
tiles_processed.push_back(tile);
cur_x += TILE_WIDTH;
}
cur_x = 0;
cur_y += TILE_HEIGHT;
}
this->tiles_parsed = tiles_processed;
//std::reverse(this->tiles_parsed.begin(), this->tiles_parsed.end());
DebugTiles(this->tiles_parsed); // DEBUG: used to print tiles to find errors
}
我已將相機坐標設置為窗口左上角的世界坐標。 此外,玩家永遠不會離開屏幕的中心。 此外,我用來從世界坐標到屏幕坐標的轉換如下: screen coordinates = camera coordinates - world coordinates of tile
void Camera::Render(std::vector<Tile> tiles_to_render, SDL_Texture* player_texture) {
// Render the scene
for (size_t i = 0; i < tiles_to_render.size(); i++) {
if (tiles_to_render[i].texture_id != '.') {
// world_pos_tl is an SDL_Rect containing the camera coordinates
SDL_Rect dstrect = {this->world_pos_tl.x - tiles_to_render[i].x, this->world_pos_tl.y - tiles_to_render[i].y, TILE_WIDTH, TILE_HEIGHT};
SDL_RenderCopy(this->renderer, this->textures[tiles_to_render[i].texture_id], NULL, &dstrect);
}
}
// Render the character (always in center of screen)
SDL_Rect player_dstrect = {(WIDTH / 2) - TILE_WIDTH, (HEIGHT / 2) - TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT};
SDL_RenderCopy(this->renderer, player_texture, NULL, &player_dstrect);
}
但是,當我編譯並運行時,我得到以下結果,因為世界旋轉了 270 度。
運行代碼的結果:
特別感謝“One Lone Coder”不和諧服務器上的Gorbit99為我解決了這個問題。
問題是它應該是屏幕位置 = 世界位置 - 相機位置。 這是因為當你向左移動一個物體而相機保持靜止時,物體應該向左移動。 但是如果相機向左移動而物體保持靜止,它應該看起來像物體向右移動。
旁注:我過度使用關鍵字, this
很多,因為它僅在函數中的變量與類成員同名並且您想指定要使用類成員時使用。 因此,在實踐中,關鍵字this
應該很少使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.