How do I slice physic object and measure its amount ?

0 favourites
  • 6 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • So i wanted to make a clone of this game : http://www.physicsgames.net/game/3_Slices.html

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

  • 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

  • Hello R0j0Hound/Timedoor,

    Any success with slicing object, can you please help since I'm unable to slice object using construct 3.

    It would be great with you could share sample source file.

    Thanks

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I haven't gone through the trouble to do it. I haven't really used C3, but it's capabilities are roughly the same as C2.

    There are three parts to this:

    1. Have a list of points to define a polygon. Then we can do the logic in the previous post to do the splitting. We wouldn't be working with actual Construct objects. So probably use arrays to store this stuff. This is math and algorithm stuff, which is doable depending on your skill.

    2. We need a way to draw these polygons. Construct doesn't have a way to do this. There's the paster object I made for C2 that has a "draw quad" action. That could be used to draw any polygon with some creativity. You'd have to deal with texture coordinates if the split objects have textures. The paster plugin was ported to C3 by someone, but it won't work with the new runtime at all if that's a concern.

    Alternatively you could just use sprites stretched into lines to do the outline of the polygon without third party stuff. The con is the polygon wouldn't be filled.

    3. Physics. The built in physics behavior is unusable for this. I don't think any of the third party ones are either. You'd either have to deal with another physics library directly in javascript or implement the physics in events. It's going to be a lot of effort either way.

    So more or less everything from scratch, plus most of it wouldn't be able to interact with other construct features. So construct doesn't really have anything to assist with making such a game. This safely throws this into the very advanced difficulty range.

  • Hello ROJOhound,

    Thank you for the feedback and I will have look at it and try based on your feedback.

    Thanks with regards

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