Skip to content

Conversation

@JiepengTan
Copy link

The goal is to migrate the coordinate transformation from C++ to Go and unify the implementation approach (Invert Y)

1. spx_sprite_mgr.cpp: 9 conversions

  • Position, velocity, and anchor point set/get methods
  • Collision detection points

2. spx_physic_mgr.cpp: 6 conversions

  • Raycast input/output
  • Shape queries

3. spx_scene_mgr.cpp: 5 conversions

  • Position/pivot setup when rendering sprites and creating static sprites

4. spx_draw_tiles.cpp/h: 8 conversions

  • All tilemap coordinate operations go through the flip_y() function

5. spx_debug_mgr.cpp: 4 conversions

  • Debug drawing functions (circles, rectangles, lines)

6. spx_input_mgr.cpp: 1 conversion

  • Mouse position retrieval

7. spx_path_finder.cpp: 2 conversions

  • Pathfinding input/output

Conversion Patterns

  • Input parameter conversion: pos.y = -pos.y or GdVec2(pos.x, -pos.y)
  • Return value conversion: return GdVec2(val.x, -val.y)
  • Helper function: flip_y() (used in spx_draw_tiles)

@JiepengTan JiepengTan force-pushed the pr_move_coord_convert_to_go_invery branch from 4c02cd6 to f5dd327 Compare October 24, 2025 08:09
@gemini-code-assist
Copy link

Summary of Changes

Hello @JiepengTan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on migrating coordinate transformation logic from C++ to Go, aiming for a unified implementation across the SPX project. The changes involve inverting the Y-axis in various managers to ensure consistency in coordinate handling. This refactoring impacts sprite management, physics, scene management, tile drawing, debugging, input handling, and pathfinding, leading to a more maintainable and coherent codebase.

Highlights

  • Coordinate Transformation Migration: The primary goal of this pull request is to migrate coordinate transformations from C++ to Go, unifying the implementation approach by inverting the Y-axis.
  • File Modifications: Several files were modified to implement the coordinate transformation, including spx_sprite_mgr.cpp, spx_physic_mgr.cpp, spx_scene_mgr.cpp, spx_draw_tiles.cpp/h, spx_debug_mgr.cpp, spx_input_mgr.cpp, and spx_path_finder.cpp.
  • Conversion Patterns: The conversion patterns include input parameter conversion (e.g., pos.y = -pos.y), return value conversion (e.g., return GdVec2(val.x, -val.y)), and the use of a helper function flip_y() in spx_draw_tiles.
  • Coordinate Inversion Removal: Coordinate inversion is removed from debug draw functions in SpxDebugMgr, tile placement and retrieval in SpxDrawTiles, mouse position retrieval in SpxInputMgr, and pathfinding input/output in SpxPathFinder.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively moves the Y-coordinate inversion logic from C++ to Go, simplifying the C++ side of the FFI. The changes are consistently applied across multiple manager classes, including sprite, physics, scene, and input management, by removing all instances of y-coordinate flipping. This aligns perfectly with the stated goal of unifying the coordinate transformation approach. Additionally, a new comprehensive guide for the AI assistant, CLAUDE.md, is introduced. My review includes a suggestion to improve the reusability of this new documentation file.

Copy link

@xgopilot xgopilot bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Summary

Clean refactoring that successfully removes Y-axis coordinate conversions from C++ layer. The changes are consistent and maintain code correctness. However, critical security and performance issues require attention before merging.

🔴 Critical Issues

1. Integer Overflow in Float-to-Int Conversion

  • Location: modules/spx/spx_utils.h:9-11 (used throughout collision/physics code)
  • Issue: spx_float_to_int() can overflow when coordinate values exceed ±922,337,203,685
  • Impact: Undefined behavior, incorrect calculations, potential exploits
  • CWE-190: Integer Overflow or Wraparound
  • Fix: Add bounds checking before multiplication

2. Missing Null Pointer Validation

  • Location: modules/spx/spx_camera_mgr.cpp:78-86
  • Issue: get_global_camera_rect() doesn't validate viewport pointer before dereferencing
  • Impact: Crash/DoS during scene transitions
  • CWE-476: NULL Pointer Dereference
  • Fix: Add ERR_FAIL_NULL_V(vp, Rect2()) check

⚠️ Performance Concerns

3. Redundant Coordinate Conversions in Nested Loops

  • Location: modules/spx/spx_sprite_mgr.cpp:920-921, 997-998
  • Issue: _to_image_coord() called twice per pixel in collision detection (O(n²) calls)
  • Impact: For 100×100 overlap = 20,000 function calls with allocations
  • Fix: Pre-compute transform parameters and use incremental arithmetic

4. Unnecessary Vector2 Allocations

  • Location: modules/spx/spx_sprite_mgr.cpp:870-872
  • Issue: 3 Vector2 allocations per _to_image_coord() call
  • Impact: 6N allocations for pixel collision checks
  • Fix: Reuse xpos and modify in-place

📝 Documentation Issues

5. Missing Architectural Documentation

  • Issue: No comments explaining that coordinate conversion moved from C++ to Go
  • Impact: Future maintainers won't understand the design change
  • Fix: Add comments to key functions like get_position(), set_velocity(), etc.

6. Undocumented FFI Boundary

  • Location: platform/web/js/engine/gdspx.util.js
  • Issue: No documentation on coordinate system conventions at Go/JS/C++ boundaries
  • Fix: Add JSDoc comments explaining Y-axis handling

✅ Positive Aspects

  • Consistent removal of Y-axis inversions across all modules
  • Clean separation of concerns (conversion logic now in Go layer)
  • No breaking changes to internal C++ logic
  • Good use of object pooling patterns

Recommendations

  1. Before Merge: Fix critical security issues (#1, #2)
  2. High Priority: Optimize collision detection performance (#3, #4)
  3. Should Have: Add architectural documentation (#5, #6)
  4. Consider: Add unit tests in Go layer to verify Y-axis inversion correctness

@JiepengTan
Copy link
Author

#187 需要在合并后 修改,里面涉及到了 y 的变动

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant