How to generate list of vectors within a sphere? - c#

I'm trying to generate mining points within an asteroid for a game.
With that said, I have the asteroid center point, the radius of the asteroid, and the size of the ship I'm using.
Here is my method, but it's creating vectors outside of the range of the asteroid.
private void ExpandAreaOfControl()
{
int yStart = (int)TargetLocation.Y;
int xStart = (int)TargetLocation.X;
int zStart = (int)TargetLocation.Z;
int startGridSize = 20;
for (double y = startGridSize; y < Size / 2; y += startGridSize)
{
for (double x = startGridSize; x < Size / 2; x += startGridSize)
{
for (double z = startGridSize; z < Size / 2; z += startGridSize)
{
var point1 = new MineralPoint(new Vector3D(xStart + x, yStart + y, zStart + z));
mineralDeposits.Add(point1);
var point2 = new MineralPoint(new Vector3D(xStart + x, yStart + y, zStart - z));
mineralDeposits.Add(point2);
var point3 = new MineralPoint(new Vector3D(xStart + x, yStart - y, zStart - z));
mineralDeposits.Add(point3);
var point4 = new MineralPoint(new Vector3D(xStart + x, yStart - y, zStart + z));
mineralDeposits.Add(point4);
var point5 = new MineralPoint(new Vector3D(xStart - x, yStart + y, zStart + z));
mineralDeposits.Add(point5);
var point6 = new MineralPoint(new Vector3D(xStart - x, yStart - y, zStart + z));
mineralDeposits.Add(point6);
var point7 = new MineralPoint(new Vector3D(xStart - x, yStart + y, zStart - z));
mineralDeposits.Add(point7);
var point8 = new MineralPoint(new Vector3D(xStart - x, yStart - y, zStart - z));
mineralDeposits.Add(point8);
}
}
}
mineralDeposits = mineralDeposits.OrderBy(y => (y.Location - StartPosition).Length()).ToList();
}

What you want to do, if you want to keep you current code, is to get the vector that goes from the center of the asteroid to the mineral you've just created.
After obtaining that vector, divide it by its length, and multiply it by the radius of the asteroid - this will put the point right on its surface, but at the same angle it was at previously.
If you want the mineral to be inside the asteroid instead then multiply the vector by a number smaller than the radius.
If you want another technique - create a 3d vector with random values (make sure they're not all 0), and do the same as previously explained (starting from the 2nd paragraph).
Finally, after obtaining the result of your multiplied vector, add the asteroid's center position vector to it and you have your mineral position (relative to the coord origin used by the asteroid).

Related

How do I check if a tree object is really close to another in unity?

I have been making a survival game in unity and I have generated the trees with the below function
The GenerateTree function
void GenerateTree(int x, int y)
{
//define our tree
//generate log
int treeHeight = Random.Range(minTreeHeight, maxTreeHeight);
for(int i = 0; i < treeHeight; i++)
{
PlaceTile(log, x, y + i);
}
//generate leaves
PlaceTile(leaf,x,y+treeHeight);
PlaceTile(leaf, x, y + treeHeight+1);
PlaceTile(leaf, x, y + treeHeight+2);
PlaceTile(leaf, x-1, y + treeHeight);
PlaceTile(leaf, x-1, y + treeHeight+1);
PlaceTile(leaf, x + 1, y + treeHeight);
PlaceTile(leaf, x + 1, y + treeHeight + 1);
}
The PlaceTile function
public void PlaceTile(Sprite tileSprite, int x, int y)
{
GameObject newTile = new GameObject();
float chunkCoord = (Mathf.Round(x / chunkSize) * chunkSize);
chunkCoord /= chunkSize;
Debug.Log(chunkCoord);
newTile.transform.parent = worldChunks[(int)chunkCoord].transform;
newTile.AddComponent<SpriteRenderer>();
newTile.GetComponent<SpriteRenderer>().sprite = tileSprite;
newTile.name = tileSprite.name;
newTile.transform.position = new Vector2(x + 0.5f, y + 0.5f);
worldTiles.Add(newTile.transform.position - (Vector3.one * 0.5f));
}
I believe I need to use a if statement to check if another log is close by, but I need help of how to do that.
Vector2.Distance(Vector2D,Vector2D)
https://docs.unity3d.com/ScriptReference/Vector2.Distance.html
if(Vector2.Distance(Log1Location,Log2Location) > 10)
{
//Spawn?!
}

How to create a 3D mesh from a heightmap represented as a float array

I am attempting to understand how a 3D mesh has been constructed from a heightmap stored as a one dimensional float array. The only examples I have seen previously have made use of a 2D float array, and I am struggling to wrap my head around the math involved here. Any insight into it would be appreciated. I have commented the code which I do not quite understand yet for your convenience.
Source of code: https://github.com/SebLague/Hydraulic-Erosion
public void ContructMesh () {
Vector3[] verts = new Vector3[mapSize * mapSize];
int[] triangles = new int[(mapSize - 1) * (mapSize - 1) * 6];
int t = 0;
//Note that default mapSize is 255
for (int i = 0; i < mapSize * mapSize; i++) {
//Following code is not properly understood
int x = i % mapSize;
int y = i / mapSize;
int meshMapIndex = y * mapSize + x;
Vector2 percent = new Vector2 (x / (mapSize - 1f), y / (mapSize - 1f));
Vector3 pos = new Vector3 (percent.x * 2 - 1, 0, percent.y * 2 - 1) * scale;
pos += Vector3.up * map[meshMapIndex] * elevationScale; //Elevation scale is 20 by default
verts[meshMapIndex] = pos;
//End of misunderstood code
if (x != mapSize - 1 && y != mapSize - 1) {
t = (y * (mapSize - 1) + x) * 3 * 2;
triangles[t + 0] = meshMapIndex + mapSize;
triangles[t + 1] = meshMapIndex + mapSize + 1;
triangles[t + 2] = meshMapIndex;
triangles[t + 3] = meshMapIndex + mapSize + 1;
triangles[t + 4] = meshMapIndex + 1;
triangles[t + 5] = meshMapIndex;
t += 6;
}
}
Mesh mesh = new Mesh();
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
mesh.vertices = verts;
mesh.triangles = triangles;
mesh.RecalculateNormals ();
What specifically do you not understand?
int x = i % mapSize; // Get the x location of the current point
int y = i / mapSize; // Get the y location of the current point
// This should be equal to i, IDK why this is even calculated
int meshMapIndex = y * mapSize + x;
// How far along either direction are we?
Vector2 percent = new Vector2 (x / (mapSize - 1f), y / (mapSize - 1f));
// Make a new vector that scales the X and Y coordinates up.
// The Y coordinate is set to the Z element in this vector
// Presumably because whatever you use to render uses the Y element as "up"
// And the X-Z plane is the horizontal plane
// Also normalize X and Z to lie between -1*scale and 1*scale
Vector3 pos = new Vector3 (percent.x * 2 - 1, 0, percent.y * 2 - 1) * scale;
// Add the value at the current index, times the scale, as the Y element of pos
pos += Vector3.up * map[meshMapIndex] * elevationScale; //Elevation scale is 20 by default
// The X-Z values of pos give you the location of the vertex in the horizontal plane
// The Y value of pos gives you the height
// save the newly calculated pos in verts
verts[meshMapIndex] = pos;

Determining if a point is inside a circle on an isometric map

I'm trying to figure out a way to check if a certain point is inside or outside a circle on an isometric map. I'm currently using the following method to draw the circle:
public static List<Coords> GetBrushCircleCoords(int x0, int y0, int radius)
{
List<Coords> coords = new List<Coords>();
int x = radius;
int y = 0;
int err = 0;
while (x >= y)
{
coords.Add(new Coords(x0 + x, y0 + y));
coords.Add(new Coords(x0 + y, y0 + x));
coords.Add(new Coords(x0 - y, y0 + x));
coords.Add(new Coords(x0 - x, y0 + y));
coords.Add(new Coords(x0 - x, y0 - y));
coords.Add(new Coords(x0 - y, y0 - x));
coords.Add(new Coords(x0 + y, y0 - x));
coords.Add(new Coords(x0 + x, y0 - y));
y += 1;
err += 1 + 2 * y;
if (2 * (err - x) + 1 > 0)
{
x -= 1;
err += 1 - 2 * x;
}
}
return coords;
}
And the approach I'm trying to determine if the point is inside the circle is basically taking the desired point, determine its distance to the center and checking if it's bigger than the radius with the following method:
public static int GetDistance(Coords _from, Coords _to)
{
return Math.Max(Math.Abs(_from.X - _to.X), Math.Abs(_from.Y - _to.Y));
}
However, it seems the GetDistance method isn't the best way to calculate this as the distance calculated by it is fairly shorter than the one used on the GetBrushCircleCoords. What would be the correct way of determining if a certain point is inside/outside this circle?
On a Euclidean plane distance function (metric) is given by the Pythagorean Theorem. So shouldn't GetDistance be something like:
public static double GetDistance(Coords from, Coords to)
{
//a^2 + b^2 = c^2
var a = from.X - to.X;
var b = from.Y - to.Y;
var c = Math.Sqrt(a*a+b*b);
return c;
}

C# DirectX Circle Drawing not working correctly

I've been trying to recode a C++ DirectX code to C# that would help me with Drawing a perfect circle. Currently I have this code that i translated by myself:
private void Circle(int X, int Y, int radius, int numSides, Color color)
{
Vector2[] Line = new Vector2[128];
float Step = (float)(Math.PI * 2.0 / numSides);
int Count = 0;
for (float a = 0; a < Math.PI * 2.0; a += Step)
{
float X1 = (float)(radius * Math.Cos(a) + X);
float Y1 = (float)(radius * Math.Sin(a) + Y);
float X2 = (float)(radius * Math.Cos(a + Step) + X);
float Y2 = (float)(radius * Math.Sin(a + Step) + Y);
Line[Count].X = X1;
Line[Count].Y = Y1;
Line[Count + 1].X = X2;
Line[Count + 1].Y = Y2;
Count += 2;
}
line.Begin();
line.Draw(Line, color);
line.End();
}
The problem is that the circle is drawn but also a Line from a point in the circle to the left top corner, like this.
Don't iterate with a floating point variable. They might get imprecise during the iteration. In your case, the last step is probably very close behind the upper bound (instead of hitting it exactly). So it won't get calculated and left as the default (0, 0).
So use an integer iteration variable:
for (int i = 0; i < numSides; ++i)
{
float a = i * Step;
...
}
Then, you can also get rid of Count.
Furthermore, you should make your coordinate buffer dynamic:
Vector2[] Line = new Vector2[2 * numSides];

Traverse Pixels in a circle from the center

I need an algorithm like Bresenham's circle algorithm, but with some modifications.
The algorithm must visit all pixels in the radius (so essentially a fill).
The algorithm must start from the center of the circle
It must visit all points that would normally be visited (no holes)
It must visit each point in the circle exactly once
One technique I came up with would first determine all pixel coordinates inside the circle by just going through the rectangle of the circle and checking with Math.Sqrt if it is inside the circle.
Then it would order the pixels by distance and then visit each of them.
That would be exactly what I want, with the exception of being fast.
So my questions is:
Is there a fast way to do this without fetching,ordering and then visiting each pixel?
Just for clarification I do not actually want to draw onto the image, I only want to traverse them in the described order.
First, we can use fact, that circle can be divided in 8 octants. So we just need to fill single octant and use simple +- coordinate change to get full circle. So if we try to fill only one octant, we need to worry only about 2 directions from center : left and left top. Also, clever use of data structures like priority queue (.NET doesn't have it, so you need to find it somewhere else) and hash map can drastically improve performance.
/// <summary>
/// Make sure it is structure.
/// </summary>
public struct Point
{
public int X { get; set; }
public int Y { get; set; }
public int DistanceSqrt()
{
return X * X + Y * Y;
}
}
/// <summary>
/// Points ordered by distance from center that are on "border" of the circle.
/// </summary>
public static PriorityQueue<Point> _pointsToAdd = new PriorityQueue<Point>();
/// <summary>
/// Set of pixels that were already added, so we don't visit single pixel twice. Could be replaced with 2D array of bools.
/// </summary>
public static HashSet<Point> _addedPoints = new HashSet<Point>();
public static List<Point> FillCircle(int radius)
{
List<Point> points = new List<Point>();
_pointsToAdd.Enqueue(new Point { X = 1, Y = 0 }, 1);
_pointsToAdd.Enqueue(new Point { X = 1, Y = 1 }, 2);
points.Add(new Point {X = 0, Y = 0});
while(true)
{
var point = _pointsToAdd.Dequeue();
_addedPoints.Remove(point);
if (point.X >= radius)
break;
points.Add(new Point() { X = -point.X, Y = point.Y });
points.Add(new Point() { X = point.Y, Y = point.X });
points.Add(new Point() { X = -point.Y, Y = -point.X });
points.Add(new Point() { X = point.X, Y = -point.Y });
// if the pixel is on border of octant, then add it only to even half of octants
bool isBorder = point.Y == 0 || point.X == point.Y;
if(!isBorder)
{
points.Add(new Point() {X = point.X, Y = point.Y});
points.Add(new Point() {X = -point.X, Y = -point.Y});
points.Add(new Point() {X = -point.Y, Y = point.X});
points.Add(new Point() {X = point.Y, Y = -point.X});
}
Point pointToLeft = new Point() {X = point.X + 1, Y = point.Y};
Point pointToLeftTop = new Point() {X = point.X + 1, Y = point.Y + 1};
if(_addedPoints.Add(pointToLeft))
{
// if it is first time adding this point
_pointsToAdd.Enqueue(pointToLeft, pointToLeft.DistanceSqrt());
}
if(_addedPoints.Add(pointToLeftTop))
{
// if it is first time adding this point
_pointsToAdd.Enqueue(pointToLeftTop, pointToLeftTop.DistanceSqrt());
}
}
return points;
}
I will leave the expansion to full list on you. Also make sure borders of the octants don't cause doubling of the points.
Ok, I couldn't handle it and did it myself. Also, to make sure it has properties you desire I did simple test :
var points = FillCircle(50);
bool hasDuplicates = points.Count != points.Distinct().Count();
bool isInOrder = points.Zip(points.Skip(1), (p1, p2) => p1.DistanceSqrt() <= p2.DistanceSqrt()).All(x => x);
I found a solution that satisfies my performance needs.
It's very simple, just a offset array.
static Point[] circleOffsets;
static int[] radiusToMaxIndex;
static void InitCircle(int radius)
{
List<Point> results = new List<Point>((radius * 2) * (radius * 2));
for (int y = -radius; y <= radius; y++)
for (int x = -radius; x <= radius; x++)
results.Add(new Point(x, y));
circleOffsets = results.OrderBy(p =>
{
int dx = p.X;
int dy = p.Y;
return dx * dx + dy * dy;
})
.TakeWhile(p =>
{
int dx = p.X;
int dy = p.Y;
var r = dx * dx + dy * dy;
return r < radius * radius;
})
.ToArray();
radiusToMaxIndex = new int[radius];
for (int r = 0; r < radius; r++)
radiusToMaxIndex[r] = FindLastIndexWithinDistance(circleOffsets, r);
}
static int FindLastIndexWithinDistance(Point[] offsets, int maxR)
{
int lastIndex = 0;
for (int i = 0; i < offsets.Length; i++)
{
var p = offsets[i];
int dx = p.X;
int dy = p.Y;
int r = dx * dx + dy * dy;
if (r > maxR * maxR)
{
return lastIndex + 1;
}
lastIndex = i;
}
return 0;
}
With this code you just get the index where to stop from radiusToMaxIndex, then loop through circleOffsets and visit those pixels.
It will cost lot of memory like this, but you can always change the datatype of the offsets from Point to a custom one with Bytes as members.
This solution is extremely fast, fast enough for my needs. It obviously has the drawback of using some memory, but lets be honest, instantiating a System.Windows.Form uses up more memory than this...
You have already mentioned Bresenhams's circle algorithm. That is a good starting point: You could start with the centre pixel and then draw Bresenham circles of increasing size.
The problem is that the Bresenham circle algorithm will miss pixels near the diagonals in a kind of Moiré effect. In another question, I have adopted the Bresenham algorithm for drawing between an inner and outer circle. With that algorithm as base, the strategy of drawing circles in a loop works.
Because the Bresenham algorithm can place pixels only at discrete integer coordinates, the order of visiting pixels will not be strictly in order of increasing distance. But the distance will always be within one pixel of the current circle you are drawing.
An implementation is below. That's in C, but it only uses scalars, so it shouldn't be hard to adapt to C#. The setPixel is what you do to each pixel when iterating.
void xLinePos(int x1, int x2, int y)
{
x1++;
while (x1 <= x2) setPixel(x1++, y);
}
void yLinePos(int x, int y1, int y2)
{
y1++;
while (y1 <= y2) setPixel(x, y1++);
}
void xLineNeg(int x1, int x2, int y)
{
x1--;
while (x1 >= x2) setPixel(x1--, y);
}
void yLineNeg(int x, int y1, int y2)
{
y1--;
while (y1 >= y2) setPixel(x, y1--);
}
void circle2(int xc, int yc, int inner, int outer)
{
int xo = outer;
int xi = inner;
int y = 0;
int erro = 1 - xo;
int erri = 1 - xi;
int patch = 0;
while (xo >= y) {
if (xi < y) {
xi = y;
patch = 1;
}
xLinePos(xc + xi, xc + xo, yc + y);
yLineNeg(xc + y, yc - xi, yc - xo);
xLineNeg(xc - xi, xc - xo, yc - y);
yLinePos(xc - y, yc + xi, yc + xo);
if (y) {
yLinePos(xc + y, yc + xi, yc + xo);
xLinePos(xc + xi, xc + xo, yc - y);
yLineNeg(xc - y, yc - xi, yc - xo);
xLineNeg(xc - xi, xc - xo, yc + y);
}
y++;
if (erro < 0) {
erro += 2 * y + 1;
} else {
xo--;
erro += 2 * (y - xo + 1);
}
if (y > inner) {
xi = y;
} else {
if (erri < 0) {
erri += 2 * y + 1;
} else {
xi--;
erri += 2 * (y - xi + 1);
}
}
}
if (patch) {
y--;
setPixel(xc + y, yc + y);
setPixel(xc + y, yc - y);
setPixel(xc - y, yc - y);
setPixel(xc - y, yc + y);
}
}
/*
* Scan pixels in circle in order of increasing distance
* from centre
*/
void scan(int xc, int yc, int r)
{
int i;
setPixel(xc, yc);
for (i = 0; i < r; i++) {
circle2(xc, yc, i, i + 1);
}
}
This code takes care of not visiting pixels that are in two octants by skipping coincident picels on alterante octants. (Edit: There was still abug in the original code, but it's fixed now by means of the ´patch` variable.)
There's also room for improvement: The inner circle is basically the outer circle of the previous iteration, so there's no point in calculating it twice; you could keep an array of the outer points of the previous circle.
The xLinePos functions are also a bit too complicated. There are never more than two pixels drawn in that function, usually only one.
If the roughness of the search order bothers you, you can run a more exact algorithm once at the beginning of the program, where you calculate a traversing order for all circles up to a reasonable maximum radius. You can then keep that data and use it for iterating all circles with smaller radii.

Categories