# Simulator¶

class simulator.Simulator(renderer, in_queue)[source]

The simulator manages simobjects and their collisions, commands supervisors and draws the world using the supplied renderer.

The simulator runs in a separate thread. None of its functions are thread-safe, and should never be called directly from other objects (except for the functions inherited from threading.Thread). The communication with the simulator should be done through its in_queue and out_queue. See Interaction between GUI and the Simulator.

Parameters: renderer (Renderer) – The renderer that will be used to draw the world. The simulator will assume control of the renderer. The renderer functions also have to be considered thread-unsafe. in_queue (Queue.Queue) – The queue that is used to send events to the simulator.

A plotable is an expression that yields a numerical value. It is evaluated every cycle and the values are announced by the simulator in the plot_update signal.

The expression has access to the following variables: robot - the first robot in the scene supervisor - the supervisor of this robot math - the math module

Zoom the view by factor

apply_parameters(robot, parameters)[source]

Apply parameters to the supervisor of robot.

The parameters have to correspond to the requirements of the supervisor, as specified in supervisor.Supervisor.get_ui_description()

focus_on_robot(rotate=True)[source]

Center the view on the (first) robot and follow it.

If rotate is true, also follow the robot’s orientation.

focus_on_world()[source]

Scale the view to include all of the world (including robots)

get_time()[source]

Get the internal simulator time.

is_running()[source]

Get the simulation state as a bool

pause_simulation()[source]

Pause the simulation

plotables()[source]

Returns a list with some examples of plotables

Load in the objects from the world XML file

reset_simulation()[source]

Reset the simulation to the start position

run()[source]

Start the thread. In the beginning there’s no world, no obstacles and no robots.

The simulator will try to draw the world undependently of the simulation status, so that the commands from the UI get processed.

set_time_multiplier(multiplier)[source]

Shorten the interval between evaluation cycles by multiplier, speeding up the simulation

show_grid(show=True)[source]

Show/hide gridlines on simulator view

show_sensors(show=True)[source]

Show or hide the robots’ sensors on the simulation view

show_supervisors(show=True)[source]

Show/hide the information from the supervisors

show_tracks(show=True)[source]

Show/hide tracks for every robot on simulator view

start_simulation()[source]

Start/continue the simulation

step_simulation()[source]

Do one step

stop()[source]

Stop the simulator thread when the entire program is closed

## Helper structures¶

### Module strings¶

pySimiam loads user code dynamically. This includes robot definitions, supervisors and controllers. It is important to specify the type of the robot and its supervisor in the world XML file (see World files), and the type of a controller in supervisor.Supervisor.add_controller()

The type is specified as a single string with the format "mymodule.MyClass". It is equivalent to a python construct from mymodule import MyClass. If mymodule is omitted, like in "MyClass", the corresponding statement is from myclass import MyClass (note the lowercase module name). If there is more than one point in the string, the class name is taken to be everything after the last point.

The file mymodule.py is expected to reside in the appropriate folder: ./robots/ for robots, ./controllers/ for controllers and ./supervisors/ for supervisors.

Load a class from a module, specified by module_string.

The path is an additional path that is prepended to the module string.

E.g. C = load_by_name('mymodule.MyClass','path.to.module') is equivalent to from path.to.module.mymodule import MyClass as C.

### Structures¶

class helpers.Struct(desc=None)[source]

This class describes structures with arbitrary fields. It is used, e.g. for the communication between the supervisor and the UI.

Example:

p = Struct()
p.goal = Struct()
p.goal.x = 0.0
p.goal.y = 0.5
p.velocity = Struct()
p.velocity.v = 0.2
p.gains = Struct()
p.gains.kp = 10.0
p.gains.ki = 2.0
p.gains.kd = 0.0


Alternatives:

p = Struct({'goal':{'x':0.0, 'y':0.5}, 'velocity':{'v':0.2}, 'gains':{'kp':10.0, 'ki':2.0, 'kd':0.0}})
p = Struct("{'goal':{'x':0.0, 'y':0.5}, 'velocity':{'v':0.2}, 'gains':{'kp':10.0, 'ki':2.0, 'kd':0.0}}")


In the second case, the string has to be a valid JSON object. It is also impossible to use dictionaries as values in the Struct in these cases.

## Collision detection¶

polygon object

class pylygon.Polygon(P, conv=True)[source]

polygon object

C[source]

returns the centroid of the polygon

collidepoint(x_y)[source]

test if point x_y = (x, y) is outside, on the boundary, or inside polygon uses raytracing algorithm

returns 0 if outside returns -1 if on boundary returns 1 if inside

collidepoly(other)[source]

test if other polygon collides with self using seperating axis theorem if collision, return projections

arguments: other – a polygon object

returns: an array of projections

distance(other, r=array([0, 0]))[source]

return distance between self and other uses GJK algorithm. for details see:

Bergen, Gino Van Den. (1999). A fast and robust GJK implementation for collision detection of convex objects. Journal of Graphics Tools 4(2).

arguments: other – a Polygon object

keyword arguments r – initial search direction; setting r to the movement vector of self - other may speed convergence

get_rect()[source]

return the AABB, as a pygame rect, of the polygon

intersection_points(other)[source]

Determine contact (intersection) points between self and other.

returns Empty list if no collisions found returns List of intersection points

move(x, y)[source]

return a new polygon moved by x, y

move_ip(x, y)[source]

move the polygon by x, y

project(axis)[source]

project self onto axis

raycast(other, r, s=array([0, 0]), self_theta=0, other_theta=0)[source]

return the hit scalar, hit vector, and hit normal from self to other in direction r uses GJK-based raycast[1] modified to accomodate constant angular rotation[2][3] without needing to recompute the Minkowski Difference after each iteration[4].

[1] Bergen, Gino Van Den. (2004). Ray casting against general convex objects with application to continuous collision detection. GDC 2005. retrieved from http://www.bulletphysics.com/ftp/pub/test/physics/papers/ jgt04raycast.pdf on 6 July 2011.

[2] Coumans, Erwin. (2005). Continuous collision detection and physics. retrieved from http://www.continuousphysics.com/ BulletContinuousCollisionDetection.pdf on 18 January 2012

[3] Mirtich, Brian Vincent. (1996). Impulse-based dynamic simulation of rigid body systems. PhD Thesis. University of California at Berkely. retrieved from http://www.kuffner.org/james/software/dynamics/mirtich/ mirtichThesis.pdf on 18 January 2012

[4] Behar, Evan and Jyh-Ming Lien. (2011). Dynamic Minkowski Sum of convex shapes. In proceedings of IEEE ICRA 2011. retrieved from http://masc.cs.gmu.edu/wiki/uploads/GeneralizedMsum/ icra11-dynsum-convex.pdf on 18 January 2012.

arguments: other – Polygon object r – direction vector NOTE: GJK searches IN THE DIRECTION of r, thus r needs to point towards the origin with respect to the direction vector of self; in other words, if r represents the movement of self then client code should call raycast with -r.

keyword arguments: s – initial position along r, (0, 0) by default theta – angular velocity in radians

returns: if r does not intersect other, returns False else, returns the hit scalar, hit vector, and hit normal hit scalar – the scalar where r intersects other hit vector – the vector where self intersects other hit normal – the edge normal at the intersection

rotoedges(theta)[source]

return an array of vectors of edges rotated theta radians

rotopoints(theta)[source]

returns an array of points rotated theta radians around the centroid

pylygon.convexhull(_P)[source]

Returns an array of the points in convex hull of P in CCW order.

arguments: P – a Polygon object or an numpy.array object of points

class pylygon.Polygon(P, conv=True)[source]

polygon object

C[source]

returns the centroid of the polygon

collidepoint(x_y)[source]

test if point x_y = (x, y) is outside, on the boundary, or inside polygon uses raytracing algorithm

returns 0 if outside returns -1 if on boundary returns 1 if inside

collidepoly(other)[source]

test if other polygon collides with self using seperating axis theorem if collision, return projections

arguments: other – a polygon object

returns: an array of projections

distance(other, r=array([0, 0]))[source]

return distance between self and other uses GJK algorithm. for details see:

Bergen, Gino Van Den. (1999). A fast and robust GJK implementation for collision detection of convex objects. Journal of Graphics Tools 4(2).

arguments: other – a Polygon object

keyword arguments r – initial search direction; setting r to the movement vector of self - other may speed convergence

get_rect()[source]

return the AABB, as a pygame rect, of the polygon

intersection_points(other)[source]

Determine contact (intersection) points between self and other.

returns Empty list if no collisions found returns List of intersection points

move(x, y)[source]

return a new polygon moved by x, y

move_ip(x, y)[source]

move the polygon by x, y

project(axis)[source]

project self onto axis

raycast(other, r, s=array([0, 0]), self_theta=0, other_theta=0)[source]

return the hit scalar, hit vector, and hit normal from self to other in direction r uses GJK-based raycast[1] modified to accomodate constant angular rotation[2][3] without needing to recompute the Minkowski Difference after each iteration[4].

[1] Bergen, Gino Van Den. (2004). Ray casting against general convex objects with application to continuous collision detection. GDC 2005. retrieved from http://www.bulletphysics.com/ftp/pub/test/physics/papers/ jgt04raycast.pdf on 6 July 2011.

[2] Coumans, Erwin. (2005). Continuous collision detection and physics. retrieved from http://www.continuousphysics.com/ BulletContinuousCollisionDetection.pdf on 18 January 2012

[3] Mirtich, Brian Vincent. (1996). Impulse-based dynamic simulation of rigid body systems. PhD Thesis. University of California at Berkely. retrieved from http://www.kuffner.org/james/software/dynamics/mirtich/ mirtichThesis.pdf on 18 January 2012

[4] Behar, Evan and Jyh-Ming Lien. (2011). Dynamic Minkowski Sum of convex shapes. In proceedings of IEEE ICRA 2011. retrieved from http://masc.cs.gmu.edu/wiki/uploads/GeneralizedMsum/ icra11-dynsum-convex.pdf on 18 January 2012.

arguments: other – Polygon object r – direction vector NOTE: GJK searches IN THE DIRECTION of r, thus r needs to point towards the origin with respect to the direction vector of self; in other words, if r represents the movement of self then client code should call raycast with -r.

keyword arguments: s – initial position along r, (0, 0) by default theta – angular velocity in radians

returns: if r does not intersect other, returns False else, returns the hit scalar, hit vector, and hit normal hit scalar – the scalar where r intersects other hit vector – the vector where self intersects other hit normal – the edge normal at the intersection

rotoedges(theta)[source]

return an array of vectors of edges rotated theta radians

rotopoints(theta)[source]

returns an array of points rotated theta radians around the centroid

class rect.Rect(xywh)[source]

The Rect class is used for storing and manipulating rectangular areas.

It has left, bottom, width and height attributes, which are automatically changed by assignment to the right, top, topleft, topright, bottomleft, bottomright or center properties.

Rects can be added to greater a greater containing rectangle, or a Rect.union classmethod is available to sum a list of Rect objects.

The collidepoint and intersects methods are used for collision testing.

Eg: >> Rect((0,0,10,10)).collidepoint((2,2)) >> True >> Rect((0,0,10,10)).collidepoint((20,20)) >> False

This Rect class is different to the Pygame Rect class, in that is stores coordinates internally as floats, and uses a left-handed coordinate system.

Add another rect to this rect, expanding as needed.

collidepoint(xy)[source]

Test if a point intersects with this rect.

contains(other)[source]

Return True if other contains self

intersection(other)[source]

Return the intersection of this rect and other rect. Return None if no intersection.

intersects(other)[source]

Test if a rect intersects with this rect.

classmethod sum(others)[source]

Return a rect which covers all rects in others.

class rect.Rect(xywh)[source]

The Rect class is used for storing and manipulating rectangular areas.

It has left, bottom, width and height attributes, which are automatically changed by assignment to the right, top, topleft, topright, bottomleft, bottomright or center properties.

Rects can be added to greater a greater containing rectangle, or a Rect.union classmethod is available to sum a list of Rect objects.

The collidepoint and intersects methods are used for collision testing.

Eg: >> Rect((0,0,10,10)).collidepoint((2,2)) >> True >> Rect((0,0,10,10)).collidepoint((20,20)) >> False

This Rect class is different to the Pygame Rect class, in that is stores coordinates internally as floats, and uses a left-handed coordinate system.

Add another rect to this rect, expanding as needed.

collidepoint(xy)[source]

Test if a point intersects with this rect.

contains(other)[source]

Return True if other contains self

intersection(other)[source]

Return the intersection of this rect and other rect. Return None if no intersection.

intersects(other)[source]

Test if a rect intersects with this rect.

classmethod sum(others)[source]

Return a rect which covers all rects in others.

QuadTree data structure of simulated objects

find_items(xywh)[source]

Returns the items that overlap a bounding rectangle.

Returns the set of all items in the quad-tree that overlap with a bounding rectangle.

@param xywh:
The bounding rectangle being tested against the quad-tree.
insert_items(items)[source]

Insert a list of SimObject items

QuadTree data structure of simulated objects

find_items(xywh)[source]

Returns the items that overlap a bounding rectangle.

Returns the set of all items in the quad-tree that overlap with a bounding rectangle.

@param xywh:
The bounding rectangle being tested against the quad-tree.
insert_items(items)[source]

Insert a list of SimObject items