Basic Shapes
Description
Basic shapes are building blocks to modify and customize your environment. All shapes are spawned by default as static objects complete with collision boundaries which can be useful to create walls and other obstacles. Dynamics can be enabled and disabled to facilitate dynamic events that are triggered by actor locations such as the sudden appearance of an obstacle crossing the path. The physics properties can also be modified to tune the physics response depending on the number of interacting actors and what they are colliding with.
Basic shapes include full support for parenting to build long kinematic chains or simply to group multiple shapes together so they can be moved by manipulating a single parent actor.
If you are using a large number of dynamic shapes, consider using Widgets instead. They have both limitations and additional features, but widgets are more efficient when spawning large numbers of dynamic actors.
See the Basic Shapes Tutorial to get a better understanding of using basic shapes in Quanser Interactive Labs.
Library
Constants
- QLabsBasicShape.ID_BASIC_SHAPE = 200
Class ID
- QLabsBasicShape.SHAPE_CUBE = 0
See configurations
- QLabsBasicShape.SHAPE_CYLINDER = 1
See configurations
- QLabsBasicShape.SHAPE_SPHERE = 2
See configurations
- QLabsBasicShape.SHAPE_CONE = 3
See configurations
- QLabsBasicShape.COMBINE_AVERAGE = 0
- QLabsBasicShape.COMBINE_MIN = 1
- QLabsBasicShape.COMBINE_MULTIPLY = 2
- QLabsBasicShape.COMBINE_MAX = 3
Member Variables
- QLabsBasicShape.actorNumber = None
The current actor number of this class to be addressed. This will be set by spawn methods and cleared by destroy methods. It will not be modified by the destroy all actors. This can be manually altered at any time to use one object to address multiple actors.
Methods
- QLabsBasicShape.__init__(qlabs, verbose=False)[source]
Constructor Method
- Parameters
qlabs (object) – A QuanserInteractiveLabs object
verbose (boolean) – (Optional) Print error information to the console.
- QLabsBasicShape.spawn(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor with the next available actor number within this class.
- Parameters
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred. Note that if this is False, the returned actor number will be invalid.
- Returns
status - 0 if successful, 1 class not available, 3 unknown error, -1 communications error.
actorNumber - An actor number to use for future references.
- Return type
int32, int32
- QLabsBasicShape.spawn_degrees(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor with the next available actor number within this class.
- Parameters
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in degrees
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred. Note that if this is False, the returned actor number will be invalid.
- Returns
status - 0 if successful, 1 class not available, 3 unknown error, -1 communications error.
actorNumber - An actor number to use for future references.
- Return type
int32, int32
- QLabsBasicShape.spawn_id(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor.
- Parameters
actorNumber (uint32) – User defined unique identifier for the class actor in QLabs
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 class not available, 2 actor number not available or already in use, 3 unknown error, -1 communications error
- Return type
int32
- QLabsBasicShape.spawn_id_degrees(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, waitForConfirmation=True)
Spawns a new actor.
- Parameters
actorNumber (uint32) – User defined unique identifier for the class actor in QLabs
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 class not available, 2 actor number not available or already in use, 3 unknown error, -1 communications error
- Return type
int32
- QLabsBasicShape.spawn_id_and_parent_with_relative_transform(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Spawns a new actor relative to an existing actor and creates a kinematic relationship.
- Parameters
actorNumber (uint32) – User defined unique identifier for the class actor in QLabs
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 class not available, 2 actor number not available or already in use, 3 cannot find the parent actor, 4 unknown error, -1 communications error
- Return type
int32
- QLabsBasicShape.spawn_id_and_parent_with_relative_transform_degrees(actorNumber, location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], configuration=0, parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Spawns a new actor relative to an existing actor and creates a kinematic relationship.
- Parameters
actorNumber (uint32) – User defined unique identifier for the class actor in QLabs
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in degrees
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
configuration (uint32) – (Optional) Spawn configuration. See class library for configuration options.
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 class not available, 2 actor number not available or already in use, 3 cannot find the parent actor, 4 unknown error, -1 communications error
- Return type
int32
- QLabsBasicShape.set_material_properties(color, roughness=0.4, metallic=False, waitForConfirmation=True)[source]
Sets the visual surface properties of the shape.
- Parameters
color (float array[3]) – Red, Green, Blue components of the RGB color on a 0.0 to 1.0 scale.
roughness (float) – A value between 0.0 (completely smooth and reflective) to 1.0 (completely rough and diffuse). Note that reflections are rendered using screen space reflections. Only objects visible in the camera view will be rendered in the reflection of the object.
metallic (boolean) – Metallic (True) or non-metallic (False)
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the operation before proceeding. This makes the method a blocking operation.
- Returns
True if successful, False otherwise
- Return type
boolean
- QLabsBasicShape.get_material_properties()[source]
Gets the visual surface properties of the shape.
- Returns
status - True if successful or False otherwise
color - Red, Green, Blue components of the RGB color on a 0.0 to 1.0 scale.
roughness - A value between 0.0 (completely smooth and reflective) to 1.0 (completely rough and diffuse).
metallic - Metallic (True) or non-metallic (False)
- Return type
boolean, float array[3], float, boolean
- QLabsBasicShape.set_enable_dynamics(enableDynamics, waitForConfirmation=True)[source]
Sets the visual surface properties of the shape.
- Parameters
enableDynamics (boolean) – Enable (True) or disable (False) the shape dynamics. A dynamic actor can be pushed with other static or dynamic actors. A static actor will generate collisions, but will not be affected by interactions with other actors.
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the operation before proceeding. This makes the method a blocking operation.
- Returns
True if successful, False otherwise
- Return type
boolean
- QLabsBasicShape.set_enable_collisions(enableCollisions, waitForConfirmation=True)[source]
Enables and disables physics collisions. When disabled, other physics or velocity-based actors will be able to pass through.
- Parameters
enableCollisions (boolean) – Enable (True) or disable (False) the collision.
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the operation before proceeding. This makes the method a blocking operation.
- Returns
True if successful, False otherwise
- Return type
boolean
- QLabsBasicShape.set_physics_properties(enableDynamics, mass=1.0, linearDamping=0.01, angularDamping=0.0, staticFriction=0.0, dynamicFriction=0.7, frictionCombineMode=COMBINE_AVERAGE, restitution=0.3, restitutionCombineMode=COMBINE_AVERAGE, waitForConfirmation=True)[source]
Sets the dynamic properties of the shape.
- Parameters
enableDynamics (boolean) – Enable (True) or disable (False) the shape dynamics. A dynamic actor can be pushed with other static or dynamic actors. A static actor will generate collisions, but will not be affected by interactions with other actors.
mass (float) – (Optional) Sets the mass of the actor in kilograms.
linearDamping (float) – (Optional) Sets the damping of the actor for linear motions.
angularDamping (float) – (Optional) Sets the damping of the actor for angular motions.
staticFriction (float) – (Optional) Sets the coefficient of friction when the actor is at rest. A value of 0.0 is frictionless.
dynamicFriction (float) – (Optional) Sets the coefficient of friction when the actor is moving relative to the surface it is on. A value of 0.0 is frictionless.
frictionCombineMode (byte) – (Optional) Defines how the friction between two surfaces with different coefficients should be calculated (see COMBINE constants).
restitution (float) – (Optional) The coefficient of restitution defines how plastic or elastic a collision is. A value of 0.0 is plastic and will absorb all energy. A value of 1.0 is elastic and will bounce forever. A value greater than 1.0 will add energy with each collision.
restitutionCombineMode (byte) – (Optional) Defines how the restitution between two surfaces with different coefficients should be calculated (see COMBINE constants).
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the operation before proceeding. This makes the method a blocking operation.
- Returns
True if successful, False otherwise
- Return type
boolean
- QLabsBasicShape.set_transform(location, rotation, scale, waitForConfirmation=True)[source]
Sets the location, rotation in radians, and scale. If a shape is parented to another actor then the location, rotation, and scale are relative to the parent actor.
- Parameters
location (float array[3]) – An array of floats for x, y and z coordinates in full-scale units.
rotation (float array[3]) – An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – An array of floats for the scale in the x, y, and z directions.
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the operation before proceeding. This makes the method a blocking operation.
- Returns
True if successful or False otherwise
- Return type
boolean
- QLabsBasicShape.set_transform_degrees(location, rotation, scale, waitForConfirmation=True)[source]
Sets the location, rotation in degrees, and scale. If a shape is parented to another actor then the location, rotation, and scale are relative to the parent actor.
- Parameters
location (float array[3]) – An array of floats for x, y and z coordinates in full-scale units.
rotation (float array[3]) – An array of floats for the roll, pitch, and yaw in degrees
scale (float array[3]) – An array of floats for the scale in the x, y, and z directions.
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the operation before proceeding. This makes the method a blocking operation.
- Returns
True if successful or False otherwise
- Return type
boolean
- QLabsBasicShape.spawn_id_box_walls_from_end_points(actorNumber, startLocation, endLocation, height, thickness, color=[1, 1, 1], waitForConfirmation=True)[source]
Given a start and end point, this helper method calculates the position, rotation, and scale required to place a box on top of this line.
- Parameters
actorNumber (uint32) – User defined unique identifier for the class actor in QLabs
startLocation (float array[3]) – An array of floats for x, y and z coordinates.
endLocation (float array[3]) – An array of floats for x, y and z coordinates.
height (float) – The height of the wall.
thickness (float) – The width or thickness of the wall.
color (float array[3]) – Red, Green, Blue components of the RGB color on a 0.0 to 1.0 scale.
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the operation before proceeding. This makes the method a blocking operation.
- Returns
True if successful or False otherwise
- Return type
boolean
- QLabsBasicShape.spawn_id_box_walls_from_center(actorNumbers, centerLocation, yaw, xSize, ySize, zHeight, wallThickness, floorThickness=0, wallColor=[1, 1, 1], floorColor=[1, 1, 1], waitForConfirmation=True)[source]
Creates a container-like box with 4 walls and an optional floor.
- Parameters
actorNumbers (uint32 array[5]) – An array of 5 user defined unique identifiers for the class actors in QLabs.
centerLocation (float array[3]) – An array of floats for x, y and z coordinates.
yaw (float) – Rotation about the z axis in radians.
xSize (float) – Size of the box in the x direction.
ySize (float) – Size of the box in the y direction.
zSize (float) – Size of the box in the z direction.
wallThickness (float) – The thickness of the walls.
floorThickness (float) – (Optional) The thickness of the floor. Setting this to 0 will spawn a box without a floor.
wallColor (float array[3]) – (Optional) Red, Green, Blue components of the wall color on a 0.0 to 1.0 scale.
floorColor (float array[3]) – (Optional) Red, Green, Blue components of the floor color on a 0.0 to 1.0 scale.
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the operation before proceeding. This makes the method a blocking operation.
- Returns
True if successful or False otherwise
- Return type
boolean
- QLabsBasicShape.spawn_id_box_walls_from_center_degrees(actorNumbers, centerLocation, yaw, xSize, ySize, zHeight, wallThickness, floorThickness=0, wallColor=[1, 1, 1], floorColor=[1, 1, 1], waitForConfirmation=True)[source]
Creates a container-like box with 4 walls and an optional floor.
- Parameters
actorNumbers (uint32 array[5]) – An array of 5 user defined unique identifiers for the class actors in QLabs.
centerLocation (float array[3]) – An array of floats for x, y and z coordinates.
yaw (float) – Rotation about the z axis in degrees.
xSize (float) – Size of the box in the x direction.
ySize (float) – Size of the box in the y direction.
zSize (float) – Size of the box in the z direction.
wallThickness (float) – The thickness of the walls.
floorThickness (float) – (Optional) The thickness of the floor. Setting this to 0 will spawn a box without a floor.
wallColor (float array[3]) – (Optional) Red, Green, Blue components of the wall color on a 0.0 to 1.0 scale.
floorColor (float array[3]) – (Optional) Red, Green, Blue components of the floor color on a 0.0 to 1.0 scale.
waitForConfirmation (boolean) – (Optional) Wait for confirmation of the operation before proceeding. This makes the method a blocking operation.
- Returns
True if successful or False otherwise
- Return type
boolean
- QLabsBasicShape.destroy()
Find and destroy a specific actor. This is a blocking operation.
- Returns
numActorsDestroyed - The number of actors destroyed. -1 if failed.
- Return type
int32
- QLabsBasicShape.destroy_all_actors_of_class()
Find and destroy all actors of this class. This is a blocking operation.
- Returns
numActorsDestroyed - The number of actors destroyed. -1 if failed.
- Return type
int32
- QLabsBasicShape.get_world_transform()
Get the location, rotation, and scale in world coordinates of the actor.
- Returns
status - True if successful, False otherwise
location
rotation
scale
- Return type
boolean, float array[3], float array[3], float array[3]
- QLabsBasicShape.get_world_transform_degrees()
Get the location, rotation, and scale in world coordinates of the actor.
- Returns
status - True if successful, False otherwise
location
rotation
scale
- Return type
boolean, float array[3], float array[3], float array[3]
- QLabsBasicShape.ping()
Checks if the actor is still present in the environment. Note that if you did not spawn the actor with one of the spawn functions, you may need to manually set the actorNumber member variable.
- Returns
status - True if successful, False otherwise
- Return type
boolean
- QLabsBasicShape.parent_with_relative_transform(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Parents one existing actor to another to create a kinematic relationship.
- Parameters
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in radians
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, 2 cannot find the parent actor, 3 unknown error, -1 communications error
- Return type
int32
- QLabsBasicShape.parent_with_relative_transform_degrees(location=[0, 0, 0], rotation=[0, 0, 0], scale=[1, 1, 1], parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Parents one existing actor to another to create a kinematic relationship.
- Parameters
location (float array[3]) – (Optional) An array of floats for x, y and z coordinates
rotation (float array[3]) – (Optional) An array of floats for the roll, pitch, and yaw in degrees
scale (float array[3]) – (Optional) An array of floats for the scale in the x, y, and z directions. Scale values of 0.0 should not be used.
parentClassID (uint32) – (Optional) See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – (Optional) User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – (Optional) 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, 2 cannot find the parent actor, 3 unknown error, -1 communications error
- Return type
int32
- QLabsBasicShape.parent_with_current_world_transform(parentClassID=0, parentActorNumber=0, parentComponent=0, waitForConfirmation=True)
Parents one existing actor to another to create a kinematic relationship while preserving the current world transform of the child actor.
- Parameters
parentClassID (uint32) – See the ID variables in the respective library classes for the class identifier
parentActorNumber (uint32) – User defined unique identifier for the class actor in QLabs
parentComponent (uint32) – 0 for the origin of the parent actor, see the parent class for additional reference frame options
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, 2 cannot find the parent actor, 3 unknown error, -1 communications error
- Return type
int32
- QLabsBasicShape.parent_break(waitForConfirmation=True)
Breaks any relationship with a parent actor (if it exists) and preserves the current world transform
- Parameters
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - 0 if successful, 1 cannot find this actor, -1 communications error
- Return type
int32
- QLabsBasicShape.set_custom_properties(measuredMass=0, IDTag=0, properties='', waitForConfirmation=True)
Assigns custom properties to an actor.
- Parameters
measuredMass (float) – A float value for use with mass sensor instrumented actors. This does not alter the dynamic properties.
IDTag (uint32) – An integer value for use with IDTag sensor instrumented actors or for custom use.
properties (string) – A string for use with properties sensor instrumented actors. This can contain any string that is available for use to parse user-customized parameters.
waitForConfirmation (boolean) – (Optional) Make this operation blocking until confirmation of the spawn has occurred.
- Returns
status - True if successful, False otherwise
- Return type
boolean
- QLabsBasicShape.get_custom_properties()
Gets previously assigned custom properties to an actor.
- Returns
status - True if successful, False otherwise
measuredMass - float value
IDTag - integer value
properties - UTF-8 string
- Return type
boolean, float, int32, string
Configurations
There are four configurations (0-3) for the basic shape actor class.
0 - Cube
1 - Cylinder
2 - Sphere
3 - Cone
Connection Points
There are no connection points for this actor class.
Basic Shapes Tutorial
Python Tutorial
Raw to download this tutorial: Basic Shapes Tutorial (.py)
.
1"""
2Basic Shape Library Example
3---------------------------
4
5.. note::
6
7 Make sure you have Quanser Interactive Labs open before running this
8 example. This example is designed to best be run in QCar Cityscape.
9
10"""
11# imports to important libraries
12import sys
13import math
14import time
15
16from qvl.qlabs import QuanserInteractiveLabs
17from qvl.free_camera import QLabsFreeCamera
18from qvl.basic_shape import QLabsBasicShape
19from qvl.system import QLabsSystem
20
21def main():
22
23 # creates a server connection with Quanser Interactive Labs and manages
24 # the communications
25 qlabs = QuanserInteractiveLabs()
26
27 # initialize our desired variables
28 # note that you can use the coordinate helper to pick locations for your camera.
29 loc = [-17.801, 31.145, 1.783]
30 rot = [0, -0.93, 6.9]
31
32 # trying to connect to QLabs and open the instance we have created - program will end if this fails
33 print("Connecting to QLabs...")
34 if (not qlabs.open("localhost")):
35 print("Unable to connect to QLabs")
36 return
37
38 print("Connected")
39
40 # Use hSystem to set the tutorial title in the upper left of the qlabs window
41 hSystem = QLabsSystem(qlabs)
42 hSystem.set_title_string('Basic Shapes Tutorial')
43
44 # destroy any spawned actors in our QLabs that currently exist
45 qlabs.destroy_all_spawned_actors()
46 # create a camera in this qlabs instance
47 camera = QLabsFreeCamera(qlabs)
48 # add a custom camera at a specified location and rotation using degrees
49 camera.spawn_degrees(location=loc, rotation=rot)
50 # to switch our view from our current camera to the new camera we just initialized
51 camera.possess()
52
53 # initialize 4 cubes in our qlabs instance
54 cube0 = QLabsBasicShape(qlabs)
55 cube1 = QLabsBasicShape(qlabs)
56 cube2 = QLabsBasicShape(qlabs, True)
57 cube3 = QLabsBasicShape(qlabs, True)
58
59 # spawn one of the cubes using radians
60 cube0.spawn_id(actorNumber=0, location=[-10.202, 36.005, 0.5], rotation=[0,0,math.pi/4], scale=[0.5,0.5,0.5], configuration=cube0.SHAPE_CUBE, waitForConfirmation=True)
61 # ping this cube, expect True if cube does exist and the actorNumber hasn't been changed
62 cube0.ping()
63 # collecting the world transform coordinates of the cube
64 x, loc, rot, scale = cube0.get_world_transform()
65 print(x, loc, rot, scale)
66
67 # spawn a second cube using degrees
68 cube1.spawn_id_degrees(actorNumber=1, location=[-13.503, 33.677, 0.5], rotation=[0,0,45], scale=[0.5,0.5,0.5], configuration=cube1.SHAPE_CUBE, waitForConfirmation=True)
69 # wait to see visualization
70 time.sleep(1)
71 # destroy this created block
72 cube1.destroy()
73 # spawn a third and fourth cube relative to another parent actor already created in our qlabs instance using radians and then degrees respectively
74 cube2.spawn_id_and_parent_with_relative_transform(actorNumber=2, location=[0,2,0], rotation=[0,0,math.pi/4], scale=[1,1,1], configuration=cube2.SHAPE_CUBE, parentClassID=cube0.ID_BASIC_SHAPE, parentActorNumber=cube0.actorNumber, parentComponent=0, waitForConfirmation=True)
75 cube3.spawn_id_and_parent_with_relative_transform_degrees(actorNumber=3, location=[0,-2,0], rotation=[0,0,45], scale=[1,1,1], configuration=cube3.SHAPE_CUBE, parentClassID=cube0.ID_BASIC_SHAPE, parentActorNumber=cube0.actorNumber, parentComponent=0, waitForConfirmation=True)
76 # set the material properties to a metallic red and gold reflective surface
77 cube2.set_material_properties(color=[1,0,0], roughness=0.0, metallic=True, waitForConfirmation=True)
78 cube3.set_material_properties(color=[252/255,144/255,3/255], roughness=0.0, metallic=True, waitForConfirmation=True)
79 # have child actors rotate around the parent actor as their scale grows in size simultaneously
80 for y in range(51):
81 cube0.set_transform(location=[-10.202, 36.005, 0.5], rotation=[0,0,math.pi/4+2*math.pi/50*y], scale=[0.5+0.5*y/50,0.5+0.5*y/50,0.5+0.5*y/50])
82 cube2.set_transform(location=[0,2,0], rotation=[0,0,math.pi/4-math.pi/25*y], scale=[1,1,1])
83 cube3.set_transform_degrees(location=[0,-2,0], rotation=[0,0,45-180/25*y], scale=[1,1,1])
84
85 # initialize 6 spheres in our qlabs instance
86 sphere10 = QLabsBasicShape(qlabs)
87 sphere11 = QLabsBasicShape(qlabs)
88 sphere12 = QLabsBasicShape(qlabs)
89
90 sphere13 = QLabsBasicShape(qlabs)
91 sphere14 = QLabsBasicShape(qlabs)
92 sphere15 = QLabsBasicShape(qlabs)
93
94 # for the three first spheres, spawns spheres increasing in size using radians
95 sphere10.spawn_id(actorNumber=10, location=[-13.75, 32.5, 0.25], rotation=[0,0,0], scale=[0.5,0.5,0.5], configuration=sphere10.SHAPE_SPHERE, waitForConfirmation=True)
96 sphere11.spawn_id(actorNumber=11, location=[-13.75, 31.5, 1], rotation=[0,0,0], scale=[0.6,0.6,0.6], configuration=sphere11.SHAPE_SPHERE, waitForConfirmation=True)
97 sphere12.spawn_id(actorNumber=12, location=[-13.75, 30.5, 0.25], rotation=[0,0,0], scale=[0.7,0.7,0.7], configuration=sphere12.SHAPE_SPHERE, waitForConfirmation=True)
98
99 # in qlabs, the color of shapes uses the RGB color space with 0 to 255 represented between 0 and 1.
100 # if you know what color you'd like to set your shape in RGB simply divide the red, green and blue numbers by 255.
101 # this script sets these spheres to red, green and blue respectively while increasing in roughness
102 sphere10.set_material_properties(color=[1,0,0], roughness=0.0, metallic=False, waitForConfirmation=True)
103 sphere11.set_material_properties(color=[0,1,0], roughness=0.5, metallic=False, waitForConfirmation=True)
104 sphere12.set_material_properties(color=[0,0,1], roughness=1.0, metallic=False, waitForConfirmation=True)
105
106 # we want to now look at physics properties that are available to us in qlabs
107 # if we spawn three more spheres and set the properties of these spheres to
108 sphere13.spawn_id(actorNumber=13, location=[-11.253, 28.614, 1], rotation=[0,0,0], scale=[0.6,0.6,0.6], configuration=sphere13.SHAPE_SPHERE, waitForConfirmation=True)
109 sphere14.spawn_id(actorNumber=14, location=[-8.669, 26.631, 1], rotation=[0,0,0], scale=[0.6,0.6,0.6], configuration=sphere14.SHAPE_SPHERE, waitForConfirmation=True)
110 sphere15.spawn_id(actorNumber=15, location=[-8.685, 25.751, 1], rotation=[0,0,0], scale=[0.6,0.6,0.6], configuration=sphere13.SHAPE_SPHERE, waitForConfirmation=True)
111 sphere13.set_physics_properties(mass=10, linearDamping=0, angularDamping=0, enableDynamics=True, waitForConfirmation=True)
112 sphere13.set_enable_collisions(enableCollisions=True, waitForConfirmation=True)
113 sphere15.set_physics_properties(mass=0.5, linearDamping=0, angularDamping=0, enableDynamics=True, waitForConfirmation=True)
114 sphere15.set_enable_collisions(enableCollisions=True, waitForConfirmation=True)
115
116 sphere10.set_enable_dynamics(enableDynamics=True, waitForConfirmation=True)
117 sphere11.set_enable_dynamics(enableDynamics=True, waitForConfirmation=True)
118 sphere12.set_enable_dynamics(enableDynamics=True, waitForConfirmation=True)
119 sphere13.set_enable_dynamics(enableDynamics=True, waitForConfirmation=True)
120
121
122
123 boxSpawn = QLabsBasicShape(qlabs)
124 boxSpawn.spawn_id_box_walls_from_center(actorNumbers=[210, 211, 212, 213, 214], centerLocation=[-9.35, 26.5, 0.005], yaw=math.pi/4, xSize=2, ySize=2, zHeight=0.5, wallThickness=0.1, floorThickness=0.1, wallColor=[1,0,0], floorColor=[0,0,0], waitForConfirmation=True)
125
126
127 boxSpawn.spawn_id_box_walls_from_center_degrees(actorNumbers=[270, 271, 272, 273, 274], centerLocation=[-11.35, 28.5, 0.005], yaw=45, xSize=2, ySize=2, zHeight=0.5, wallThickness=0.1, floorThickness=0.1, wallColor=[1,0,0], floorColor=[0,0,0], waitForConfirmation=True)
128
129
130 boxSpawn.spawn_id_box_walls_from_end_points(actorNumber=280, startLocation=[-10.5, 32.5, 0.005], endLocation=[-10.5, 30.5, 0.005], height=0.1, thickness=0.1, color=[0.2,0.2,0.2], waitForConfirmation=True)
131
132
133 x, shapeHandle1 = boxSpawn.spawn(location=[-6.945, 31.5, 0.5], rotation=[0,0,math.pi/4], scale=[1,1,1], configuration=boxSpawn.SHAPE_CUBE, waitForConfirmation=True)
134 x, shapeHandle2 = boxSpawn.spawn(location=[-6.945, 31.5, 1.375], rotation=[0,0,0], scale=[0.75,0.75,0.75], configuration=boxSpawn.SHAPE_CUBE, waitForConfirmation=True)
135 x, shapeHandle3 = boxSpawn.spawn(location=[-6.945, 31.5, 2], rotation=[0,0,math.pi/4], scale=[0.5,0.5,0.5], configuration=boxSpawn.SHAPE_CUBE, waitForConfirmation=True)
136
137
138 x, shapeHandle4 = boxSpawn.spawn_degrees(location=[-6.945, 31.5, 2.50], rotation=[0,0,0], scale=[0.5,0.5,0.5], configuration=boxSpawn.SHAPE_CUBE, waitForConfirmation=True)
139 x, shapeHandle5 = boxSpawn.spawn_degrees(location=[-6.945, 31.5, 2.875], rotation=[0,0,45], scale=[0.25,0.25,0.25], configuration=boxSpawn.SHAPE_CUBE, waitForConfirmation=True)
140
141
142 boxSpawn.actorNumber = shapeHandle1
143 boxSpawn.set_material_properties(color=[0,0,0], roughness=0.0, metallic=False, waitForConfirmation=True)
144 boxSpawn.actorNumber = shapeHandle2
145 boxSpawn.set_material_properties(color=[1,1,1], roughness=0.0, metallic=False, waitForConfirmation=True)
146 boxSpawn.actorNumber = shapeHandle3
147 boxSpawn.set_material_properties(color=[0.5,0.5,0.5], roughness=0.0, metallic=False, waitForConfirmation=True)
148 boxSpawn.actorNumber = shapeHandle4
149 boxSpawn.set_material_properties(color=[0,0,0], roughness=0.0, metallic=False, waitForConfirmation=True)
150
151 # Close qlabs
152 qlabs.close()
153 print('Done !')
154
155if __name__ == "__main__":
156 main()
Matlab Tutorial
Raw to download this tutorial: Basic Shapes Tutorial (.m)
.
1% Basic Shape Library Example
2% -------------------------
3%
4% .. note::
5%
6% Make sure you have Quanser Interactive Labs open before running this
7% example. This example is designed to best be run in QCar Cityscape.
8
9close all;
10clear all;
11clc;
12
13% --------------------------------------------------------------
14% Setting MATLAB Path for the libraries
15% Always keep at the start, it will make sure it finds the correct references
16newPathEntry = fullfile(getenv('QAL_DIR'), 'libraries', 'matlab', 'qvl');
17pathCell = regexp(path, pathsep, 'split');
18if ispc % Windows is not case-sensitive
19 onPath = any(strcmpi(newPathEntry, pathCell));
20else
21 onPath = any(strcmp(newPathEntry, pathCell));
22end
23
24if onPath == 0
25 path(path, newPathEntry)
26 savepath
27end
28% --------------------------------------------------------------
29
30fprintf('\n\n----------------- Communications -------------------\n\n');
31
32qlabs = QuanserInteractiveLabs();
33connection_established = qlabs.open('localhost');
34
35if connection_established == false
36 disp("Failed to open connection.")
37 return
38end
39
40
41disp('Connected')
42
43num_destroyed = qlabs.destroy_all_spawned_actors();
44
45fprintf('%d actors destroyed', num_destroyed);
46
47% Use hSystem to set the tutorial title on the qlabs display screen
48hSystem = QLabsSystem(qlabs);
49hSystem.set_title_string('Basic Shapes Tutorial')
50
51% initialize our desired variables
52% note that you can use the coordinate helper to pick locations for your camera.
53loc = [-17.801, 31.145, 1.783];
54rot = [0, -0.93, 6.9];
55
56% create a camera in this qlabs instance
57camera = QLabsFreeCamera(qlabs);
58% add a custom camera at a specified location and rotation using degrees
59camera.spawn_degrees(loc, rot);
60% to switch our view from our current camera to the new camera we just initialized
61camera.possess();
62
63% initialize 4 cubes in our qlabs instance
64cube0 = QLabsBasicShape(qlabs);
65cube1 = QLabsBasicShape(qlabs);
66cube2 = QLabsBasicShape(qlabs, 1);
67cube3 = QLabsBasicShape(qlabs, 1);
68
69% spawn one of the cubes using radians
70cube0.spawn_id(0, [-10.202, 36.005, 0.5], [0, 0, pi/4], [0.5, 0.5, 0.5], cube0.SHAPE_CUBE, 1);
71
72% ping this cube, expect True if cube does exist and the actorNumber hasn't been changed
73cube0.ping();
74
75% collecting the world transform coordinates of the cube
76[success, loc, rot, scale] = cube0.get_world_transform();
77
78% spawn a second cube using degrees
79cube1.spawn_id_degrees(1, [-13.503, 33.677, 0.5], [0, 0, 45], [0.5, 0.5, 0.5], cube1.SHAPE_CUBE, 1);
80
81% wait to see visualization
82pause(1);
83
84% destroy this created block
85cube1.destroy();
86
87% spawn a third and fourth cube relative to another parent actor already created in our qlabs instance using radians and then degrees respectively
88cube2.spawn_id_and_parent_with_relative_transform(2, [0, 2, 0], [0, 0, pi/4], [1, 1, 1], cube2.SHAPE_CUBE, cube0.ID_BASIC_SHAPE, cube0.actorNumber, 0, 1);
89cube3.spawn_id_and_parent_with_relative_transform_degrees(3, [0, -2, 0], [0, 0, 45], [1, 1, 1], cube3.SHAPE_CUBE, cube0.ID_BASIC_SHAPE, cube0.actorNumber, 0, 1);
90
91% set the material properties to a metallic red and gold reflective surface
92cube2.set_material_properties([1, 0, 0], 0.0, 1, 1);
93cube3.set_material_properties([252/255, 144/255, 3/255], 0.0, 1, 1);
94
95% have child actors rotate around the parent actor as their scale grows in size simultaneously
96for y = 0:50
97 cube0.set_transform([-10.202, 36.005, 0.5], [0, 0, pi/4 + 2*pi/50*y], [0.5+0.5*y/50, 0.5+0.5*y/50, 0.5+0.5*y/50]);
98 cube2.set_transform([0, 2, 0], [0, 0, pi/4 - pi/25*y], [1, 1, 1]);
99 cube3.set_transform_degrees([0, -2, 0], [0, 0, 45 - 180/25*y], [1, 1, 1]);
100end
101
102% initialize 6 spheres in our qlabs instance
103sphere10 = QLabsBasicShape(qlabs);
104sphere11 = QLabsBasicShape(qlabs);
105sphere12 = QLabsBasicShape(qlabs);
106
107sphere13 = QLabsBasicShape(qlabs);
108sphere14 = QLabsBasicShape(qlabs);
109sphere15 = QLabsBasicShape(qlabs);
110
111% for the three first spheres, spawns spheres increasing in size using radians
112sphere10.spawn_id(10, [-13.75, 32.5, 0.25], [0, 0, 0], [0.5, 0.5, 0.5], sphere10.SHAPE_SPHERE, 1);
113sphere11.spawn_id(11, [-13.75, 31.5, 1], [0, 0, 0], [0.6, 0.6, 0.6], sphere11.SHAPE_SPHERE, 1);
114sphere12.spawn_id(12, [-13.75, 30.5, 0.25], [0, 0, 0], [0.7, 0.7, 0.7], sphere12.SHAPE_SPHERE, 1);
115
116% in qlabs, the color of shapes uses the RGB color space with 0 to 255 represented between 0 and 1.
117% if you know what color you'd like to set your shape in RGB simply divide the red, green and blue numbers by 255.
118% this script sets these spheres to red, green and blue respectively while increasing in roughness
119sphere10.set_material_properties([1, 0, 0], 0.0, 0, 1);
120sphere11.set_material_properties([0, 1, 0], 0.5, 0, 1);
121sphere12.set_material_properties([0, 0, 1], 1.0, 0, 1);
122
123% we want to now look at physics properties that are available to us in qlabs
124% if we spawn three more spheres and set the properties of these spheres to
125sphere13.spawn_id(13, [-11.253, 28.614, 1], [0, 0, 0], [0.6, 0.6, 0.6], sphere13.SHAPE_SPHERE, 1);
126sphere14.spawn_id(14, [-8.669, 26.631, 1], [0, 0, 0], [0.6, 0.6, 0.6], sphere14.SHAPE_SPHERE, 1);
127sphere15.spawn_id(15, [-8.685, 25.751, 1], [0, 0, 0], [0.6, 0.6, 0.6], sphere13.SHAPE_SPHERE, 1);
128
129sphere13.set_physics_properties(1, 10, 0.0 , 0.0, 0.0, 0.7, sphere13.COMBINE_AVERAGE, 0.3, sphere13.COMBINE_AVERAGE, 1);
130sphere13.set_enable_collisions(1, 1);
131sphere15.set_physics_properties(1, 0.5, 0.0, 0.0, 0.0, 0.7, sphere15.COMBINE_AVERAGE, 0.3, sphere15.COMBINE_AVERAGE, 1);
132sphere15.set_enable_collisions(1, 1);
133
134sphere10.set_enable_dynamics(1, 1);
135sphere11.set_enable_dynamics(1, 1);
136sphere12.set_enable_dynamics(1, 1);
137sphere13.set_enable_dynamics(1, 1);
138
139boxSpawn = QLabsBasicShape(qlabs);
140boxSpawn.spawn_id_box_walls_from_center([210, 211, 212, 213, 214], [-9.35, 26.5, 0.005], pi/4, 2, 2, 0.5, 0.1, 0.1, [1, 0, 0], [0, 0, 0], 1);
141
142boxSpawn.spawn_id_box_walls_from_center_degrees([270, 271, 272, 273, 274], [-11.35, 28.5, 0.005], 45, 2, 2, 0.5, 0.1, 0.1, [1, 0, 0], [0, 0, 0], 1);
143
144
145boxSpawn.spawn_id_box_walls_from_end_points(280, [-10.5, 32.5, 0.005], [-10.5, 30.5, 0.005], 0.1, 0.1, [0.2, 0.2, 0.2], 1);
146
147
148[x, shapeHandle1] = boxSpawn.spawn([-6.945, 31.5, 0.5], [0, 0, pi/4], [1, 1, 1], boxSpawn.SHAPE_CUBE, 1);
149[x, shapeHandle2] = boxSpawn.spawn([-6.945, 31.5, 1.375], [0, 0, 0], [0.75, 0.75, 0.75], boxSpawn.SHAPE_CUBE, 1);
150[x, shapeHandle3] = boxSpawn.spawn([-6.945, 31.5, 2], [0, 0, pi/4], [0.5, 0.5, 0.5], boxSpawn.SHAPE_CUBE, 1);
151
152
153[x, shapeHandle4] = boxSpawn.spawn_degrees([-6.945, 31.5, 2.50], [0, 0, 0], [0.5, 0.5, 0.5], boxSpawn.SHAPE_CUBE, 1);
154[x, shapeHandle5] = boxSpawn.spawn_degrees([-6.945, 31.5, 2.875], [0, 0, 45], [0.25, 0.25, 0.25], boxSpawn.SHAPE_CUBE, 1);
155
156
157boxSpawn.actorNumber = shapeHandle1;
158boxSpawn.set_material_properties([0, 0, 0], 0.0, 0, 1);
159boxSpawn.actorNumber = shapeHandle2;
160boxSpawn.set_material_properties([1, 1, 1], 0.0, 0, 1);
161boxSpawn.actorNumber = shapeHandle3;
162boxSpawn.set_material_properties([0.5, 0.5, 0.5], 0.0, 0, 1);
163boxSpawn.actorNumber = shapeHandle4;
164boxSpawn.set_material_properties([0, 0, 0], 0.0, 0, 1);
165
166% close qlabs
167qlabs.close();
168disp('Done !')