Skip to content

Commit a15de50

Browse files
committed
[add] implementations for set_position, set_rotation, set_velocity.
1 parent 1f3036a commit a15de50

1 file changed

Lines changed: 121 additions & 28 deletions

File tree

crates/lambda-rs/src/physics/rigid_body_2d.rs

Lines changed: 121 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ impl RigidBody2D {
6060
world: &PhysicsWorld2D,
6161
) -> Result<RigidBodyType, RigidBody2DError> {
6262
self.validate_handle(world)?;
63-
return Err(RigidBody2DError::BodyNotFound);
63+
let body_type = world
64+
.backend
65+
.rigid_body_type_2d(self.slot_index, self.slot_generation)
66+
.map_err(map_backend_error)?;
67+
68+
return Ok(map_body_type_from_backend(body_type));
6469
}
6570

6671
/// Returns the current position, in meters.
@@ -79,7 +84,10 @@ impl RigidBody2D {
7984
world: &PhysicsWorld2D,
8085
) -> Result<[f32; 2], RigidBody2DError> {
8186
self.validate_handle(world)?;
82-
return Err(RigidBody2DError::BodyNotFound);
87+
return world
88+
.backend
89+
.rigid_body_position_2d(self.slot_index, self.slot_generation)
90+
.map_err(map_backend_error);
8391
}
8492

8593
/// Returns the current rotation, in radians.
@@ -98,7 +106,10 @@ impl RigidBody2D {
98106
world: &PhysicsWorld2D,
99107
) -> Result<f32, RigidBody2DError> {
100108
self.validate_handle(world)?;
101-
return Err(RigidBody2DError::BodyNotFound);
109+
return world
110+
.backend
111+
.rigid_body_rotation_2d(self.slot_index, self.slot_generation)
112+
.map_err(map_backend_error);
102113
}
103114

104115
/// Returns the current linear velocity, in meters per second.
@@ -117,7 +128,10 @@ impl RigidBody2D {
117128
world: &PhysicsWorld2D,
118129
) -> Result<[f32; 2], RigidBody2DError> {
119130
self.validate_handle(world)?;
120-
return Err(RigidBody2DError::BodyNotFound);
131+
return world
132+
.backend
133+
.rigid_body_velocity_2d(self.slot_index, self.slot_generation)
134+
.map_err(map_backend_error);
121135
}
122136

123137
/// Sets the position, in meters.
@@ -141,7 +155,10 @@ impl RigidBody2D {
141155
) -> Result<(), RigidBody2DError> {
142156
validate_position(x, y)?;
143157
self.validate_handle(world)?;
144-
return Err(RigidBody2DError::BodyNotFound);
158+
return world
159+
.backend
160+
.rigid_body_set_position_2d(self.slot_index, self.slot_generation, [x, y])
161+
.map_err(map_backend_error);
145162
}
146163

147164
/// Sets the rotation, in radians.
@@ -163,7 +180,14 @@ impl RigidBody2D {
163180
) -> Result<(), RigidBody2DError> {
164181
validate_rotation(radians)?;
165182
self.validate_handle(world)?;
166-
return Err(RigidBody2DError::BodyNotFound);
183+
return world
184+
.backend
185+
.rigid_body_set_rotation_2d(
186+
self.slot_index,
187+
self.slot_generation,
188+
radians,
189+
)
190+
.map_err(map_backend_error);
167191
}
168192

169193
/// Sets the linear velocity, in meters per second.
@@ -187,7 +211,14 @@ impl RigidBody2D {
187211
) -> Result<(), RigidBody2DError> {
188212
validate_velocity(vx, vy)?;
189213
self.validate_handle(world)?;
190-
return Err(RigidBody2DError::BodyNotFound);
214+
return world
215+
.backend
216+
.rigid_body_set_velocity_2d(
217+
self.slot_index,
218+
self.slot_generation,
219+
[vx, vy],
220+
)
221+
.map_err(map_backend_error);
191222
}
192223

193224
/// Applies a force, in Newtons, at the center of mass.
@@ -573,7 +604,7 @@ mod tests {
573604
use crate::physics::PhysicsWorld2DBuilder;
574605

575606
#[test]
576-
fn builder_inserts_static_body_into_backend() {
607+
fn static_body_is_queryable_and_does_not_move_on_step() {
577608
let mut world = PhysicsWorld2DBuilder::new().build().unwrap();
578609

579610
let body = RigidBody2DBuilder::new(RigidBodyType::Static)
@@ -583,25 +614,14 @@ mod tests {
583614
.build(&mut world)
584615
.unwrap();
585616

586-
let position = world
587-
.backend
588-
.rigid_body_position_2d(body.slot_index, body.slot_generation)
589-
.unwrap();
590-
let rotation = world
591-
.backend
592-
.rigid_body_rotation_2d(body.slot_index, body.slot_generation)
593-
.unwrap();
594-
595-
assert_eq!(position, [1.0, 2.0]);
596-
assert_eq!(rotation, 0.5);
617+
assert_eq!(body.body_type(&world).unwrap(), RigidBodyType::Static);
618+
assert_eq!(body.position(&world).unwrap(), [1.0, 2.0]);
619+
assert_eq!(body.rotation(&world).unwrap(), 0.5);
620+
assert_eq!(body.velocity(&world).unwrap(), [100.0, -200.0]);
597621

598622
world.step();
599623

600-
let position_after_step = world
601-
.backend
602-
.rigid_body_position_2d(body.slot_index, body.slot_generation)
603-
.unwrap();
604-
assert_eq!(position_after_step, [1.0, 2.0]);
624+
assert_eq!(body.position(&world).unwrap(), [1.0, 2.0]);
605625

606626
return;
607627
}
@@ -622,11 +642,84 @@ mod tests {
622642

623643
world.step();
624644

625-
let position_after_step = world
626-
.backend
627-
.rigid_body_position_2d(body.slot_index, body.slot_generation)
645+
assert_eq!(body.position(&world).unwrap(), [0.0, -1.0]);
646+
647+
return;
648+
}
649+
650+
#[test]
651+
fn setters_are_immediately_observable() {
652+
let mut world = PhysicsWorld2DBuilder::new()
653+
.with_gravity(0.0, 0.0)
654+
.with_timestep_seconds(1.0)
655+
.build()
628656
.unwrap();
629-
assert_eq!(position_after_step, [0.0, -1.0]);
657+
658+
let body = RigidBody2DBuilder::new(RigidBodyType::Kinematic)
659+
.with_position(0.0, 0.0)
660+
.build(&mut world)
661+
.unwrap();
662+
663+
body.set_position(&mut world, 10.0, 20.0).unwrap();
664+
body.set_rotation(&mut world, 0.25).unwrap();
665+
body.set_velocity(&mut world, 1.0, 2.0).unwrap();
666+
667+
assert_eq!(body.position(&world).unwrap(), [10.0, 20.0]);
668+
assert_eq!(body.rotation(&world).unwrap(), 0.25);
669+
assert_eq!(body.velocity(&world).unwrap(), [1.0, 2.0]);
670+
671+
world.step();
672+
assert_eq!(body.position(&world).unwrap(), [11.0, 22.0]);
673+
674+
return;
675+
}
676+
677+
#[test]
678+
fn handle_world_mismatch_is_reported() {
679+
let mut world_a = PhysicsWorld2DBuilder::new().build().unwrap();
680+
let world_b = PhysicsWorld2DBuilder::new().build().unwrap();
681+
682+
let body = RigidBody2DBuilder::new(RigidBodyType::Static)
683+
.build(&mut world_a)
684+
.unwrap();
685+
686+
assert_eq!(
687+
body.position(&world_b),
688+
Err(RigidBody2DError::WorldMismatch)
689+
);
690+
691+
return;
692+
}
693+
694+
#[test]
695+
fn handle_body_not_found_is_reported() {
696+
let world = PhysicsWorld2DBuilder::new().build().unwrap();
697+
698+
let body = RigidBody2D {
699+
world_id: world.world_id,
700+
slot_index: 999,
701+
slot_generation: 1,
702+
};
703+
704+
assert_eq!(body.position(&world), Err(RigidBody2DError::BodyNotFound));
705+
706+
return;
707+
}
708+
709+
#[test]
710+
fn static_set_velocity_returns_unsupported_operation() {
711+
let mut world = PhysicsWorld2DBuilder::new().build().unwrap();
712+
713+
let body = RigidBody2DBuilder::new(RigidBodyType::Static)
714+
.build(&mut world)
715+
.unwrap();
716+
717+
assert_eq!(
718+
body.set_velocity(&mut world, 1.0, 2.0),
719+
Err(RigidBody2DError::UnsupportedOperation {
720+
body_type: RigidBodyType::Static,
721+
})
722+
);
630723

631724
return;
632725
}

0 commit comments

Comments
 (0)