Skip to content

Commit

Permalink
Add GravityEnabled boolean component (#2451)
Browse files Browse the repository at this point in the history
This adds a boolean GravityEnabled component to the existing gz/sim/components/Gravity.hh header file, which should be used with Link entities instead of the Vector3 gravity component.

---------

Signed-off-by: Steve Peters <scpeters@openrobotics.org>
Signed-off-by: youhy <haoyuan2019@outlook.com>
Co-authored-by: youhy <haoyuan2019@outlook.com>
  • Loading branch information
scpeters and AzulRadio authored Jun 26, 2024
1 parent 45239e9 commit ca1b626
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
5 changes: 5 additions & 0 deletions include/gz/sim/components/Gravity.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ namespace components
/// \brief Store the gravity acceleration.
using Gravity = Component<math::Vector3d, class GravityTag>;
IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.Gravity", Gravity)

/// \brief Store the gravity acceleration.
using GravityEnabled = Component<bool, class GravityEnabledTag>;
IGN_GAZEBO_REGISTER_COMPONENT(
"ign_gazebo_components.GravityEnabled", GravityEnabled)
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/SdfEntityCreator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,9 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Link *_link)

if (!_link->EnableGravity())
{
// If disable gravity, create a gravity component to the entity
// This gravity will have value 0,0,0
// If disable gravity, create a GravityEnabled component to the entity
this->dataPtr->ecm->CreateComponent(
linkEntity, components::Gravity());
linkEntity, components::GravityEnabled(false));
}

// Visuals
Expand Down
21 changes: 6 additions & 15 deletions src/systems/physics/Physics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1161,21 +1161,12 @@ void PhysicsPrivate::CreateLinkEntities(const EntityComponentManager &_ecm)
}

// get link gravity
const components::Gravity *gravity =
_ecm.Component<components::Gravity>(_entity);
if (nullptr != gravity)
{
// Entity has a gravity component that is all zeros when
// <gravity> is set to false
// See SdfEntityCreator::CreateEntities()
if (gravity->Data() == math::Vector3d::Zero)
{
link.SetEnableGravity(false);
}
else
{
link.SetEnableGravity(true);
}
const components::GravityEnabled *gravityEnabled =
_ecm.Component<components::GravityEnabled>(_entity);
if (nullptr != gravityEnabled)
{
// gravityEnabled set in SdfEntityCreator::CreateEntities()
link.SetEnableGravity(gravityEnabled->Data());
}

auto linkPtrPhys = modelPtrPhys->ConstructLink(link);
Expand Down

0 comments on commit ca1b626

Please sign in to comment.