larvaworld.lib.model.box2d ========================== .. py:module:: larvaworld.lib.model.box2d .. autoapi-nested-parse:: A Box2D extension for larvaworld. This requires the box2d-py package, specified as an optional dependency during larvaworld installation. If not already installed, run : "pip install box2d-py" Classes ------- .. autoapisummary:: larvaworld.lib.model.box2d.SegmentBox2D larvaworld.lib.model.box2d.LarvaBox2D larvaworld.lib.model.box2d.ArenaBox2D Module Contents --------------- .. py:class:: SegmentBox2D(space: Box2D.b2.world, physics_pars: dict[str, Any], **kwargs: Any) Bases: :py:obj:`larvaworld.lib.param.ShapeMobile` Box2D-based segment of a larva. Args: space (Box2D.b2World): The Box2D world space. physics_pars (dict): Parameters related to the physics simulation. **kwargs (dict): Additional keyword arguments. Methods: get_position(): Get the world position of the segment. set_position(position): Set the world position of the segment. get_orientation(): Get the orientation of the segment. set_orientation(orientation): Set the orientation of the segment. get_pose(): Get the pose (position and orientation) of the segment. set_linearvelocity(lin_vel, local=False): Set the linear velocity of the segment. get_angularvelocity(): Get the angular velocity of the segment. set_angularvelocity(ang_vel): Set the angular velocity of the segment. set_mass(mass): Set the mass of the segment. get_mass(): Get the mass of the segment. get_world_point(point): Transform a local point to world coordinates. get_world_facing_axis(): Get the world-facing axis of the segment. .. py:method:: get_position() -> numpy.ndarray Get the world position of the segment. Returns ------- numpy.ndarray The world position of the segment. .. py:method:: set_position(position: Tuple[float, float]) -> None Set the world position of the segment. Parameters ---------- position : tuple The new world position. .. py:method:: get_orientation() -> float Get the orientation of the segment. Returns ------- float The orientation of the segment. .. py:method:: set_orientation(orientation: float) -> None Set the orientation of the segment. Parameters ---------- orientation : float The new orientation of the segment. .. py:method:: get_pose() -> Tuple[float, float, float] Get the pose (position and orientation) of the segment. Returns ------- tuple The pose of the segment, including position and orientation. .. py:method:: set_linearvelocity(lin_vel: Tuple[float, float], local: bool = False) -> None Set the linear velocity of the segment. Parameters ---------- lin_vel : tuple The new linear velocity. local : bool, optional Whether the linear velocity is in local coordinates. Defaults to False. .. py:method:: get_angularvelocity() -> float Get the angular velocity of the segment. Returns ------- float The angular velocity of the segment. .. py:method:: set_angularvelocity(ang_vel: float) -> None Set the angular velocity of the segment. Parameters ---------- ang_vel : float The new angular velocity of the segment. .. py:method:: set_mass(mass: float) -> None Set the mass of the segment. Parameters ---------- mass : float The new mass of the segment. .. py:method:: get_mass() -> float Get the mass of the segment. Returns ------- float The mass of the segment. .. py:method:: get_world_point(point: Tuple[float, float]) -> numpy.ndarray Transform a local point to world coordinates. Parameters ---------- point : tuple The local point coordinates. Returns ------- numpy.ndarray The world coordinates of the point. .. py:method:: get_world_facing_axis() -> numpy.ndarray Get the world-facing axis of the segment. Returns ------- numpy.ndarray The world-facing axis of the segment. .. py:class:: LarvaBox2D(Box2D: dict[str, Any], **kwargs: Any) Bases: :py:obj:`larvaworld.lib.model.agents.LarvaSim` Box2D-based larva simulation. .. py:attribute:: segs .. py:attribute:: Box2D_params .. py:method:: generate_segs() -> None Generate the segments of the larva. .. py:method:: prepare_motion(lin: float, ang: float) -> None Prepare the larva for motion with given linear and angular velocities. Parameters ---------- lin : float Linear velocity. ang : float Angular velocity. .. py:method:: updated_by_Box2D() -> None Update the larva simulation based on Box2D physics. .. py:method:: create_joints(Nsegs: int, segs: List[SegmentBox2D], joint_types: dict[str, Any] | None = None) -> None Create joints to connect the segments of the larva. Parameters ---------- Nsegs : int The number of segments in the larva. segs : list of Box2DSegment The list of Box2DSegment objects representing the larva segments. joint_types : dict, optional A dictionary specifying the types of joints to create. The dictionary should contain keys for different joint types ('distance', 'revolute', 'friction') and values specifying the number of joints of each type to create ('N') and the joint parameters ('args') for each type. Notes ----- The `joint_types` parameter is optional and, if not provided, it will use the joint types and parameters defined in the `Box2D` attribute of the larva simulation. This method creates various types of joints (distance, revolute, and friction) to connect the segments of the larva together in a physically realistic way. .. py:class:: ArenaBox2D(**kwargs: Any) Bases: :py:obj:`larvaworld.lib.model.envs.Arena`, :py:obj:`Box2D.b2.world` .. py:attribute:: ground_body :type: Box2D.b2.staticBody