Skip to content

Commit 50715a0

Browse files
committed
Move coord convert from c++ => go (invert y)
1 parent 0fa7a3a commit 50715a0

File tree

8 files changed

+30
-63
lines changed

8 files changed

+30
-63
lines changed

modules/spx/spx_debug_mgr.cpp

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

107-
pos.y = -pos.y;
108107
Line2D* circle = memnew(Line2D);
109108
circle->set_default_color(color);
110109
circle->set_width(2.0f);
@@ -137,8 +136,7 @@ void SpxDebugMgr::debug_draw_rect(GdVec2 pos, GdVec2 size, GdColor color) {
137136
Line2D* rect = memnew(Line2D);
138137
rect->set_default_color(color);
139138
rect->set_width(2.0f);
140-
141-
pos.y = -pos.y;
139+
142140
size = size * 0.5;
143141
PackedVector2Array points;
144142
points.append(Vector2(-size.x, -size.y));
@@ -165,10 +163,6 @@ void SpxDebugMgr::debug_draw_line(GdVec2 from, GdVec2 to, GdColor color) {
165163
return;
166164
}
167165

168-
// 翻转Y轴坐标
169-
from.y = -from.y;
170-
to.y = -to.y;
171-
172166
Line2D* line = memnew(Line2D);
173167
line->set_default_color(color);
174168
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
@@ -50,8 +50,7 @@ void SpxInputMgr::on_reset(int reset_code) {
5050

5151
// input
5252
GdVec2 SpxInputMgr::get_global_mouse_pos() {
53-
auto mouse_pos = cameraMgr->get_global_mouse_position();
54-
return GdVec2(mouse_pos.x, -mouse_pos.y);
53+
return cameraMgr->get_global_mouse_position();
5554
}
5655

5756
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
@@ -108,10 +108,6 @@ SpxRaycastInfo SpxPhysicMgr::_raycast(GdVec2 from, GdVec2 to,GdArray ignore_spri
108108
info.normal = GdVec2{0, 0};
109109
info.sprite_gid = 0;
110110

111-
// invert y
112-
GdVec2 current_from = GdVec2{from.x, -from.y};
113-
GdVec2 target_to = GdVec2{to.x, -to.y};
114-
115111
HashSet<RID> ignore_set;
116112
if(ignore_sprites && ignore_sprites->size > 0){
117113
GdObj* sprite_data = (SpxBaseMgr::get_array<GdObj>(ignore_sprites, 0));
@@ -129,8 +125,8 @@ SpxRaycastInfo SpxPhysicMgr::_raycast(GdVec2 from, GdVec2 to,GdArray ignore_spri
129125
}
130126
PhysicsDirectSpaceState2D::RayResult result;
131127
PhysicsDirectSpaceState2D::RayParameters params;
132-
params.from = current_from;
133-
params.to = target_to;
128+
params.from = from;
129+
params.to = to;
134130
params.collision_mask = (uint32_t)collision_mask;
135131
params.collide_with_areas = collide_with_areas;
136132
params.collide_with_bodies = collide_with_bodies;
@@ -149,8 +145,8 @@ SpxRaycastInfo SpxPhysicMgr::_raycast(GdVec2 from, GdVec2 to,GdArray ignore_spri
149145
SpxSprite *collider = dynamic_cast<SpxSprite *>(result.collider);
150146
GdObj current_gid = collider ? collider->get_gid() : 0;
151147
info.collide = true;
152-
info.position = GdVec2{result.position.x, -result.position.y};
153-
info.normal = GdVec2{result.normal.x, -result.normal.y};
148+
info.position = result.position;
149+
info.normal = result.normal;
154150
info.sprite_gid = current_gid;
155151
return info;
156152
}
@@ -168,9 +164,6 @@ GdObj SpxPhysicMgr::raycast(GdVec2 from, GdVec2 to, GdInt collision_mask) {
168164

169165
PhysicsDirectSpaceState2D::RayResult result;
170166
PhysicsDirectSpaceState2D::RayParameters params;
171-
// flip y axis
172-
from = GdVec2{ from.x, -from.y };
173-
to = GdVec2{ to.x, -to.y };
174167
params.from = from;
175168
params.to = to;
176169
params.collision_mask = (uint32_t)collision_mask;
@@ -189,10 +182,6 @@ GdBool SpxPhysicMgr::check_collision(GdVec2 from, GdVec2 to, GdInt collision_mas
189182
PhysicsDirectSpaceState2D *space_state = node->get_world_2d()->get_direct_space_state();
190183
PhysicsDirectSpaceState2D::RayResult result;
191184
PhysicsDirectSpaceState2D::RayParameters params;
192-
193-
// flip y axis
194-
from = GdVec2{ from.x, -from.y };
195-
to = GdVec2{ to.x, -to.y };
196185
params.from = from;
197186
params.to = to;
198187
params.collision_mask = (uint32_t)collision_mask;
@@ -300,8 +289,7 @@ GdArray SpxPhysicMgr::_check_collision(RID shape, GdVec2 pos, GdInt collision_ma
300289
return create_array(GD_ARRAY_TYPE_GDOBJ, 0);
301290
}
302291

303-
GdVec2 flipped_pos = GdVec2{ pos.x, -pos.y };
304-
Transform2D query_transform(0, flipped_pos);
292+
Transform2D query_transform(0, pos);
305293

306294
PhysicsDirectSpaceState2D::ShapeParameters params;
307295
params.shape_rid = shape;

modules/spx/spx_scene_mgr.cpp

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

233233
SpxRenderSprite* sprite = memnew(SpxRenderSprite);
234-
sprite->set_pivot(GdVec2(pivot.x, -pivot.y));
234+
sprite->set_pivot(pivot);
235235
auto path_str = SpxStr(texture_path);
236236
Ref<Texture2D> texture = resMgr->load_texture(path_str, true);
237237
sprite->set_texture(texture);
238-
sprite->set_position(Vector2(pos.x, -pos.y));
238+
sprite->set_position(pos);
239239
sprite->set_rotation_degrees(degree);
240240
sprite->set_scale(Vector2(scale.x, scale.y));
241241
sprite->set_name(path_str.get_file());
@@ -262,7 +262,7 @@ GdObj SpxSceneMgr::create_static_sprite(GdString texture_path, GdVec2 pos,GdFloa
262262
auto path_str = SpxStr(texture_path);
263263
// Create StaticBody2D
264264
SpxStaticSprite* static_body = memnew(SpxStaticSprite);
265-
static_body->set_position(Vector2(pos.x, -pos.y));
265+
static_body->set_position(pos);
266266
static_body->set_rotation_degrees(degree);
267267
static_body->set_name(path_str.get_file());
268268

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

277277
// Create collision shape (default: rectangle matching texture size)
278278
CollisionShape2D* collision_shape = memnew(CollisionShape2D);
279279

280280
static_body->collider2d = collision_shape;
281281
static_body->add_child(collision_shape);
282-
collision_shape->set_position(Vector2(collider_pivot.x, -collider_pivot.y));
282+
collision_shape->set_position(collider_pivot);
283283
auto data_len = collider_params == nullptr ? 0 : collider_params->size;
284284
switch (type)
285285
{

modules/spx/spx_sprite_mgr.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,15 @@ void SpxSpriteMgr::set_child_position(GdObj obj, GdString path, GdVec2 pos) {
196196
check_and_get_sprite_v()
197197
auto child = (Node2D *)sprite->get_node(SpxStr(path));
198198
if (child != nullptr) {
199-
child->set_position(GdVec2{ pos.x, -pos.y });
199+
child->set_position(pos);
200200
}
201201
}
202202

203203
GdVec2 SpxSpriteMgr::get_child_position(GdObj obj, GdString path) {
204204
check_and_get_sprite_r(GdVec2())
205205
auto child = (Node2D *)sprite->get_node(SpxStr(path));
206206
if (child != nullptr) {
207-
auto pos = child->get_position();
208-
return GdVec2{ pos.x, -pos.y };
207+
return child->get_position();
209208
}
210209
return GdVec2();
211210
}
@@ -252,7 +251,6 @@ GdBool SpxSpriteMgr::check_collision(GdObj obj, GdObj target, GdBool is_src_trig
252251

253252
GdBool SpxSpriteMgr::check_collision_with_point(GdObj obj, GdVec2 point, GdBool is_trigger) {
254253
check_and_get_sprite_r(false)
255-
point.y = - point.y;
256254
return sprite->check_collision_with_point(point, is_trigger);
257255
}
258256

@@ -270,7 +268,7 @@ GdInt SpxSpriteMgr::_create_sprite(GdString path, GdVec2 pos, GdBool is_backdrop
270268
SpxSprite *sprite = nullptr;
271269
if (path_str == "") {
272270
sprite = memnew(SpxSprite);
273-
sprite->set_position(GdVec2(pos.x, -pos.y));
271+
sprite->set_position(pos);
274272
AnimatedSprite2D *animated_sprite = memnew(AnimatedSprite2D);
275273
sprite->add_child(animated_sprite);
276274
Area2D *area = memnew(Area2D);
@@ -345,8 +343,7 @@ GdBool SpxSpriteMgr::is_sprite_alive(GdObj obj) {
345343

346344
void SpxSpriteMgr::set_position(GdObj obj, GdVec2 pos) {
347345
check_and_get_sprite_v()
348-
// flip y axis
349-
sprite->set_position(GdVec2(pos.x, -pos.y));
346+
sprite->set_position(pos);
350347
}
351348

352349
void SpxSpriteMgr::set_rotation(GdObj obj, GdFloat rot) {
@@ -361,9 +358,7 @@ void SpxSpriteMgr::set_scale(GdObj obj, GdVec2 scale) {
361358

362359
GdVec2 SpxSpriteMgr::get_position(GdObj obj) {
363360
check_and_get_sprite_r(GdVec2())
364-
auto pos = sprite->get_position();
365-
// flip y axis
366-
return GdVec2{ pos.x, -pos.y };
361+
return sprite->get_position();
367362
}
368363

369364
GdFloat SpxSpriteMgr::get_rotation(GdObj obj) {
@@ -596,15 +591,12 @@ GdString SpxSpriteMgr::get_current_anim_name(GdObj obj) {
596591

597592
void SpxSpriteMgr::set_velocity(GdObj obj, GdVec2 velocity) {
598593
check_and_get_sprite_v()
599-
// flip y axis
600-
sprite->set_velocity(GdVec2(velocity.x, -velocity.y));
594+
sprite->set_velocity(velocity);
601595
}
602596

603597
GdVec2 SpxSpriteMgr::get_velocity(GdObj obj) {
604598
check_and_get_sprite_r(GdVec2())
605-
auto val = sprite->get_velocity();
606-
// flip y axis
607-
return GdVec2{ val.x, -val.y };
599+
return sprite->get_velocity();
608600
}
609601

610602
GdBool SpxSpriteMgr::is_on_floor(GdObj obj) {
@@ -1092,11 +1084,9 @@ void SpxSpriteMgr::_check_pixel_collision_events() {
10921084

10931085
void SpxSpriteMgr::set_pivot(GdObj obj, GdVec2 pivot){
10941086
check_and_get_sprite_v()
1095-
pivot.y = - pivot.y;
10961087
sprite->set_pivot(pivot);
10971088
}
10981089
GdVec2 SpxSpriteMgr::get_pivot(GdObj obj){
10991090
check_and_get_sprite_r(GdVec2())
1100-
auto pivot= sprite->get_pivot();
1101-
return GdVec2(pivot.x,-pivot.y);
1091+
return sprite->get_pivot();
11021092
}

0 commit comments

Comments
 (0)