Boundary Detection and Position Limiting – Code in Description – Another TouchDesigner Tutorial

The Python code used in this tutorial is located below the video timestamps in this description.

In this video, we’ll continue to look at basic video game functionality within TouchDesigner by creating a boundary detection and position limiting system for simple 2D geometry. We’ll use the system to restrict the movement of a red square based on the color values of a background image. The end result will be somewhat reminiscent of the Atari 2600 game “Adventure”.

We will be using the Speed CHOPs for positioning the red square, and the TOP to CHOP to retrieve RGB color information at the edges of the square. We’ll send this color information into a CHOP Execute DAT, which will use Python code to set position limits within the Speed CHOPs.

As I mention in the video, this method of boundary detection has its limitations – you will run into occasional glitches. However, I think the technique is a good illustration of what can be accomplished with CHOPs and simple Python code.

——————

00:00 Intro / Network Overview
03:38 Building the TOP Network – Rectangle TOP
05:03 Noise TOP for Background
07:30 Building the CHOP Network – Positioning System
09:02 Y Positioning Branch
11:28 X Positioning Branch
13:27 Applying Position Values
16:14 Boundary Detection Intro
17:05 TOP to CHOP, UV Coordinates
22:45 Top Boundary Detection
26:32 Bottom, Left and Right Boundary Detection
30:42 Merging CHOPs
31:37 CHOP Execute DAT
32:31 Python Code: Setting + Resetting Boundaries in All Directions
48:57 Closing Comments + Outro

——————

Code used within CHOP Execute DAT (make sure you add the tab spacing from the video):

if val == 1:
if channel.name == ‘t’:
op(‘speed1’).par.max = op(‘posNull’)[‘yPos’]

elif channel.name == ‘b’:
op(‘speed1’).par.min = op(‘posNull’)[‘yPos’]

elif channel.name == ‘r’:
op(‘speed2’).par.max = op(‘posNull’)[‘xPos’]

elif channel.name == ‘l’:
op(‘speed2’).par.min = op(‘posNull’)[‘xPos’]

else:
op(‘speed1’).par.max = 0.5
op(‘speed1’).par.min = -0.5
op(‘speed2’).par.max = 0.5
op(‘speed2’).par.min = -0.5

——————

If you have any questions as you implement this technique, feel free to reach out.

——————

This is the second video in a new series called “Another TouchDesigner Tutorial”. Check back soon for new content.

IG: @jackdilaura
https://www.instagram.com/jackdilaura/

About The Author

You might be interested in