Skip to content

center of mass vector issue? #617

@tomara-x

Description

@tomara-x

when spawning objects with a CenterOfMass it apprears as though the given Vec2 was .perp()ed

top left: (50., 0.)
top right: (0., 50.)
bottom left: (-50., 0.)
bottom right: (0., 50.)

Screenshot_2025-01-08_22-30-46

use avian2d::prelude::*;
use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(PhysicsPlugins::default().with_length_unit(100.))
        .add_plugins(PhysicsDebugPlugin::default())
        .add_systems(Startup, setup)
        .insert_resource(SleepingThreshold {
            linear: -1.,
            angular: -1.,
        })
        .insert_resource(Gravity::ZERO)
        .run();
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    commands.spawn(Camera2d);
    let c = Color::hsl(100., 1., 0.2);
    let mesh = meshes.add(RegularPolygon::new(1., 6));
    let mat = materials.add(ColorMaterial::from_color(c));
    // center (copy of the top left but perped 3 times)
    commands.spawn((
        Mesh2d(mesh.clone()),
        MeshMaterial2d(mat.clone()),
        RigidBody::Dynamic,
        Collider::regular_polygon(1., 6),
        CenterOfMass(Vec2::new(50., 0.).perp().perp().perp()),
        Transform {
            translation: Vec3::new(0., 0., 0.),
            scale: Vec3::new(100., 100., 1.),
            ..default()
        },
    ));
    // top left
    commands.spawn((
        Mesh2d(mesh.clone()),
        MeshMaterial2d(mat.clone()),
        RigidBody::Dynamic,
        Collider::regular_polygon(1., 6),
        CenterOfMass(Vec2::new(50., 0.)),
        Transform {
            translation: Vec3::new(-300., 200., 0.),
            scale: Vec3::new(100., 100., 1.),
            ..default()
        },
    ));
    // top right
    commands.spawn((
        Mesh2d(mesh.clone()),
        MeshMaterial2d(mat.clone()),
        RigidBody::Dynamic,
        Collider::regular_polygon(1., 6),
        CenterOfMass(Vec2::new(0., 50.)),
        Transform {
            translation: Vec3::new(300., 200., 0.),
            scale: Vec3::new(100., 100., 1.),
            ..default()
        },
    ));
    // bottom left
    commands.spawn((
        Mesh2d(mesh.clone()),
        MeshMaterial2d(mat.clone()),
        RigidBody::Dynamic,
        Collider::regular_polygon(1., 6),
        CenterOfMass(Vec2::new(-50., 0.)),
        Transform {
            translation: Vec3::new(-300., -200., 0.),
            scale: Vec3::new(100., 100., 1.),
            ..default()
        },
    ));
    // bottom right
    commands.spawn((
        Mesh2d(mesh.clone()),
        MeshMaterial2d(mat.clone()),
        RigidBody::Dynamic,
        Collider::regular_polygon(1., 6),
        CenterOfMass(Vec2::new(0., -50.)),
        Transform {
            translation: Vec3::new(300., -200., 0.),
            scale: Vec3::new(100., 100., 1.),
            ..default()
        },
    ));
}

avian is amazing! thank you so much for making it! 🧡

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-TransformRelates to transforms or physics positionsC-BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions