Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creation of Custom RigidBodies #14

Closed
jaipack17 opened this issue Dec 6, 2021 · 1 comment · Fixed by #15
Closed

Creation of Custom RigidBodies #14

jaipack17 opened this issue Dec 6, 2021 · 1 comment · Fixed by #15
Assignees

Comments

@jaipack17
Copy link
Owner

jaipack17 commented Dec 6, 2021

Support for RigidBodies formed by user-generated point-constraint structures, has been in development for quite some time now. While I am on it, something I have been puzzled about is the way you would create these objects.

RigidBodies take in UI Elements as their base from which the constraints and points and formed behind the hood, which makes your UI element have physical presence on the screen. But for RigidBodies that you create on your own as per your requirements, say for example a triangle, or an irregular quadrilateral, you would have to create your own constraints and points instead of the library doing the job for you.

There are several methods I have though of, in order to make creation of these objects easier. But first it is important to know what the "point-constraint" structure really is.

image

In the above image, the green dots are the arbitrary points we chose for our RigidBody, if we pass in a UI element, the 4 dots are automatically placed at its corners, also keeping in mind its rotation. There are 6 lines connecting the points all together. The lines in black are the edges of the RigidBody and the lines in red are "support constraints" which are meant to hold the RigidBody's structure in place and it prevent it from collapsing into nothingness. It is worth noting that support constraints are not used in collision detection. All constraints are rods.

You will have to create those points, edges and support constraints on your own! And to make this process easier, I wish to create API that is feasible and easy to understand. Here are some rough ideas I have thought of, and I would need you to help me with the evaluation of the same. If you have better and cleaner methods you would want to be used, please consider sharing that with me.

Creating Points and Constraints from scratch

You would have to create your own points and constraints from scratch using Create() and then pass in those, as properties of the RigidBody. At first glance, this method is very tedious.

-- an example of a triangle

local function makeTriangle(a: Vector2, b: Vector2, c: Vector2)
      local PointA = Engine:Create("Point", { Position = a })
      local PointB = Engine:Create("Point", { Position = b })
      local PointC = Engine:Create("Point", { Position = c })
      
      local C1 = Engine:Create("Constraint", { Type = "Rod", Point1 = PointA, Point2 = PointB, Visible = true })
      local C2 = Engine:Create("Constraint", { Type = "Rod", Point1 = PointB, Point2 = PointC, Visible = true })
      local C3 = Engine:Create("Constraint", { Type = "Rod", Point1 = PointA, Point2 = PointC, Visible = true })
      
      return Engine:Create("RigidBody", {
            Custom = true,
            Collidable = true,
            Anchored = false,
            Points = { PointA, PointB, PointC },
            Constraints = { C1, C2, C3 }
      })
end
 
local triangle = makeTriangle(Vector2.new(10, 10), Vector2.new(5, 10), Vector2.new(15, 10))

Providing just the Positions of vertices

This method would use delaunay triangulation to form the structure behind the scenes. It will handle both support constraints and edges of the RigidBody. A downside of this is, it lacks initial customization and may result into incorrect structures being formed.

local quad = Engine:Create("RigidBody", {
     Custom = true,
     Collidable = true,
     Anchored = true,
     Points = { Vector2.new(0, 0), Vector2.new(10, 0), Vector2.new(10, 10), Vector2.new(0, 10) }
})

Providing the positions for points of each constraint

We provide a table of dictionaries containing required information for the edges and support constraints of a RigidBody.

local struct = {
    { Point1 = Vector2.new(0, 0), Point2 = Vector2.new(0, 10) },
    { Point1 = Vector2.new(0, 10), Point2 = Vector2.new(10, 10) },
    { Point1 = Vector2.new(10, 10), Point2 = Vector2.new(10, 0) },
    { Point1 = Vector2.new(10, 0), Point2 = Vector2.new(0, 0) },
    { Point1 = Vector2.new(0, 0), Point2 = Vector2.new(10, 10), Support = true },
    { Point1 = Vector2.new(10, 0), Point2 = Vector2.new(0, 10), Support = true }
}

local quad = Engine:Create("RigidBody", {
     Custom = true,
     Collidable = true,
     Anchored = true,
     Structure = struct
})

Let me know about what you think about these creation methods. Which one should I consider? Any other options?

@jaipack17 jaipack17 self-assigned this Dec 6, 2021
@ecurtiss
Copy link

ecurtiss commented Dec 6, 2021

As an uninformed bystander, I think you should support option 2 for ease of use and option 3 for those who need precision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants