How to randomly position two videos without overlap?

Christien Ayers's icon

Is it possible to to use jit.gl.pix or another object to randomly position two videos (using two jit.movie objects) without having them overlap?

I'm struggling to figure out how to do so. I'm thinking maybe I need to use codebox...? Regardless, I don't know how to set things up such that I can track the position of a video, as opposed to the position of each pixel (before cropping, zooming, etc in jit.gl.pix).

Rob Ramirez's icon

here's one way of potentially many to skin this one

Max Patch
Copy patch and select New From Clipboard in Max.

Christien Ayers's icon

Thank you so much, Rob, but I forgot to mention... I'm on Max 8.5.6😭

Rob Ramirez's icon

the only thing that's not max 8 compatible is the v8 codebox. here's that source, you'll have to convert it to ES5 JS

outlets = 2;
const count = 6;
const margin = 0.3;
let positions = [];

function isTooClose(x, y) {
    for (const [px, py] of positions) {
        const dx = x - px;
        const dy = y - py;
        if (Math.abs(dx) < margin && Math.abs(dy) < margin) {
            return true;
        }
    }
    return false;
}
function bang() {
	let attempts = 0;
	const maxattempts = 1000;
	positions = [];
	while (positions.length < count && attempts < maxattempts) {
	    const x = Math.random();
	    const y = Math.random();
	    if (!isTooClose(x, y)) {
			positions.push([x, y]);
	    }
	    attempts++;
	}
	
	outlet(0, "dim", positions.length);
	outlet(1, "dim", positions.length);
	for (let i = 0; i < positions.length; i++) {
		outlet(0, "setcell1d", i, positions[i][0], positions[i][1]);
		outlet(1, "setcell1d", i, i);
	}
	outlet(0, "bang");
	outlet(1, "bang");
}
bang();
Christien Ayers's icon

Appreciate it... this route makes a lot of sense! I was working on using jit.gl.pix to track the coordinates of the corners of each video, then compare them in order to generate non-overlapping positions. I'll try both ways and see which I like better.