Sg2.9
Object collision test
Recent RSS
Object collision test
Date:  14. October 2010, 01:16

Hi,

I'm writing a tool that will produce circles every time I press the mousebutton. The circles shall continue to grow until some arbitrary condition is met OR collide with another circle/object.
Do I have to keep looping through every object in the document every time the circle changes size (which would seem very memory intense, not to mention slow)?
The best thing would be having a routine that checks wether the integrity of the current circle is broken or not.

How would I easiest go about that?

Thanks

Re: Object collision test
Date:  19. October 2010, 17:29

Anyone?
Jürg? Jonathan?

Re: Object collision test
Date:  1. November 2010, 22:49

Is there anyone who can help me?

I know you people are busy, but some feedback would be nice.
If not only to send me off in the right direction.

Thanks

Re: Object collision test
Date:  3. November 2010, 03:11

Hi Hakan,

I guess looping through all objects would be a way to do it. You could optimize this by dividing the document up into an imaginary grid and then only check for items within each column within the grid parts that intersect with the circle's bounds..

I made a quick example for you, but its quite untested.. I'm also unsure how much faster it is then simply checking each item for intersections..

tool.eventInterval = 5;

var grid = [];
var rows = 10;
var columns = 5;

var radius, path;

function onMouseDown(event) {
	var colSize = document.size / new Size(columns, rows);
	for(var i = 0; i < rows; i++) {
		for(var j = 0; j < columns; j++) {
			var location = new Point(j, i);
			var gridPart = {
				items: [],
				rectangle: new Rectangle(location * colSize, location * colSize + colSize)
			};
			grid.push(gridPart);
		}
	}
	var items = document.getItems({
		type: Item
	});
	for(var i = 0; i < items.length; i++) {
		var item = items[i];
		for(var j = 0; j < grid.length; j++) {
			var column = grid[j];
			if(column.rectangle.intersects(item.bounds)) {
				column.items.push(item);
			}
		}
	}
	radius = 5;
	hit = false;
	path = null;
}

var hit;
function onMouseDrag(event) {
	if(!hit && path) {
		for(var i = 0; i < grid.length; i++) {
			var column = grid[i];
			if(column.rectangle.intersects(path.bounds)) {
				var items = column.items;
				for(var j = 0; j < items.length; j++) {
					var item = items[j];
					if(item.bounds.intersects(path.bounds) && item.intersects(path)) {
						hit = true;
						j = items.length;
						i = grid.length;
					}
				}
			}
		}
	}
	
	if(!hit) {
		if(path)
			path.remove();
		radius += 2;
		path = new Path.Circle(event.downPoint, radius);
	}
}
Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Sketchy Structures 04.11.10