GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2018-02-09 07:51:19

gnysek
Member
Registered: 2011-12-29
Posts: 36

path_get_smooth_points

If path is smooth, path_get_point_x() still returns corners for straight one.

This script tries to find nearest points on smoothed path, and returns their percentage positions as array.

Precision is a param which says how long distance in pixels between checks should be - so this function is rather time-consuming (for path shown in image below, it have around 1800 checks in case of prevision = 1, and 180 when precision is 10). It's best to use it only in create event, and assign result to a value which will be later use.

Expand	/// @param pathid
	/// @param precision_pixels
	
	var _is_smooth = path_get_kind(argument0);
	var _corners = [];
	var _precision = 1/path_get_length(argument0) * clamp(argument1, 1, 10);
	
	// fix issue where if path is closed, it starts to find point 0 in half distance between points 0-1
	var _is_closed = path_get_closed(argument0);
	var _pos = 0;
	if (_is_closed) {
		var _l1 = path_get_length(argument0);
		path_set_closed(argument0, 0);
		var _l2 = path_get_length(argument0);
		path_set_closed(argument0, 1);
		var _l = _l1 - _l2;
		var _pos = - ( _l / _l1) + _precision;
	}
        // end of fix
	
	var _pts = path_get_number(argument0);
	for(var i = 0; i < _pts; i++) {
		var xx = path_get_point_x(argument0, i);
		var yy = path_get_point_y(argument0, i);
			
		var dst = 99999999;
			
		while (_pos < 1) {
			var xxx = path_get_x(argument0, (_pos + 1) % 1);
			var yyy = path_get_y(argument0, (_pos + 1) % 1);
				
			var _dst = point_distance(xx, yy, xxx, yyy);
				
			if (_dst < dst) {
				dst = _dst;
			}
				
			if (dst < 1.1 or _dst > dst) {
				_corners[ array_length_1d(_corners) ] = (_pos + 1) % 1;
				_pos += _precision;
				break;
			}
				
			_pos += _precision;
		}
	}
		
	return _corners;

On image below:
- green path is path when straight
- green points are positions returned by path_get_point_x/path_get_point_y BOTH when path is straight/smooth
- red is path when smooth
- yellow are positions returned by above functions for smooth path. See that they are really precise smile You need to use path_get_x(path_name, point[num]) to get x/y coordinates.

DVl0EZwWsAAXs0B.jpg

This script also solves issue where path_get_point_x/y(path, 0) returns middle position between points 0-1 on path when smooth. It moves backwards by half distance between path start/end and starts to search from there in this case.

Last edited by gnysek (2018-02-09 12:16:21)

Offline

Board footer

Powered by FluxBB