.. _rigid_bodies: ============= Rigid Bodies ============= Translations and rotations ========================== Translation and rotation operations change the position and orientation of a rigid body and they do not modify the shape. Mekanimo allows users to translate, reorient, or rotate (translate + reorient) a rigid body via GUI or code. Translate (move) ******************** Rigid bodies can be moved via GUI or code. You can click on a rigid body and drag it to change its position or use the object tree. Rigid bodies can also be repositioned by assigning a new positon (x, y pair) via code as shown below. .. code-block:: python >>> circle1.position = [20, 35] It is also possible to change the x and y coordinates of a rigid body individually. .. code-block:: python >>> rectangle1.x = 42 >>> polygon2.y += 25 The object tree can be used to assign coordinates to rigid bodies too (see Figure1). .. figure:: object_tree_position.png :alt: object_tree_position Figure 1: Modifying position from the object tree Reorient (rotate) ****************** Reorientation changes the orientation angle of a rigid body but not its position. When rigid bodies are selected their rotation handles become visible as depicted in Figure 2. By default, root of the rotation handle is located at the center of gravity (or local [0, 0] point). When you click and drag the tip of the rotaition handle, the selected rigid body rotates about the root of the rotation handle. Since, the center of gravity and root of the rotation handle is coincident by default, this action changes only the orientation angle but not the position of the selected rigid body. .. figure:: rotation_handle.png :alt: rotation_handle Figure 2: Rotation handle Two examples of reorienting with code is shown below. Please note that both examples would yield the same transformation. .. code-block:: python >>> circle1.rotate(.2) >>> circle1.angle += .2 In addition to rotation operations, orientation of the rigid bodies can be specified directly as shown below. .. code-block:: python >>> circle1.angle = pi / 3 .. Note:: All angles in Mekanimo are specified in radians and counter-clockwise direction is the positive rotation direction. And finally, if you need to rotate a rigid body by 30, 45, 60, 90, or 180 degrees via GUI, you can use the popup menu as shown below. .. figure:: regular_angles.png :alt: regular_angles Figure 3: Rotating by frequently used angles. Rotate about a point ******************** When rigid bodies are rotated about a point their rotation angles and potentially positions change accordingly. As explained in the section above, if the center of rotation does not coincide with the center of gravity of the rigid body in question, then both orientation angle and position of the rigid body are altered due to rotation. To rotate a rigid body about a point via GUI, you can drag the root of the rotation handle to reposition it. Figure 4 shows a rectangle with repositioned rotation handle. .. figure:: rotation_handle_root.png :alt: rotation_handle_root Figure 4: Repositioned rotation center .. code-block:: python >>> circle1.rotateAbout([25, 35], .2) To relocate the rotation handle root at the center of gravity, right click on the canvas to bring up the popup menu and then select the *Reset rotation center* option as shown below. .. figure:: reset_rotation_center.png :alt: reset_rotation_center Figure 5: Reseting rotation handle root Alternatively, rotation handle root can be relocated to the default position from code as shown below. .. code-block:: python >>> rectangle1.rotCenter = [0, 0] Reflect ======= Rigid bodies and connectors can be reflected either horizontally or vertically by using the popup menu or from the Python interpreter as shown below. .. figure:: reflect.png :alt: reflect Figure 6: Reflecting elements from the Python interpreter. .. code-block:: python >>> system.reflect(polygon1, VERTICAL, 25) >>> system.reflect([circle1, circle2, pin1], HORIZONTAL, 33.5) When you use the popup menu, outlines of the selected elements move according to the mouse position. If the vertical distance between the mouse cursor and center of gravity of the selected element/s is larger than that of the horizontal distance then Mekanimo displays vertical reflection outlines. Reflection operation is completed upon left-click. Press ``Esc`` key to cancel the operation without reflecting the selected objects. Circles ======== .. index:: single: Circles In Mekanimo, circles are defined by their center point and radius. The first circle of the model is named *Circle1* with an alias *c1* , the second one is named *Circle2* with and alias *c2* and so on. Like any other element, circles can be renamed if desired. When the canvas is in *CIRCLE* mode, a circle can be created by clicking and dragging. Where you click defines the center and the distance between the center and where you release the mouse defines the radius. Circles can be created from the Python interpreter as shown below: .. code-block:: python >>> circle(40, 50, 3) Modifying Circles ****************** Polygons ======== .. index:: single: Polygons Mekanimo supports only simple polygons that can be either convex or concave (see Figure 6). Simple polygons have no intersecting edges, Figure 7 depicts a self intersecting polygon. .. figure:: concave_convex.png :alt: concave_convex Figure 6: Simple polygons .. figure:: self_intersecting_polygon.png :alt: self_intersecting_polygon Figure 7: A self-intersecting polygon Modifying Polygons ****************** Like all other rigid bodies polygons can be resized, scaled and rotated. In addition, individual vertices can be modified too. While scaling does not change an element's position resizing may cause the position of an element to be modified as well. To resize a polygon with GUI, simply drag any of the resizing handles. .. code-block:: python polygon1.resize(pos=[45.5, 18.0], scaleX=1.2, scaleY=1.5) polygon1.scale(scaleX=2, scaleY=2) To edit vertices right click on the canvas to bring up the popup menu and select `Edit Vertices`. Now you can drag the vertices. To finish the operation, click on an empty area in the canvas. .. figure:: edit_vertices.png :alt: edit_vertices Figure 8: Editing vertices To programmatically edit vertices you need to specify the positions of all vertices as shown below .. code-block:: python system.editVertices(polygon1, [[35.088471664191985, 21.203194107239007], [45.127152060097174, 35.27611055009676], [30.074017533651386, 43.1123577014774], [20.658360670195588, 41.01961089298856], [19.702083020000863, 30.672140562127066], [23.821432897762772, 24.393900136660545]]) Rectangles ========== .. index:: single: Rectangles Modifying Rectangles ********************* Like all other rigid bodies rectangles can be resized, scaled and rotated. While scaling does not change an element's position resizing may cause the position of an element to be modified as well. To resize a rectangle with GUI, simply drag any of the resizing handles. .. code-block:: python rectangle1.resize(pos=[45.5, 18.0], scaleX=1.2, scaleY=1.5) rectangle1.scale(scaleX=2, scaleY=2)