Skip to content

Commit

Permalink
was going to use builtin shapes, but there are lots of problems with …
Browse files Browse the repository at this point in the history
…exporting resources. see: godot-rust/gdext#440
  • Loading branch information
lokimckay committed May 7, 2024
1 parent 75fb7a9 commit c05e4ec
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 26 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Godot Rapier 3D

## Roadmap

- [] Editor gizmos for collider shapes
- [] Serialize/deserialize physics state to/from variable
- [] Save/load physics state to/from resource file

## Contributing

See [CONTRIBUTING.md]()
Expand All @@ -10,12 +16,11 @@ Inspired by [ilyas-taouaou/rapier-gdext](https://github.com/ilyas-taouaou/rapier

## TODO

1. Visualise colliders
1. collision shapes as resource
1. Simple example
1. On init, register all found rigid bodies and colliders
1. Save/load state
1. Switch to editor singleton for guaranteed access to scene tree?
1. Maybe wait 3 frames before step starts working to give colliders a chance to connect? (call deferred)
1. refactor engine singleton function to include pipeline retrieval
1. Maybe wait 3 frames before step starts working to give colliders a chance to connect? (call deferred) (document this in readme)
1. refactor utils engine singleton function to include pipeline retrieval

## Saving/loading

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extends RapierShape3D

@export var radius := 1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extends RapierShape3D

@export var half_extents := Vector3(0.5, 0.5, 0.5)
3 changes: 3 additions & 0 deletions addons/godot-rapier-3d/src/gdscript/shapes/RapierShape3D.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extends Resource
class_name RapierShape3D

45 changes: 25 additions & 20 deletions addons/godot-rapier-3d/src/rust/collider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use crate::rigid_body::RapierRigidBody3D;
use godot::engine::notify::Node3DNotification;
use godot::engine::INode3D;
use godot::engine::Node3D;
use godot::engine::Shape3D;
use godot::prelude::*;
use nalgebra::geometry::OPoint;
use nalgebra::Vector3 as NAVector3;
use rapier3d::prelude::*;

#[derive(GodotClass)]
Expand All @@ -14,8 +13,12 @@ pub struct RapierCollider3D {
#[var]
pub id: Array<Variant>, // ColliderHandle::into_raw_parts
pub handle: ColliderHandle,

#[export]
pub shape: ShapeType,
#[var(
usage_flags = [DEFAULT, EDITOR_INSTANTIATE_OBJECT]
)]
pub shape: Option<Gd<Shape3D>>,
pub parent: Option<RigidBodyHandle>,
notify_parent: bool,
base: Base<Node3D>,
Expand All @@ -27,7 +30,7 @@ impl INode3D for RapierCollider3D {
Self {
id: Array::new(),
handle: ColliderHandle::invalid(),
shape: ShapeType::Ball,
shape: None,
parent: None,
notify_parent: true,
base,
Expand Down Expand Up @@ -178,23 +181,25 @@ impl RapierCollider3D {
}

pub fn build(&self) -> Collider {
let shape = match self.shape {
ShapeType::Ball => SharedShape::ball(0.5),
ShapeType::Cuboid => SharedShape::cuboid(10.0, 1.0, 10.0),
ShapeType::Capsule => {
SharedShape::capsule(OPoint::origin(), OPoint::from(NAVector3::y()), 0.5)
}
};
let collider = ColliderBuilder::new(shape).restitution(0.7).build();
// let shape = match self.shape {
// ShapeType::Ball => SharedShape::ball(0.5),
// ShapeType::Cuboid => SharedShape::cuboid(10.0, 1.0, 10.0),
// ShapeType::Capsule => {
// SharedShape::capsule(OPoint::origin(), OPoint::from(NAVector3::y()), 0.5)
// }
// };
let collider = ColliderBuilder::new(SharedShape::ball(0.5))
.restitution(0.7)
.build();
collider
}
}

#[derive(GodotConvert, Var, Export)]
#[godot(via = GString)]
// https://docs.rs/rapier3d/latest/rapier3d/geometry/enum.ShapeType.html
pub enum ShapeType {
Ball,
Cuboid,
Capsule,
}
// #[derive(GodotConvert, Var, Export)]
// #[godot(via = GString)]
// // https://docs.rs/rapier3d/latest/rapier3d/geometry/enum.ShapeType.html
// pub enum ShapeType {
// Ball,
// Cuboid,
// Capsule,
// }
2 changes: 1 addition & 1 deletion addons/godot-rapier-3d/src/rust/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Rapier3DSingleton;
use crate::singleton::Rapier3DSingleton;
use godot::builtin::Array as GArray;
use godot::builtin::Quaternion as GQuaternion;
use godot::builtin::Vector3 as GVector;
Expand Down
1 change: 1 addition & 0 deletions examples/main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ rotation_order = 0
visible = false

[node name="Ground" parent="." instance=ExtResource("3_ayqm6")]
shape = null
editor_description = "<null>"
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)
rotation_order = 0
Expand Down

0 comments on commit c05e4ec

Please sign in to comment.