0 Favourites

How do I slice physic object and measure its amount ?

  • So i wanted to make a clone of this game :

    someone can give me a hint how to make it works ?

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • The core part is being able to slice a polygon by a line into two polygons. This can be done by calculating the intersection between the line and the edges, as well as finding which side of the line a point is.

    Here's one possible psuedcode of the algorithm to do it:

    for each edge clockwise
       if edge.point0 is on left side of line
          add point to poly1
       else
          add point to poly2
       if edge intersects line
          add intersection to poly1
          add intersection to poly2[/code:3d7po4e6]
    
    Next we have the issue of drawing an arbitrary polygon and changing the collision polygon.  Vanilla C2 plugins won't help here.  I don't use it but the third party polygon plugin by yann probably could work since it both lets you draw arbitrary polygons at runtime and it makes it's collision polygon match.
    
    The rough psuedocode for that could be:
    [code:3d7po4e6]get the list of edges of a polygon
    try to slice the polygon in two with a line
    destroy the old polygon and create two new polygons from the sliced polygons[/code:3d7po4e6]
    
    Also on a side note you'll want to set the new polygons' velocities the same as the old polygon otherwise the pieces will go at a complete stop after slicing.
    
    At least that's a general idea how to do it.  There might be some issues that need to be ironed out, such as I don't know how well the polygon plugin works with the physics behavior.
  • The core part is being able to slice a polygon by a line into two polygons. This can be done by calculating the intersection between the line and the edges, as well as finding which side of the line a point is.

    Here's one possible psuedcode of the algorithm to do it:

    for each edge clockwise
       if edge.point0 is on left side of line
          add point to poly1
       else
          add point to poly2
       if edge intersects line
          add intersection to poly1
          add intersection to poly2[/code:35epcbu6]
    
    Next we have the issue of drawing an arbitrary polygon and changing the collision polygon.  Vanilla C2 plugins won't help here.  I don't use it but the third party polygon plugin by yann probably could work since it both lets you draw arbitrary polygons at runtime and it makes it's collision polygon match.
    
    The rough psuedocode for that could be:
    [code:35epcbu6]get the list of edges of a polygon
    try to slice the polygon in two with a line
    destroy the old polygon and create two new polygons from the sliced polygons[/code:35epcbu6]
    
    Also on a side note you'll want to set the new polygons' velocities the same as the old polygon otherwise the pieces will go at a complete stop after slicing.
    
    At least that's a general idea how to do it.  There might be some issues that need to be ironed out, such as I don't know how well the polygon plugin works with the physics behavior.
    

    wow that a lot of explanation, thanks rojo

    i will look forward to try this

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)