Skip to content

添加常用向量结构和一些特定形状结构 #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 68 commits into
base: master
Choose a base branch
from

Conversation

BAKAOLC
Copy link
Collaborator

@BAKAOLC BAKAOLC commented Apr 28, 2025

This pull request introduces new implementations for 2D, 3D, and 4D vector classes in Lua using the ffi library. These classes provide various mathematical operations, utility methods, and compatibility with LuaSTG Evo. Below is a summary of the most important changes:

2D Vector (Vector2)

  • Added a Vector2 class with fields x and y, operator overloads for arithmetic operations (+, -, *, /), unary negation, equality, and string representation.
  • Implemented utility methods such as length, angle, dot, normalize, and normalized for vector operations.
  • Introduced LuaSTG Evo compatibility methods and exposed Vector2 creation via lstg.Vector2.

3D Vector (Vector3)

  • Added a Vector3 class with fields x, y, and z, along with operator overloads for arithmetic operations, unary negation, equality, and string representation.
  • Implemented utility methods such as length, dot, normalize, and normalized for 3D vector operations.

4D Vector (Vector4)

  • Added a Vector4 class with fields x, y, z, and w, along with operator overloads for arithmetic operations, unary negation, equality, and string representation.
  • Implemented utility methods such as length, dot, normalize, and normalized for 4D vector operations.

These changes enhance the mathematical foundation of the project by introducing efficient, reusable vector classes with extensive functionality.

@BAKAOLC BAKAOLC requested a review from Copilot April 28, 2025 15:09
Copilot

This comment was marked as outdated.

@BAKAOLC BAKAOLC changed the title 添加 ffi 版本的 Vector2/Vector3/Vector4 添加常用向量结构和一些特定形状结构 Apr 29, 2025
@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented Apr 29, 2025

Geometric Shape: Triangle

  • Triangle Class:
    • Introduced a Triangle class supporting operations like area, centroid, circumradius, inradius, and geometric transformations (rotation, scaling). (game/packages/thlib-scripts-v2/foundation/shape/Triangle.lua)
    • Added methods for point containment checks and string representation for debugging. (game/packages/thlib-scripts-v2/foundation/shape/Triangle.lua)

These additions establish a solid mathematical foundation for further development in game physics, geometry, and spatial computations.

@BAKAOLC BAKAOLC closed this Apr 29, 2025
@BAKAOLC BAKAOLC reopened this Apr 29, 2025
@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented Apr 29, 2025

Line Implementation:

  • 2D Line (Line):
    • Introduced a Line class for 2D geometric operations, including creation from points, length calculation, midpoint, angle, translation, rotation, intersection detection, and point projection. (game/packages/thlib-scripts-v2/foundation/shape/Line.lua)

@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented Apr 29, 2025

Summary of Changes:

  1. File Updates:

    • Revisions to existing files within the foundation/math and foundation/shape directories, such as:
      • Vector2.lua
      • Vector3.lua
      • Vector4.lua
      • Segment.lua
      • Triangle.lua
  2. New Files Added:

    • New shape-related modules have been introduced:
      • Circle.lua
      • Line.lua
      • Ray.lua
  3. Structural Renaming:

    • Structs like Vector2, Vector3, and Vector4 have been renamed for consistency, e.g., to foundation_math_Vector2, foundation_math_Vector3, and foundation_math_Vector4.
  4. Enhanced Features & Methods:

    • Zero Vector Methods: Added zero() methods for initializing zero vectors in Vector2, Vector3, and Vector4.
    • Clone Method: Introduced clone() methods for creating copies of vectors and shapes.
    • Intersection Checks: New methods for intersection handling among shapes (e.g., lines, circles, triangles).
    • Scaling and Movement: Added scaling and moving functionality for shapes like segments, triangles, and circles.
  5. Code Refactoring:

    • Reorganized and standardized method names and variable references for clarity and consistency.
    • Updated all references to use new struct names and standardized methods.
  6. New Functionalities for Shapes:

    • Added robust intersection checks for shapes like Segment, Triangle, Circle, and Ray.
    • Enhanced mathematical operations for geometry handling (e.g., centroids, projections, normals).

@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented Apr 29, 2025

  1. New Intersection Methods:

    • Added methods to check for intersections between various shapes (Circle, Line, Ray, Segment, Triangle) to identify if they intersect with other geometric shapes.
    • These methods include both detailed intersection logic (returning points) and simplified checks (returning boolean).
  2. Enhanced Shape Capabilities:

    • Implemented utility methods for operations like calculating the closest point, projecting points, distance calculations, and containment checks for shapes.
  3. Key Updates:

    • Circle: Methods added for checking intersections and calculating the closest point or distance to other shapes.
    • Line: Added methods for simplified intersection logic and operations like projecting points and checking containment.
    • Ray: Introduced similar intersection and utility methods as Line, alongside distance and projection calculations.
    • Segment: Enhanced intersection checks and utility functions for containment and distance.
    • Triangle: Expanded functionality with methods for intersections, closest points, projections, and distance calculations.

@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented Apr 29, 2025

  1. Intersection Methods for Rectangle:

    • New intersection methods were added to the Circle, Line, Ray, Segment, and Triangle classes to handle interactions with Rectangle objects.
    • Both detailed intersection logic (returning intersection points) and simplified checks (returning boolean values) were implemented.
  2. Vector Comparison Improvements:

    • Updated the equality (__eq) methods in Vector2, Vector3, and Vector4 classes to use a tolerance threshold (1e-10) for floating-point comparison, improving numerical stability.
  3. Enhanced Geometric Classes:

    • Line: Added methods to create lines using radians (createFromPointAndRad) and improved initialization by normalizing direction vectors.
    • Ray: Added similar methods as Line for initialization using radians.
    • Segment: Renamed angle-based creation methods (createFromPointAndAngle to createFromAngle) and added support for intersection checks with rectangles.
    • Triangle: Added methods for rectangle interactions and refactored existing intersection logic.

@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented Apr 29, 2025

  1. Addition of the Rectangle Class:

    • A new Rectangle class was introduced, providing comprehensive support for geometric operations and interactions.
  2. Key Features of the Rectangle Class:

    • Creation Methods:
      • Methods for creating rectangles using center, width, height, and direction.
      • Support for creation with radians or angles for rotation.
    • Utility Methods:
      • Retrieve vertices (getVertices) and edges (getEdges) of the rectangle.
      • Methods for translation (move, moved), rotation (rotate, rotated), and scaling (scale, scaled).
    • Intersection Checks:
      • Methods for determining intersections with other shapes, such as Segment, Triangle, Line, Ray, Circle, and other Rectangle objects.
      • Both detailed intersection logic (returning points) and simplified checks (returning boolean values) are included.
    • Geometric Calculations:
      • Calculate the closest point and distance to a given point.
      • Project a point onto the rectangle.
    • Containment Checks:
      • Determine if points are within the rectangle or on its boundary.

@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented May 1, 2025

  1. Enhancements in Geometric Shapes:

    • Circle, Polygon, Rectangle, Sector, and Triangle:
      • Introduced an additional parameter boundary to methods (closestPoint, projectPoint) to restrict calculations to the shape's boundary.
      • Added overloads for these methods to maintain backward compatibility.
  2. Code Improvements:

    • Refactored logic for calculating the closest point to improve flexibility and precision.
    • Updated projection methods to consistently use the new boundary-restricted logic.
  3. Overall Improvements:

    • Enhanced the mathematical precision of geometric operations.
    • Improved code clarity and added documentation.

@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented May 1, 2025

Because it's a pain in the ass to re-record and modify gifs, please run your own tests to get the latest performance.
Current fix: If the detection point is inside the graph, the projection point is now calculated to be confined to the graph boundary instead of coinciding with the closest point.

@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented May 1, 2025

Something like this.
image
image
image

BAKAOLC added 2 commits May 1, 2025 17:10
…Polygon, Ray, Rectangle, Sector, Segment, and Triangle classes
@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented May 1, 2025

439435E61CF28847B4553472A8642CDB

@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented May 1, 2025

50e1cbf7709b03d1a122cd368c13a299
228abb1d90bf83e872278865329e4422

  1. New Files Added:

    • BezierCurve.lua:
      • Introduced a comprehensive implementation of Bézier curves, including methods for creating, manipulating, and performing geometric computations (e.g., splitting, rotations, and intersections).
    • Ellipse.lua:
      • Implemented functionality for handling ellipses, supporting operations like scaling, rotations, and intersection checks.
    • Modules in ShapeIntersector/:
      • Added specialized intersection logic for various geometric shapes (e.g., Bézier curves, circles, ellipses).
  2. Enhancements and Refactorings:

    • Improved modularity by splitting logic into separate files (e.g., ShapeIntersector components).
    • Added clone methods across multiple shape classes for deep copying instances.
    • Enhanced __index and __newindex metamethods in shape classes to better handle data encapsulation and property access.
  3. Fixes and Optimizations:

    • Rectified issues in geometric operations, ensuring accurate computations.
    • Refactored redundant calculations into reusable methods for maintainability.

@BAKAOLC BAKAOLC marked this pull request as draft May 1, 2025 20:26
@BAKAOLC
Copy link
Collaborator Author

BAKAOLC commented May 2, 2025

  1. Addition of New Lua Modules:

    • Matrix.lua: A foundational math module for matrix operations.
    • MatrixTransformation.lua: Provides matrix transformation utilities.
    • SpecialMatrix.lua: Includes functionality for special types of matrices (e.g., diagonal, tridiagonal).
    • BooleanArray.lua, DoubleArray.lua, IntArray.lua: Modules for handling arrays with different types like boolean, double, and integer.
  2. Enhancements to Existing Modules:

    • BezierCurve.lua:
      • Introduced a new method getEqualArcLengthPoints to divide a Bézier curve evenly by arc length.
      • Enhanced containsPoint to accept an optional number of segments for approximation.
    • Ellipse.lua:
      • Added functionality to calculate equal arc-length points and segments.
      • Refined containsPoint logic for better accuracy.
  3. Bug Fixes and Refactoring:

    • Fixed and streamlined logic in Ellipse:getEdges and Ellipse:closestPoint.
    • Addressed issues in ShapeIntersector/EllipseIntersector.lua related to ellipse intersection calculations.
  4. Laboratory Module Changes:

    • Updated laboratory/geometry/main.lua to use the enhanced functionality of Bézier curve detection with the added segment parameter.

@Demonese Demonese added the enhancement New feature or request label May 7, 2025
@Legacy-LuaSTG-Engine Legacy-LuaSTG-Engine locked and limited conversation to collaborators May 8, 2025
@Legacy-LuaSTG-Engine Legacy-LuaSTG-Engine unlocked this conversation May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants