Python 3D Graphics Tutorial 2: Animating 3D Objects in Vpython

Python 3D Graphics Tutorial 2: Animating 3D Objects in Vpython

Introduction to 3D Graphics with Python

Overview of the Lesson

  • Paul McQuarter introduces lesson number two in a tutorial series on 3D graphics and animations using Python.
  • He emphasizes the importance of having a strong cup of black coffee, referring to it as "go juice" for enhancing programming focus.

Homework Review

  • Paul asks viewers if they completed the homework from lesson one, encouraging them to comment on their success or struggles.
  • He stresses that attempting homework is crucial for learning, as merely watching does not equate to understanding or being able to code independently.

Setting Up the Environment

Preparing for Coding

  • Viewers are instructed to open Visual Studio Code and are thanked for supporting Paul on Patreon.
  • A link to his Patreon account is provided for those who wish to support his content creation efforts.

Creating a Room Simulation

Starting the Project

  • Paul begins by creating a new file named room_simulation.py in the VPython folder.
  • He imports necessary libraries from VPython and time, preparing for coding the room simulation.

Defining Room Dimensions

  • The assignment involves creating a room with walls that are 10 feet high and wide, including a back wall, ceiling, floor, right wall, left wall, and placing a red marble at the center.

Building Room Elements

Constructing the Floor

  • Paul starts coding by defining the floor as a box with specific dimensions: color white and positioned correctly in 3D space.
  • He explains how dimensions work in 3D (length in x-direction, width in z-direction, height in y-direction).

Adjusting Dimensions Correctly

  • The length is set at 10 units along x-axis; width at 10 units along z-axis; height is adjusted accordingly.
  • Emphasizes remembering spatial orientation while coding in three dimensions.

Running Initial Code

Testing Floor Creation

  • A loop (while true) is implemented to keep running without crashing.
  • After running the initial code successfully, he confirms that dimensions were set correctly based on visual output.

Creating Additional Elements

Adding Ceiling Element

  • Paul copies the floor code to create a ceiling but adjusts its position upwards (+5 instead of -5).

Creating a 3D Room in Visual Studio

Building the Walls

  • The speaker discusses creating a ceiling and floor for a 3D room, expressing ease with the process.
  • The back wall is defined to be positioned at coordinates (0, 0, -5), utilizing the right-hand rule to determine positive and negative directions in 3D space.
  • A left wall is introduced, set at (-5, 0, z), indicating its position along the x-axis.
  • Adjustments are made to ensure proper dimensions: length of 10 units and width of 0.1 units for the back wall while maintaining height at 10 units.
  • Clarification on dimensions: length corresponds to x direction (10), width corresponds to z direction (0.1), and height corresponds to y direction (10).

Finalizing Wall Dimensions

  • The speaker emphasizes understanding which axis represents length, width, and height when adjusting wall sizes.
  • Corrections are made to ensure that the left wall's dimensions match intended specifications: length should be 0.1 units while width remains at 10 units.
  • The right wall is created by copying parameters from the left wall but adjusted for position at (+5).
  • Confirmation that all walls are now correctly sized; preparation for adding additional elements like a marble.

Adding Objects to the Room

  • Introduction of a marble object as a sphere placed in the center of the room with specified radius and color attributes.
  • The marble's properties include being centered at coordinates (0, 0, 0), with a radius of 0.75 and colored red.

Animating Objects

  • Discussion shifts towards animating the marble within the room; initial thoughts on how movement can be implemented effectively.
  • Plans outlined for moving the marble back and forth using delta values; starting position set at zero for animation purposes.

Implementing Animation Logic

  • Explanation of using a loop structure where each iteration updates marble's position based on delta x value set at 0.1 units per frame.
  • Emphasis on controlling animation speed through Python commands rather than delays; introduction of 'rate' function set to manage frame rate during animation execution.

Understanding Marble Movement in Python

Implementing Movement Logic

  • The movement of the marble is controlled by a loop that adjusts its position based on a variable delta x, which can be modified to change the speed of movement.
  • The marble's position is updated using a vector, where the x-coordinate is incremented by delta x, while y and z coordinates remain unchanged.
  • An initial issue arises when the marble does not move due to an error in referencing x position, highlighting the importance of accurate variable names in coding.

Handling Boundary Conditions

  • Upon running the code, unexpected behavior occurs as the marble escapes its defined boundaries, indicating a need for boundary checks.
  • To prevent this escape, conditions are introduced: if x position exceeds 5 or falls below -5, delta x is reversed to change direction upon hitting walls.

Refining Collision Detection

  • Observations reveal that when bouncing off walls, the marble appears to penetrate them before reversing direction; thus, adjustments are necessary for more accurate collision detection.
  • A dual condition check ensures that both positive and negative boundaries are respected, allowing for proper reversal of direction at both ends.

Parameterizing Model Dimensions

  • The current model struggles with confusion over dimensions (length vs. width), necessitating a shift towards parameterization for clarity in defining object sizes.
  • Transitioning from fixed constants to parameters will simplify understanding how different dimensions apply across various objects within the simulation.

Implementing Size Command

  • Introducing a uniform size command (size = vector(x,y,z)), helps eliminate ambiguity regarding dimensions and maintains consistency across different objects.
  • By setting specific values for size parameters (e.g., floor dimensions), it becomes easier to manage object properties without confusion over their meanings.

This structured approach provides insights into programming concepts related to movement logic and collision detection within simulations using Python.

Moving Towards a Parameterized Model

Homework Assignment

  • The instructor assigns homework to replace all instances of length, width, and height with a size vector in the code. This is aimed at simplifying the model for better functionality.

Upcoming Lesson Focus

  • In the next lesson, the focus will shift to fully parameterizing the model. This change aims to reduce confusion regarding dimensions by using parameters instead of specific measurements.

Simplification Strategy

  • The goal is to ensure that any changes made to parameters will consistently yield correct results, enhancing usability and flexibility in coding.

Progress Acknowledgment

  • The instructor acknowledges significant progress over two lessons, highlighting that students now have an animated scene as part of their learning experience.

Engagement Encouragement

  • Students are encouraged to engage with the content by liking videos, subscribing for updates, and sharing knowledge about coding with others.
Video description

You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming: https://www.patreon.com/PaulMcWhorter In this video we show step-by-step instructions on how to animate three dimensional objects in vpython, (visual python), and how to bring your models to life. I do not assume you are an expert, so these lessons are designed for complete beginners. #Python #Lessons #Programming