Detect objects colliding with the current object.
Sensor to detect objects colliding with the current object.
It allows to select which objects are sensed for collision by setting the property only_objects_with_property (see the Examples section for an example).
This sensor is a wrapper around Blender’s own Collision sensor.
You can set these properties in your scripts with <component>.properties(<property1>=..., <property2>=...).
Only report collision with objects that have this property, default “” (all objects)
This sensor exports these datafields at each simulation step:
number of seconds in simulated time
objects colliding with the current object
(attention, no interface support!)
Returns the properties of a component.
a dictionary of the current component’s properties
Returns the current data stored in the sensor.
a dictionary of the current sensor’s data
Modify one property on a component
Returns the configurations of a component (parsed from the properties).
a dictionary of the current component’s configurations
The following examples show how to use this component in a Builder script:
from morse.builder import * # adds a default robot (the MORSE mascott!) robot = Morsy() # create and add a Collision sensor collision = Collision() # place it on the front side of the robot collision.translate(0.43,0,0.3) # only detect collision with objects which have the property 'Object'. # see the documentation of `Passive Objects` for details: # http://www.openrobots.org/morse/doc/latest/user/others/passive_objects.html collision.properties(only_objects_with_property="Object") robot.append(collision) # for this example, we use the socket interface collision.add_interface("socket") # we also add a keyboard actuator to be able to move # around our robot to test collisions keyboard = Keyboard() robot.append(keyboard) # the 'sandbox' test environment offers plenty of objects to test # collisions. These objects have all the property 'Object' already set. env = Environment('sandbox') # Copy this code to a script, and run it with `morse run <script>`!
# This is a sample *client* code that uses pymorse to count the # collisions. Copy this code to a different script, start it with # `python3 <script>`, and move the robot in the simulator with the # arrow keys: when you collide with an object, it is printed on the # console. import pymorse nb_collisions = 0 def counter(data): global nb_collisions if data["collision"]: nb_collisions += 1 print("Collision with %s! In total, %d collisions occured" % (data["objects"], nb_collisions)) with pymorse.Morse() as morse: morse.robot.collision.subscribe(counter) print("Press ctrl+C to stop") while True: morse.sleep(10)