# How do I slice physic object and measure its amount ?

Get help using Construct 2

### » Mon Feb 06, 2017 5:30 am

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 ?
B
15
S
2
Posts: 9
Reputation: 695

### » Mon Feb 06, 2017 6:41 pm

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:
Code: Select all
`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`

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: Select all
`get the list of edges of a polygontry to slice the polygon in two with a linedestroy the old polygon and create two new polygons from the sliced polygons`

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.
B
95
S
34
G
128
Posts: 5,495
Reputation: 81,678

### » Wed Feb 08, 2017 8:18 am

R0J0hound wrote: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:
Code: Select all
`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`

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: Select all
`get the list of edges of a polygontry to slice the polygon in two with a linedestroy the old polygon and create two new polygons from the sliced polygons`

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
B
15
S
2
Posts: 9
Reputation: 695