ry-physics
Physics module for Ry-Dit — Projectile motion, Gravity, N-body simulation
Overview
ry-physics is a physics simulation library for the Ry-Dit game engine. It provides projectile motion calculations, gravitational N-body simulations, and more — all accessible via a clean JSON-based API.
Installation
[]
= "0.7.34"
= "0.8.2"
= "1.0"
Quick Start
use PhysicsModule;
use RyditModule;
use json;
let module = PhysicsModule;
// Projectile motion: x0, y0, velocity (m/s), angle (degrees)
let result = module.execute?;
// Returns: [x_final, y_final, flight_time, max_height, range]
// N-body gravity: 2 bodies
let force = module.execute?;
// Returns: [fx1, fy1, fx2, fy2, distance]
// N-body simulation: multiple bodies over time
let bodies = json!;
let result = module.execute?;
// Returns: [[x, y, vx, vy], ...] updated positions
API Reference
Projectile Motion
module.execute
| Parameter | Type | Description |
|---|---|---|
x0 |
f64 | Initial X position |
y0 |
f64 | Initial Y position |
velocity |
f64 | Initial velocity (m/s) |
angle_degrees |
f64 | Launch angle in degrees |
Returns: [x_final, y_final, flight_time, max_height, range]
Formulas:
- Flight time:
2 * vy / g - Max height:
vy² / (2 * g) - Range:
vx * flight_time
N-Body (2 Bodies)
module.execute
| Parameter | Type | Description |
|---|---|---|
m1, m2 |
f64 | Masses of the two bodies |
x1, y1 |
f64 | Position of body 1 |
x2, y2 |
f64 | Position of body 2 |
G |
f64 | Gravitational constant (default: 6.674e-11) |
Returns: [fx1, fy1, fx2, fy2, distance]
Formula: F = G * m1 * m2 / r²
N-Body Simulation (Multiple Bodies)
module.execute
| Parameter | Type | Description |
|---|---|---|
bodies |
array | [[mass, x, y, vx, vy, is_static], ...] |
dt |
f64 | Delta time (default: 0.016 = ~60 FPS) |
G |
f64 | Gravitational constant (default: 6.674e-11) |
Returns: [[x, y, vx, vy], ...] — updated positions and velocities
Algorithm: O(n²) pairwise gravitational computation with Euler integration.
Examples
Ball Trajectory
// Kick a ball at 20 m/s, 30 degrees from ground level
let result = PhysicsModule.execute;
let data = result.unwrap.as_array.unwrap;
let flight_time = data.as_f64.unwrap;
let max_height = data.as_f64.unwrap;
let range = data.as_f64.unwrap;
println!;
Orbital Simulation
// Sun + Earth + Moon simplified
let bodies = json!;
for step in 0..1000
LAZOS Protocol
# Projectile
|
# N-body gravity
|
# N-body simulation
|
Performance
- O(n²) N-body: Suitable for up to ~500 bodies in real-time
- Zero allocations in projectile calculation (pure math)
- 6 unit tests ensuring correctness across all functions
Dependencies
| Crate | Version | Purpose |
|---|---|---|
ry-core |
0.8.2 | Module trait system |
serde_json |
1.0 | JSON serialization |
serde |
1.0 | Derive macros |
Roadmap
- RK4 integration (more accurate than Euler)
- Collision detection between bodies
- Soft-body physics
- Fluid dynamics (simplified)
- Electromagnetism simulation
Contributing
Contributions are welcome! This crate is part of the Ry-Dit game engine project.
- Repository: https://github.com/lapumlbb18-blip/Ry-dit
- Issues: https://github.com/lapumlbb18-blip/Ry-dit/issues
- Pull Requests: Welcome!
Please read CONTRIBUTING.md for guidelines.
License
MIT License - See LICENSE for details.
ry-physics — Physics simulations for Ry-Dit game engine 🚀🌍
6 tests · 296 lines · Projectile + N-body gravity