larvaworld.lib.model.box2d

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

SegmentBox2D

Box2D-based segment of a larva.

LarvaBox2D

Box2D-based larva simulation.

ArenaBox2D

Module Contents

class larvaworld.lib.model.box2d.SegmentBox2D(space: Box2D.b2.world, physics_pars: dict[str, Any], **kwargs: Any)

Bases: 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.

get_position() numpy.ndarray

Get the world position of the segment.

Returns

numpy.ndarray

The world position of the segment.

set_position(position: Tuple[float, float]) None

Set the world position of the segment.

Parameters

positiontuple

The new world position.

get_orientation() float

Get the orientation of the segment.

Returns

float

The orientation of the segment.

set_orientation(orientation: float) None

Set the orientation of the segment.

Parameters

orientationfloat

The new orientation of the segment.

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.

set_linearvelocity(lin_vel: Tuple[float, float], local: bool = False) None

Set the linear velocity of the segment.

Parameters

lin_veltuple

The new linear velocity.

localbool, optional

Whether the linear velocity is in local coordinates. Defaults to False.

get_angularvelocity() float

Get the angular velocity of the segment.

Returns

float

The angular velocity of the segment.

set_angularvelocity(ang_vel: float) None

Set the angular velocity of the segment.

Parameters

ang_velfloat

The new angular velocity of the segment.

set_mass(mass: float) None

Set the mass of the segment.

Parameters

massfloat

The new mass of the segment.

get_mass() float

Get the mass of the segment.

Returns

float

The mass of the segment.

get_world_point(point: Tuple[float, float]) numpy.ndarray

Transform a local point to world coordinates.

Parameters

pointtuple

The local point coordinates.

Returns

numpy.ndarray

The world coordinates of the point.

get_world_facing_axis() numpy.ndarray

Get the world-facing axis of the segment.

Returns

numpy.ndarray

The world-facing axis of the segment.

class larvaworld.lib.model.box2d.LarvaBox2D(Box2D: dict[str, Any], **kwargs: Any)

Bases: larvaworld.lib.model.agents.LarvaSim

Box2D-based larva simulation.

segs
Box2D_params
generate_segs() None

Generate the segments of the larva.

prepare_motion(lin: float, ang: float) None

Prepare the larva for motion with given linear and angular velocities.

Parameters

linfloat

Linear velocity.

angfloat

Angular velocity.

updated_by_Box2D() None

Update the larva simulation based on Box2D physics.

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

Nsegsint

The number of segments in the larva.

segslist of Box2DSegment

The list of Box2DSegment objects representing the larva segments.

joint_typesdict, 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.

class larvaworld.lib.model.box2d.ArenaBox2D(**kwargs: Any)

Bases: larvaworld.lib.model.envs.Arena, Box2D.b2.world

ground_body: Box2D.b2.staticBody