add get_part_box

This commit is contained in:
shenjack 2023-06-18 01:20:17 +08:00
parent 0673ba6981
commit f31a693eae
6 changed files with 42 additions and 101 deletions

View File

@ -121,6 +121,7 @@ if TYPE_CHECKING:
@property @property
def img_pos(self) -> Tuple[int, int, int, int]: ... def img_pos(self) -> Tuple[int, int, int, int]: ...
""" -x -y +x +y 左下右上 """ """ -x -y +x +y 左下右上 """
def get_part_box(self, part_id: int) -> Optional[Tuple[Tuple[int, int], Tuple[int, int]]]: ...
class Console_rs: class Console_rs:
def __init__(self) -> None: ... def __init__(self) -> None: ...

View File

@ -42,7 +42,6 @@ fn test_call(py_obj: &PyAny) -> PyResult<bool> {
fn module_init(_py: Python<'_>, m: &PyModule) -> PyResult<()> { fn module_init(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(get_version_str, m)?)?; m.add_function(wrap_pyfunction!(get_version_str, m)?)?;
m.add_function(wrap_pyfunction!(test_call, m)?)?; m.add_function(wrap_pyfunction!(test_call, m)?)?;
m.add_function(wrap_pyfunction!(simulator::simulation, m)?)?;
m.add_function(wrap_pyfunction!(sr1_data::part_list::read_part_list_py, m)?)?; m.add_function(wrap_pyfunction!(sr1_data::part_list::read_part_list_py, m)?)?;
m.add_function(wrap_pyfunction!(sr1_data::ship::py_raw_ship_from_file, m)?)?; m.add_function(wrap_pyfunction!(sr1_data::ship::py_raw_ship_from_file, m)?)?;
m.add_class::<render::camera::CameraRs>()?; m.add_class::<render::camera::CameraRs>()?;

View File

@ -12,6 +12,7 @@
/// 插件加载 /// 插件加载
/// ///
#[allow(unused)]
pub mod plugin_trait { pub mod plugin_trait {
pub struct ModInfo { pub struct ModInfo {
pub name: String, pub name: String,

View File

@ -12,6 +12,7 @@ pub mod data {
use pyo3::prelude::*; use pyo3::prelude::*;
use crate::sr1_data::part_list::RawPartList; use crate::sr1_data::part_list::RawPartList;
use crate::types::math::{Point2D, Rotatable};
use crate::types::sr1::{get_max_box, SR1PartData, SR1PartListTrait}; use crate::types::sr1::{get_max_box, SR1PartData, SR1PartListTrait};
use crate::types::sr1::{SR1PartList, SR1PartType, SR1Ship}; use crate::types::sr1::{SR1PartList, SR1PartType, SR1Ship};
@ -142,6 +143,25 @@ pub mod data {
} }
parts parts
} }
fn get_part_box(&self, part_id: i64) -> Option<((f64, f64), (f64, f64))> {
let part_data = self.ship.parts.iter().find(|&x| x.id == part_id);
if let Some(part_data) = part_data {
let part_type = self.part_list.get_part_type(part_data.part_type_id.clone()).unwrap();
// rotate
let radius = part_data.angle;
let ((x1, y1), (x2, y2)) = part_type.get_box();
let mut p1 = Point2D::new(x1, y1);
let mut p2 = Point2D::new(x2, y2);
p1.rotate_radius_mut(radius);
p2.rotate_radius_mut(radius);
// transform
p1.add_mut(part_data.x * 2.0, part_data.y * 2.0);
p2.add_mut(part_data.x * 2.0, part_data.y * 2.0);
return Some(((p1.x, p1.y), (p2.x, p2.y)));
}
None
}
} }
} }
@ -206,30 +226,6 @@ pub mod translate {
} }
} }
pub mod physics {
use pyo3::prelude::*;
use crate::simulator::interface::PhysicsSpace;
#[pyclass]
#[pyo3(name = "PhysicsSpace_rs")]
pub struct PyPhysicsSpace {
pub space: PhysicsSpace,
}
#[pymethods]
impl PyPhysicsSpace {
#[new]
fn new(gravity: (f64, f64)) -> Self {
Self {
space: PhysicsSpace::new(gravity),
}
}
fn tick_space(&mut self) { self.space.tick_space() }
}
}
pub mod console { pub mod console {
use pyo3::prelude::*; use pyo3::prelude::*;

View File

@ -9,6 +9,7 @@
use pyo3::prelude::*; use pyo3::prelude::*;
use rapier2d_f64::prelude::*; use rapier2d_f64::prelude::*;
#[allow(unused)]
#[pyfunction] #[pyfunction]
#[pyo3(name = "simulation")] #[pyo3(name = "simulation")]
pub fn simulation() -> () { pub fn simulation() -> () {
@ -63,75 +64,8 @@ pub fn simulation() -> () {
} }
} }
pub mod interface { #[allow(unused)]
pub mod python {
use pyo3::prelude::*;
use rapier2d_f64::prelude::*; use rapier2d_f64::prelude::*;
pub struct PhysicsSpace {
pub rigid_body_set: RigidBodySet,
pub collider_set: ColliderSet,
pub gravity: Vector<f64>,
pub integration_parameters: IntegrationParameters,
pub physics_pipeline: PhysicsPipeline,
pub island_manager: IslandManager,
pub broad_phase: BroadPhase,
pub narrow_phase: NarrowPhase,
pub impulse_joint_set: ImpulseJointSet,
pub multibody_joint_set: MultibodyJointSet,
pub ccd_solver: CCDSolver,
pub physics_hooks: (),
pub event_handler: (),
}
impl PhysicsSpace {
pub fn new(gravity: (f64, f64)) -> Self {
let rigid_body_set = RigidBodySet::new();
let collider_set = ColliderSet::new();
let gravity = vector![gravity.0, gravity.1];
let integration_parameters = IntegrationParameters::default();
let physics_pipeline = PhysicsPipeline::new();
let island_manager = IslandManager::new();
let broad_phase = BroadPhase::new();
let narrow_phase = NarrowPhase::new();
let impulse_joint_set = ImpulseJointSet::new();
let multibody_joint_set = MultibodyJointSet::new();
let ccd_solver = CCDSolver::new();
let physics_hooks = ();
let event_handler = ();
Self {
rigid_body_set,
collider_set,
gravity,
integration_parameters,
physics_pipeline,
island_manager,
broad_phase,
narrow_phase,
impulse_joint_set,
multibody_joint_set,
ccd_solver,
physics_hooks,
event_handler,
}
}
pub fn tick_space(&mut self) {
self.physics_pipeline.step(
&self.gravity,
&self.integration_parameters,
&mut self.island_manager,
&mut self.broad_phase,
&mut self.narrow_phase,
&mut self.rigid_body_set,
&mut self.collider_set,
&mut self.impulse_joint_set,
&mut self.multibody_joint_set,
&mut self.ccd_solver,
None,
&self.physics_hooks,
&self.event_handler,
);
}
}
} }

View File

@ -197,9 +197,13 @@ pub mod sr1 {
} }
impl SR1PartType { impl SR1PartType {
pub fn get_box(&self) -> (f64, f64, f64, f64) { pub fn get_box(&self) -> ((f64, f64), (f64, f64)) {
let x = self.width as f64 / 2.0; // -x, -y, x, y
(-x, 0.0, x, self.height as f64) // 居中
(
(-(self.width as f64 / 2.0), -(self.height as f64 / 2.0)),
(self.width as f64 / 2.0, self.height as f64 / 2.0),
)
} }
} }
@ -777,14 +781,14 @@ pub mod sr1 {
let mut max_box = (0_f64, 0_f64, 0_f64, 0_f64); let mut max_box = (0_f64, 0_f64, 0_f64, 0_f64);
for part in parts.iter() { for part in parts.iter() {
let part_type = part_list.get_part_type(part.part_type_id.clone()).unwrap(); let part_type = part_list.get_part_type(part.part_type_id.clone()).unwrap();
let (x1, y1, x2, y2) = part_type.get_box(); let ((x1, y1), (x2, y2)) = part_type.get_box();
// rotate // rotate
let mut p1 = Point2D::new(x1, y1); let mut p1 = Point2D::new(x1, y1);
let mut p2 = Point2D::new(x2, y2); let mut p2 = Point2D::new(x2, y2);
p1.rotate_radius_mut(part.angle); p1.rotate_radius_mut(part.angle);
p2.rotate_radius_mut(part.angle); p2.rotate_radius_mut(part.angle);
let p1 = p1.add(part.x, part.y); let p1 = p1.add(part.x * 2.0, part.y * 2.0);
let p2 = p2.add(part.x, part.y); let p2 = p2.add(part.x * 2.0, part.y * 2.0);
let (x1, y1, x2, y2) = (p1.x, p1.y, p2.x, p2.y); let (x1, y1, x2, y2) = (p1.x, p1.y, p2.x, p2.y);
// get max box // get max box
max_box.0 = max_box.0.min(x1).min(part.x); max_box.0 = max_box.0.min(x1).min(part.x);
@ -835,6 +839,12 @@ pub mod math {
#[inline] #[inline]
pub fn add(&self, x: f64, y: f64) -> Self { Point2D::new(self.x + x, self.y + y) } pub fn add(&self, x: f64, y: f64) -> Self { Point2D::new(self.x + x, self.y + y) }
#[inline]
pub fn add_mut(&mut self, x: f64, y: f64) -> () {
self.x += x;
self.y += y;
}
} }
impl Rotatable for Point2D { impl Rotatable for Point2D {