Compound objects in VPython

VPython (Visual Python) is a 3D graphics library that allows us to create and visualize three-dimensional objects on the screen. It is primarily used to visualize the impact of physics equations on the objects' motion.

Note: Read more about VPython.

Compound objects

A compound object contains multiple basic objects grouped to create a more complex and cohesive structure. Compound objects are useful for creating objects with hierarchical structures or simplifying the manipulation of interconnected objects.

Syntax

The syntax for making a compound object is as follows:

compound_object = compound([obj1, obj2, obj3, ...])

where obj1, obj2, and obj3 are the basic VPython objects. We can keep on adding more objects to make more complex models.

Note: Once you have created a compound object, you can treat it as a single entity and manipulate it.

Example code

The provided sample creates a house using different objects. It uses a pyramid as a roof, boxes as windows, and a cuboid as the body of the house. To execute the provided code, you have to follow below mentioned steps:

Once you click the "Run" button, follow the encircled link.
1 of 4

The sample code is as follows:

from vpython import *

# Create basic objects for the house
house_base = box(pos=vector(0, -2, 0), size=vector(6, 4, 6), color=color.green)  # Base of the house
roof = pyramid(pos=vector(0, 0, 0), size=vector(3, 8, 6), color=color.red)  # Roof of the house
roof.rotate(angle=pi/2, axis=vector(0, 0, 1)) # Rotating the roof of the house
door = box(pos=vector(0, -2, 3), size=vector(1.5, 3, 0.2), color=color.orange)  # Door of the house
window1 = box(pos=vector(-2, -2, 3), size=vector(1.5, 1.5, 0.2), color=color.blue)  # Window 1
window2 = box(pos=vector(2, -2, 3), size=vector(1.5, 1.5, 0.2), color=color.blue)  # Window 2

# Combine basic objects into a compound object (house)
house = compound([house_base, roof, door, window1, window2])

# Add a floor for better visualization
floor = box(pos=vector(0, -3.5, 0), size=vector(20, 0.2, 20), color=color.gray(0.8))

# Run the animation loop
while True:
    rate(30)

Code explanation

  • Line 1: Importing the VPython library.

  • Line 4: The box object creates the body of the house will be constructed.

  • Line 5–6: The pyramid object creates the house's roof in a three-dimensional triangular shape. The object is stored in the roof variable. The roof is then rotated counter-clockwise by π2rad\frac{\pi}{2} rad to beautify the house.

  • Lines 7–9: The box object creates the door and two windows of the house and stores them in the door, window1, and window2 object.

  • Line 12: The .compound() method is used to group the house's body, roof, door, and windows into a single entity. The final product is stored in the house variable.

  • Line 15: A box object is used again to create a flat ground on which the house will be visualized.

Note: Continue learning about

Copyright ©2024 Educative, Inc. All rights reserved