- Home /
Need help generating points around an object.
Hello, recently i have been trying to create a script that generates points around an object in 3D on the X and Z planes depending on its size. The changing sizes of the objects will always be integers so no floating points.
I cannot for the life of me figure out how to generate these points around my object, and i was wondering if anyone could help me with my problem. I have attached an image where the points(blue x's) should be with the black boxes representing the object.
Answer by RaptorRush · Aug 12, 2020 at 01:24 AM
Simple shapes: (square or rectangle)
using System;
using UnityEngine;
public class Example : MonoBehaviour {
private const int xAxisI = 0, zAxisI = 2;
private static readonly Tuple<int, int>[] axisIOrders = { new Tuple<int, int>(xAxisI, zAxisI), new Tuple<int, int>(zAxisI, xAxisI) };
private Vector3Int[] GetSimpleShapePoints(Vector3 centerPos, Vector2Int size) {
Vector2Int arrayLength = size * 2;
Vector3Int[] points = new Vector3Int[arrayLength.x + arrayLength.y];
Vector3Int origin = new Vector3Int(GetOriginAxis(centerPos.x, size.x), Mathf.RoundToInt(centerPos.y), GetOriginAxis(centerPos.z, size.y));
for (int axisI = 0, pointI = 0; axisI < 2; axisI++) {
Tuple<int, int> axisIOrder = axisIOrders[axisI];
for (int v1 = 1, axisLength = size[TranslateAxisI(axisIOrder.Item1)]; v1 <= axisLength; v1++) {
int axisMax = size[TranslateAxisI(axisIOrder.Item2)] + 1;
for (int v2 = 0, i = 0; i < 2; v2 = axisMax, i++) {
Vector3Int offset = new Vector3Int();
offset[axisIOrder.Item1] = v1;
offset[axisIOrder.Item2] = v2;
points[pointI++] = origin + offset;
}
}
} return points;
}
private int GetOriginAxis(float centerPosInAxis, int sizeInAxis) => Mathf.RoundToInt(centerPosInAxis - (((float)sizeInAxis / 2) - .5f)) - 1;
private int TranslateAxisI(int axisI) => Mathf.Min(axisI, 1);
}
Complex shapes:
using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour {
private static readonly Vector3Int[] dirs = { new Vector3Int(0, 0, 1), new Vector3Int(0, 0, -1), Vector3Int.right, Vector3Int.left };
private Vector3Int[] GetComplexShapePoints(Vector3Int[] cellsPos) {
HashSet<Vector3Int> occupiedCells = new HashSet<Vector3Int>(cellsPos), points = new HashSet<Vector3Int>();
for (int cellI = 0; cellI < cellsPos.Length; cellI++) {
Vector3Int cellPos = cellsPos[cellI];
for (int dirI = 0; dirI < dirs.Length; dirI++) {
Vector3Int potentialPoint = cellPos + dirs[dirI];
if (!occupiedCells.Contains(potentialPoint) && !points.Contains(potentialPoint)) points.Add(potentialPoint);
}
} return System.Linq.Enumerable.ToArray(points);
}
}
hey thanks for this @RaptorRush, i have to be honest i was expecting someone to point me in the right direction but this just topples that.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Firing A Projectile Along A Parabola 1 Answer
Percentage based chance in C#? 2 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer