Skip to content

Implementation of rotate_by on Aabb2d is incorrect #18969

Closed
@kyon4ik

Description

@kyon4ik

Bevy version

0.16

What you did

let bullet_rotation = transform.rotation.to_euler(EulerRot::XYZ).2;
let bullet_aabb = Aabb2d::new(Vec2::ZERO, BULLET_SIZE / 2.0).rotated_by(bullet_rotation);

What went wrong

I was expected Aabb2d to rotate by any angle between -PI and PI, but instead get assertion failed: half_size.x >= 0.0 && half_size.y >= 0.0 at bevy_math-0.16.0/src/bounding/bounded2d/mod.rs:61:9.

Additional information

I investigated this and found that rotate_by implementation is probably incorrect:

fn rotate_by(&mut self, rotation: impl Into<Self::Rotation>) {
    let rotation: Rot2 = rotation.into();
    let abs_rot_mat = Mat2::from_cols(
        Vec2::new(rotation.cos, rotation.sin),
        Vec2::new(rotation.sin, rotation.cos),
    );
    let half_size = abs_rot_mat * self.half_size();
    *self = Self::new(rotation * self.center(), half_size);
}

As name of abs_rot_mat suggests it should have absolute values, so my current fixed version looks like this:

fn rotate_by(&mut self, rotation: impl Into<Self::Rotation>) {
    let rotation: Rot2 = rotation.into();
    let rot_mat = Mat2::from_cols(
        Vec2::new(rotation.cos, rotation.sin),
        Vec2::new(rotation.sin, rotation.cos),
    );
    let half_size = rot_mat.abs() * self.half_size();
    *self = Self::new(rotation * self.center(), half_size);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MathFundamental domain-agnostic mathematical operationsC-BugAn unexpected or incorrect behaviorS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions