Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate and move object in bevy

Tags:

rust

bevy

I want to rotate my object by a given amount and translate it forward to create a steerable tank.

I couldn't find out how to do it, all the matrices, vectors, and quaternions make it difficult for me to find a solution.

This is the Unity equivalent of what i want to do:

transform.Rotate(0, 0, -turn_input * turnSpeed * Time.deltaTime);
transform.position += transform.forward * drive * speed * Time.deltaTime;

I used to use this code in Bevy 0.2.1 but it broke after updating to 0.4

*transform.value_mut() = *transform.value()
    * Mat4::from_rotation_translation(
        Quat::from_rotation_z(-turn_input * tank.turn_speed * time.delta_seconds),
        Vec3::unit_y() * drive * tank.speed * time.delta_seconds,
    );
like image 810
Redline Avatar asked Nov 05 '25 13:11

Redline


1 Answers

That was changed in Bevy 0.3 with "Move transform data out of Mat4" (PR #596)

This changed, so instead of Transform having a value: Mat4 field, then instead it would have translation: Vec3, rotation: Quat, and scale: Vec3.

If you want a literal translation of your code, then that would be:

transform = Transform::from_matrix(
    transform.compute_matrix()
        * Mat4::from_rotation_translation(
            Quat::from_rotation_z(-turn_input * tank.turn_speed * time.delta_seconds),
            Vec3::unit_y() * drive * tank.speed * time.delta_seconds,
        ),
);

However, it might be more straightforward to use transform.rotate() and/or assign directly to transform.translation and transform.rotation.

like image 160
vallentin Avatar answered Nov 08 '25 10:11

vallentin