0
\$\begingroup\$

How to create objects that fill a specific area? How to instantiate cubes within a random 2D form ... Trying not to leave the black lines.

@ JaamiParvez

Objetive:

enter image description here

\$\endgroup\$
10
  • 2
    \$\begingroup\$ How do you currently represent the area you need to fill? Can you show us your code? \$\endgroup\$
    – DMGregory
    Oct 20 '19 at 19:22
  • \$\begingroup\$ A list of coordinates. \$\endgroup\$
    – Rhoger
    Oct 20 '19 at 19:35
  • \$\begingroup\$ Do you need to use squares to fill the space or would you be okay with filling the space with, let's say, triangles? \$\endgroup\$ Oct 20 '19 at 19:48
  • \$\begingroup\$ They could fill the shape with triangles too. - I use points into map to make the shape ... Point X, Y = (0.5, 0.5) Point X, Y = (0.0, 0.8) Point X, Y = (- 0.9, 1.2) Point X, Y = (- 1.0, 1.1) Point X, Y = (1.2, -1.0) \$\endgroup\$
    – Rhoger
    Oct 20 '19 at 19:51
  • \$\begingroup\$ Then it should be quite easy to triangulate the shape, and then fill the generated triangles with a solid color or some texture. \$\endgroup\$ Oct 20 '19 at 21:17
2
\$\begingroup\$

I'm not very familiar with C# so most of the code will be pseudo code. You probably have to adapt it to work for you. You wrote in a comment that you were okay with using triangles instead of squares, so the example below is generating triangles and edge shapes.

I also don't know anything about the coordinate system that you're using in your code, what engine you're using etc. In my code example below, the coordinate system has (0, 0) in the upper left corner of the window, with X+ pointing to the right and Y+ pointing down.

I can't help you with the rendering of the triangles and how the rigid bodies should be created, based on the triangles' information etc, since I, like I said, don't know what engine/library and physics library etc that you're using.

An example picture has also been attached below to show how the triangles are placed and how I've numbered the points of the triangles.

// Convenient way to store an X- and Y position in one combined unit
public struct Vector2D
{
    public Vector2D(void)
    {

    }

    public Vector2D(const float xPosition, const float yPosition)
    : x(xPosition)
    , y(yPosition)
    {

    }

    public x;
    public y;
};

// A simple 2D triangle struct containing 3 positions, one for each point in the triangle
public struct Triangle2D
{
    public Triangle2D(void)
    {

    }

    public Triangle2D(const Vector2D& rPoint1, const Vector2D& rPoint2, const Vector2D& rPoint3)
    : point1(rPoint1)
    , point2(rPoint2)
    , point3(rPoint3)
    {

    }

    public Vector2D point1 = new Vector2D(0.0f, 0.0f);
    public Vector2D point2 = new Vector2D(0.0f, 0.0f);
    public Vector2D point3 = new Vector2D(0.0f, 0.0f);
};

// The list containing the points that's forming the shape
List<Vector2D> pointList = new List<Vector2D>();

pointList.Add(new Vector2D(0.0f, 0.0f));
pointList.Add(new Vector2D(0.7f, 0.5f));
pointList.Add(new Vector2D(0.8f, 0.4f));
pointList.Add(new Vector2D(1.0f, 0.8f));
pointList.Add(new Vector2D(0.0f, 0.8f));

// Creating the collision triangles

// A list containing the generated triangles, based on the above shape's points
List<Triangle2D> triangleList = new List<Triangle2D>();

// The red triangle in the image below
triangleList.Add(new Triangle2D(pointList[0], pointList[1], pointList[4]));

// The purple triangle
triangleList.Add(new Triangle2D(pointList[1], pointList[2], pointList[4]));

// And the orange triangle
triangleList.Add(new Triangle2D(pointList[2], pointList[3], pointList[4]));

And if you would like to generate edge shapes / collision lines instead:

// A simple 2D line struct containing 2 positions, one for each point in the line
public struct Line2D
{
    public Line2D(void)
    {

    }

    public Line2D(const Vector2D& rStart, const Vector2D& rEnd)
    : start(rStart)
    , end(rEnd)
    {

    }

    public Vector2D start   = new Vector2D(0.0f, 0.0f);
    public Vector2D end     = new Vector2D(0.0f, 0.0f);
};

// Creating edge shapes / collision lines

// A list containing the generated lines, based on the above shape's points
List<Line2D> lineList = new List<Line2D>();

// Line 1 - point 0 = start, point 1 = end
lineList.Add(new Line2D(pointList[0], pointList[1]));

// Line 2 - point 1 = start, point 2 = end
lineList.Add(new Line2D(pointList[1], pointList[2]));

// Line 3 - point 2 = start, point 3 = end
lineList.Add(new Line2D(pointList[2], pointList[3]));

// Line 4 - point 3 = start, point 4 = end
lineList.Add(new Line2D(pointList[3], pointList[4]));

// Line 5 - point 4 = start, point 0 = end
lineList.Add(new Line2D(pointList[4], pointList[0]));

Result:

Triangulate

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.