-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Improve collision system with support for multiple shapes
Overview
Currently, the physics engine only supports circular objects for collision detection. This enhancement will add support for multiple shapes including rectangles, polygons, and potentially other geometric forms, along with improved collision algorithms.
Current State Analysis
From the existing code in src/lib.rs, the collision system:
- Only handles circle-to-circle collisions
- Uses basic distance-based collision detection
- Has collision response with restitution coefficients
- Supports frozen objects
Proposed Improvements
1. Shape System Architecture
#[derive(Clone, Serialize, Deserialize)]
pub enum Shape {
Circle { radius: f64 },
Rectangle { width: f64, height: f64 },
Polygon { vertices: Vec<(f64, f64)> },
// Future: Ellipse, Capsule, etc.
}2. Enhanced Object Structure
- Modify the
Objectstruct to include aShapefield instead of justradius - Add rotation angle for non-circular shapes
- Include moment of inertia calculations for proper rotational physics
3. Collision Detection Algorithms
Circle-Circle (Current - Improve)
- Optimize the existing implementation
- Better handling of edge cases
Circle-Rectangle
- Implement closest point on rectangle to circle center
- Handle corner cases properly
Rectangle-Rectangle
- Separating Axis Theorem (SAT) implementation
- AABB (Axis-Aligned Bounding Box) optimization for axis-aligned rectangles
Circle-Polygon & Polygon-Polygon
- SAT for convex polygons
- GJK algorithm for more complex cases
4. Collision Response Improvements
- Rotational dynamics: Objects should rotate upon collision
- Contact point calculation: More accurate collision response based on contact points
- Friction simulation: Add surface friction between objects
- Multiple contact points: Handle scenarios where objects have multiple contact points
5. Performance Optimizations
- Broad phase collision detection: Spatial partitioning (quadtree/grid)
- Bounding box pre-checks: Quick elimination of non-colliding pairs
- Shape-specific optimizations: Fast paths for common cases
Technical Implementation Plan
Phase 1: Core Shape System
- Define Shape enum and update Object structure
- Implement basic rendering for different shapes in WorldView
- Add rotation support to objects
Phase 2: Collision Detection
- Implement circle-rectangle collision detection
- Add rectangle-rectangle collision (AABB first, then oriented)
- Create polygon collision detection using SAT
Phase 3: Enhanced Collision Response
- Add rotational physics (angular velocity, moment of inertia)
- Implement contact point calculation
- Add friction coefficients and simulation
Phase 4: Performance & Polish
- Implement broad-phase collision detection
- Add spatial partitioning for better performance
- Optimize hot paths in collision code
API Changes
Breaking Changes
add_objectmethod will need to accept aShapeparameter instead ofradius- Serialization format will change (consider migration strategy)
New Methods
// Shape creation helpers
pub fn add_circle_object(&mut self, pos_x: f64, pos_y: f64, radius: f64, ...);
pub fn add_rectangle_object(&mut self, pos_x: f64, pos_y: f64, width: f64, height: f64, ...);
pub fn add_polygon_object(&mut self, pos_x: f64, pos_y: f64, vertices: Vec<(f64, f64)>, ...);
// Rotation methods
pub fn set_object_rotation(&mut self, index: usize, angle: f64);
pub fn set_object_angular_velocity(&mut self, index: usize, angular_vel: f64);Testing Strategy
- Unit tests for each collision detection algorithm
- Integration tests with various shape combinations
- Performance benchmarks comparing old vs new system
- Visual tests to ensure collision responses look realistic
Backward Compatibility
- Provide migration path for existing circular objects
- Maintain current API with deprecation warnings
- Ensure existing web interface continues to work
Additional Considerations
- Impact on WASM bundle size
- Performance implications for browser rendering
- Memory usage with more complex shape data
- Integration with the planned ratatui interface
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request