Skip to content

Commit 4c02cd6

Browse files
committed
Move coord convert from c++ => go (invert y)
1 parent af30e42 commit 4c02cd6

File tree

8 files changed

+29
-62
lines changed

8 files changed

+29
-62
lines changed

modules/spx/spx_debug_mgr.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ void SpxDebugMgr::debug_draw_circle(GdVec2 pos, GdFloat radius, GdColor color) {
7171
return;
7272
}
7373

74-
pos.y = -pos.y;
7574
Line2D* circle = memnew(Line2D);
7675
circle->set_default_color(color);
7776
circle->set_width(2.0f);
@@ -104,8 +103,7 @@ void SpxDebugMgr::debug_draw_rect(GdVec2 pos, GdVec2 size, GdColor color) {
104103
Line2D* rect = memnew(Line2D);
105104
rect->set_default_color(color);
106105
rect->set_width(2.0f);
107-
108-
pos.y = -pos.y;
106+
109107
size = size * 0.5;
110108
PackedVector2Array points;
111109
points.append(Vector2(-size.x, -size.y));
@@ -132,10 +130,6 @@ void SpxDebugMgr::debug_draw_line(GdVec2 from, GdVec2 to, GdColor color) {
132130
return;
133131
}
134132

135-
// 翻转Y轴坐标
136-
from.y = -from.y;
137-
to.y = -to.y;
138-
139133
Line2D* line = memnew(Line2D);
140134
line->set_default_color(color);
141135
line->set_width(2.0f);

modules/spx/spx_draw_tiles.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,10 @@ void SpxDrawTiles::place_tile_spx(GdVec2 pos, GdString texture_path, GdInt index
275275
}
276276

277277
void SpxDrawTiles::erase_tile_spx(GdVec2 pos, GdInt layer_index) {
278-
auto flipped_pos = flip_y(pos);
279-
280278
auto erase_at_layer = [&](TileMapLayer* layer) {
281279
if (!layer) return;
282280

283-
Vector2 local_pos = layer->to_local(flipped_pos);
281+
Vector2 local_pos = layer->to_local(pos);
284282
Vector2i coords = layer->local_to_map(local_pos);
285283
layer->erase_cell(coords);
286284
};
@@ -304,7 +302,7 @@ GdString SpxDrawTiles::get_tile_spx(GdVec2 pos, GdInt layer_index) {
304302

305303
if(index_layer_map.has(layer_index)){
306304
auto layer = index_layer_map[layer_index];
307-
Vector2 local_pos = layer->to_local(flip_y(pos));
305+
Vector2 local_pos = layer->to_local(pos);
308306
Vector2i coords = layer->local_to_map(local_pos);
309307
return SpxReturnStr(_get_tile_texture_path(layer, coords));
310308
}
@@ -332,7 +330,7 @@ void SpxDrawTiles::set_tile_texture_spx(GdString texture_path, const Vector<Vect
332330
}
333331

334332
void SpxDrawTiles::erase_tile_spx(GdVec2 pos) {
335-
place_or_erase_tile(flip_y(pos), true);
333+
place_or_erase_tile(pos, true);
336334
}
337335

338336
void SpxDrawTiles::_place_tiles_bulk_spx(GdArray positions) {
@@ -353,15 +351,15 @@ void SpxDrawTiles::_place_tiles_bulk_spx(GdArray positions) {
353351
auto y = *(SpxBaseMgr::get_array<float>(positions, i + 1));
354352

355353
Vector2 pos = {x, y};
356-
Vector2 local_pos = layer->to_local(flip_y(pos));
354+
Vector2 local_pos = layer->to_local(pos);
357355
Vector2i coords = layer->local_to_map(local_pos);
358356

359357
layer->set_cell(coords, source_id, default_atlas_coord, 0);
360358
}
361359
}
362360

363361
void SpxDrawTiles::_place_tile_spx(GdVec2 pos) {
364-
place_or_erase_tile(flip_y(pos), false);
362+
place_or_erase_tile(pos, false);
365363
}
366364

367365
void SpxDrawTiles::set_layer_index(int index) {
@@ -376,7 +374,7 @@ void SpxDrawTiles::set_layer_offset_spx(int layer_index, Vector2 offset) {
376374
return;
377375
}
378376

379-
layer->set_position(flip_y(offset));
377+
layer->set_position(offset);
380378
}
381379

382380
Vector2 SpxDrawTiles::get_layer_offset_spx(int layer_index) {
@@ -385,8 +383,7 @@ Vector2 SpxDrawTiles::get_layer_offset_spx(int layer_index) {
385383
return Vector2();
386384
}
387385

388-
auto pos = layer->get_position();
389-
return flip_y(pos);
386+
return layer->get_position();
390387
}
391388

392389
void SpxDrawTiles::set_texture(Ref<Texture2D> texture, bool with_collision) {

modules/spx/spx_draw_tiles.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ class SpxDrawTiles : public Node2D {
238238

239239
void _place_tiles_bulk_spx(GdArray positions);
240240
void _place_tile_spx(GdVec2 pos);
241-
_FORCE_INLINE_ Vector2 flip_y(const Vector2 &pos) { return pos * Vector2(1, -1); }
242241

243242
void _destroy_layers();
244243
void _clear_cache();

modules/spx/spx_input_mgr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ void SpxInputMgr::on_start() {
4444

4545
// input
4646
GdVec2 SpxInputMgr::get_global_mouse_pos() {
47-
auto mouse_pos = cameraMgr->get_global_mouse_position();
48-
return GdVec2(mouse_pos.x, -mouse_pos.y);
47+
return cameraMgr->get_global_mouse_position();
4948
}
5049

5150
GdBool SpxInputMgr::get_mouse_state(GdInt mouse_id) {

modules/spx/spx_path_finder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ void SpxPathFinder::set_sprite_obstacle(GdObj obj, bool enabled) {
131131
}
132132

133133
GdArray SpxPathFinder::find_path_spx(GdVec2 p_from, GdVec2 p_to) {
134-
auto path_points = find_path(p_from * Vector2(1, -1), p_to * Vector2(1, -1));
134+
auto path_points = find_path(p_from, p_to);
135135
auto count = path_points.size();
136136
GdArray result = SpxBaseMgr::create_array(GD_ARRAY_TYPE_FLOAT, count * 2);
137137

138138
for(auto i = 0; i < count; i ++){
139139
auto idx = i * 2;
140140
SpxBaseMgr::set_array(result, idx, path_points[i].x);
141-
SpxBaseMgr::set_array(result, idx + 1, -path_points[i].y);
141+
SpxBaseMgr::set_array(result, idx + 1, path_points[i].y);
142142
}
143143

144144
return result;

modules/spx/spx_physic_mgr.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ SpxRaycastInfo SpxPhysicMgr::_raycast(GdVec2 from, GdVec2 to,GdArray ignore_spri
105105
info.normal = GdVec2{0, 0};
106106
info.sprite_gid = 0;
107107

108-
// invert y
109-
GdVec2 current_from = GdVec2{from.x, -from.y};
110-
GdVec2 target_to = GdVec2{to.x, -to.y};
111-
112108
HashSet<RID> ignore_set;
113109
if(ignore_sprites && ignore_sprites->size > 0){
114110
GdObj* sprite_data = (SpxBaseMgr::get_array<GdObj>(ignore_sprites, 0));
@@ -126,8 +122,8 @@ SpxRaycastInfo SpxPhysicMgr::_raycast(GdVec2 from, GdVec2 to,GdArray ignore_spri
126122
}
127123
PhysicsDirectSpaceState2D::RayResult result;
128124
PhysicsDirectSpaceState2D::RayParameters params;
129-
params.from = current_from;
130-
params.to = target_to;
125+
params.from = from;
126+
params.to = to;
131127
params.collision_mask = (uint32_t)collision_mask;
132128
params.collide_with_areas = collide_with_areas;
133129
params.collide_with_bodies = collide_with_bodies;
@@ -146,8 +142,8 @@ SpxRaycastInfo SpxPhysicMgr::_raycast(GdVec2 from, GdVec2 to,GdArray ignore_spri
146142
SpxSprite *collider = dynamic_cast<SpxSprite *>(result.collider);
147143
GdObj current_gid = collider ? collider->get_gid() : 0;
148144
info.collide = true;
149-
info.position = GdVec2{result.position.x, -result.position.y};
150-
info.normal = GdVec2{result.normal.x, -result.normal.y};
145+
info.position = result.position;
146+
info.normal = result.normal;
151147
info.sprite_gid = current_gid;
152148
return info;
153149
}
@@ -165,9 +161,6 @@ GdObj SpxPhysicMgr::raycast(GdVec2 from, GdVec2 to, GdInt collision_mask) {
165161

166162
PhysicsDirectSpaceState2D::RayResult result;
167163
PhysicsDirectSpaceState2D::RayParameters params;
168-
// flip y axis
169-
from = GdVec2{ from.x, -from.y };
170-
to = GdVec2{ to.x, -to.y };
171164
params.from = from;
172165
params.to = to;
173166
params.collision_mask = (uint32_t)collision_mask;
@@ -186,10 +179,6 @@ GdBool SpxPhysicMgr::check_collision(GdVec2 from, GdVec2 to, GdInt collision_mas
186179
PhysicsDirectSpaceState2D *space_state = node->get_world_2d()->get_direct_space_state();
187180
PhysicsDirectSpaceState2D::RayResult result;
188181
PhysicsDirectSpaceState2D::RayParameters params;
189-
190-
// flip y axis
191-
from = GdVec2{ from.x, -from.y };
192-
to = GdVec2{ to.x, -to.y };
193182
params.from = from;
194183
params.to = to;
195184
params.collision_mask = (uint32_t)collision_mask;
@@ -297,8 +286,7 @@ GdArray SpxPhysicMgr::_check_collision(RID shape, GdVec2 pos, GdInt collision_ma
297286
return create_array(GD_ARRAY_TYPE_GDOBJ, 0);
298287
}
299288

300-
GdVec2 flipped_pos = GdVec2{ pos.x, -pos.y };
301-
Transform2D query_transform(0, flipped_pos);
289+
Transform2D query_transform(0, pos);
302290

303291
PhysicsDirectSpaceState2D::ShapeParameters params;
304292
params.shape_rid = shape;

modules/spx/spx_scene_mgr.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ GdObj SpxSceneMgr::create_render_sprite(GdString texture_path, GdVec2 pos, GdFlo
228228
}
229229

230230
SpxRenderSprite* sprite = memnew(SpxRenderSprite);
231-
sprite->set_pivot(GdVec2(pivot.x, -pivot.y));
231+
sprite->set_pivot(pivot);
232232
auto path_str = SpxStr(texture_path);
233233
Ref<Texture2D> texture = resMgr->load_texture(path_str, true);
234234
sprite->set_texture(texture);
235-
sprite->set_position(Vector2(pos.x, -pos.y));
235+
sprite->set_position(pos);
236236
sprite->set_rotation_degrees(degree);
237237
sprite->set_scale(Vector2(scale.x, scale.y));
238238
sprite->set_name(path_str.get_file());
@@ -259,7 +259,7 @@ GdObj SpxSceneMgr::create_static_sprite(GdString texture_path, GdVec2 pos,GdFloa
259259
auto path_str = SpxStr(texture_path);
260260
// Create StaticBody2D
261261
SpxStaticSprite* static_body = memnew(SpxStaticSprite);
262-
static_body->set_position(Vector2(pos.x, -pos.y));
262+
static_body->set_position(pos);
263263
static_body->set_rotation_degrees(degree);
264264
static_body->set_name(path_str.get_file());
265265

@@ -268,15 +268,15 @@ GdObj SpxSceneMgr::create_static_sprite(GdString texture_path, GdVec2 pos,GdFloa
268268
Ref<Texture2D> texture = resMgr->load_texture(path_str, true);
269269
sprite->set_texture(texture);
270270
sprite->set_z_index(zindex);
271-
static_body->add_child(sprite);
272-
sprite->set_position(Vector2(pivot.x, -pivot.y));
271+
static_body->add_child(sprite);
272+
sprite->set_position(pivot);
273273

274274
// Create collision shape (default: rectangle matching texture size)
275275
CollisionShape2D* collision_shape = memnew(CollisionShape2D);
276276

277277
static_body->collider2d = collision_shape;
278278
static_body->add_child(collision_shape);
279-
collision_shape->set_position(Vector2(collider_pivot.x, -collider_pivot.y));
279+
collision_shape->set_position(collider_pivot);
280280
auto data_len = collider_params == nullptr ? 0 : collider_params->size;
281281
switch (type)
282282
{

modules/spx/spx_sprite_mgr.cpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,15 @@ void SpxSpriteMgr::set_child_position(GdObj obj, GdString path, GdVec2 pos) {
186186
check_and_get_sprite_v()
187187
auto child = (Node2D *)sprite->get_node(SpxStr(path));
188188
if (child != nullptr) {
189-
child->set_position(GdVec2{ pos.x, -pos.y });
189+
child->set_position(pos);
190190
}
191191
}
192192

193193
GdVec2 SpxSpriteMgr::get_child_position(GdObj obj, GdString path) {
194194
check_and_get_sprite_r(GdVec2())
195195
auto child = (Node2D *)sprite->get_node(SpxStr(path));
196196
if (child != nullptr) {
197-
auto pos = child->get_position();
198-
return GdVec2{ pos.x, -pos.y };
197+
return child->get_position();
199198
}
200199
return GdVec2();
201200
}
@@ -242,7 +241,6 @@ GdBool SpxSpriteMgr::check_collision(GdObj obj, GdObj target, GdBool is_src_trig
242241

243242
GdBool SpxSpriteMgr::check_collision_with_point(GdObj obj, GdVec2 point, GdBool is_trigger) {
244243
check_and_get_sprite_r(false)
245-
point.y = - point.y;
246244
return sprite->check_collision_with_point(point, is_trigger);
247245
}
248246

@@ -333,8 +331,7 @@ GdBool SpxSpriteMgr::is_sprite_alive(GdObj obj) {
333331

334332
void SpxSpriteMgr::set_position(GdObj obj, GdVec2 pos) {
335333
check_and_get_sprite_v()
336-
// flip y axis
337-
sprite->set_position(GdVec2(pos.x, -pos.y));
334+
sprite->set_position(pos);
338335
}
339336

340337
void SpxSpriteMgr::set_rotation(GdObj obj, GdFloat rot) {
@@ -349,9 +346,7 @@ void SpxSpriteMgr::set_scale(GdObj obj, GdVec2 scale) {
349346

350347
GdVec2 SpxSpriteMgr::get_position(GdObj obj) {
351348
check_and_get_sprite_r(GdVec2())
352-
auto pos = sprite->get_position();
353-
// flip y axis
354-
return GdVec2{ pos.x, -pos.y };
349+
return sprite->get_position();
355350
}
356351

357352
GdFloat SpxSpriteMgr::get_rotation(GdObj obj) {
@@ -584,15 +579,12 @@ GdString SpxSpriteMgr::get_current_anim_name(GdObj obj) {
584579

585580
void SpxSpriteMgr::set_velocity(GdObj obj, GdVec2 velocity) {
586581
check_and_get_sprite_v()
587-
// flip y axis
588-
sprite->set_velocity(GdVec2(velocity.x, -velocity.y));
582+
sprite->set_velocity(velocity);
589583
}
590584

591585
GdVec2 SpxSpriteMgr::get_velocity(GdObj obj) {
592586
check_and_get_sprite_r(GdVec2())
593-
auto val = sprite->get_velocity();
594-
// flip y axis
595-
return GdVec2{ val.x, -val.y };
587+
return sprite->get_velocity();
596588
}
597589

598590
GdBool SpxSpriteMgr::is_on_floor(GdObj obj) {
@@ -1080,11 +1072,9 @@ void SpxSpriteMgr::_check_pixel_collision_events() {
10801072

10811073
void SpxSpriteMgr::set_pivot(GdObj obj, GdVec2 pivot){
10821074
check_and_get_sprite_v()
1083-
pivot.y = - pivot.y;
10841075
sprite->set_pivot(pivot);
10851076
}
10861077
GdVec2 SpxSpriteMgr::get_pivot(GdObj obj){
10871078
check_and_get_sprite_r(GdVec2())
1088-
auto pivot= sprite->get_pivot();
1089-
return GdVec2(pivot.x,-pivot.y);
1079+
return sprite->get_pivot();
10901080
}

0 commit comments

Comments
 (0)