Coding challenge: convex shapes

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
togFox
Party member
Posts: 779
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Coding challenge: convex shapes

Post by togFox »

At the start of the game I create a shape that has 8 segments and forms a perfect square:

Image

Love2D requires this to be convex (Box2d):

Creates a new PolygonShape.
This shape can have 8 vertices at most, and must form a convex shape.


As the game proceeds and the player is rewarded, the shape grows by moving one 'point' "outwards", but it must do so without breaking the "convex" rule:

ImageImageImageImage

Image

The shape can get quite large and quite complex as the player keeps winning and getting rewarded. Over time, the size of the shape starts to work against the player and that is one of the mechanics of the game.

Here is the challenge:

Assuming a 'point' is the end of one segment and the start of the next segment:
  • Starting with the square, each time the player receives a reward, two segments (or one 'point') are moved so that the shape gets larger, i.e. the inside area increases.
  • The shape must always be convex
  • The 8 'points' of the convex can move according to whatever rules you invent (i.e. along axis or random or whatever)
  • Any point can be moved according to described rules
  • Assume moving a 'point' is by an arbitrary distance (unless it matters) but for simplicity, assume whole numbers of pixels
  • You can assume that moving the end of one segment means moving the start of the segment it joins
  • Bonus points if the shape formed is irregular i.e. not a perfect square or rectangle.
What algorithm can I use to move a point in a direction such that it remains convex?
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
darkfrei
Party member
Posts: 1182
Joined: Sat Feb 08, 2020 11:09 pm

Re: Coding challenge: convex shapes

Post by darkfrei »

togFox wrote: Sun Jul 17, 2022 10:56 am What algorithm can I use to move a point in a direction such that it remains convex?
Just keep in the green, cyan or magenta areas.
2022-07-17T14_13_20-Window.png
2022-07-17T14_13_20-Window.png (9.58 KiB) Viewed 2984 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
togFox
Party member
Posts: 779
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Coding challenge: convex shapes

Post by togFox »

Thanks.

How would I avoid choosing a corner at the very start and fail instantly? Should I try, fail, then choose another point at random and repeat? It's a 50/50 shot so not a terrible approach.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
Xugro
Party member
Posts: 110
Joined: Wed Sep 29, 2010 8:14 pm

Re: Coding challenge: convex shapes

Post by Xugro »

Just a small nitpick:
darkfrei wrote: Sun Jul 17, 2022 12:21 pm Just keep in the green, cyan or magenta areas.
You cannot move point A into the magenta area, because this would decrease the overall area of the polygon which is against togFoxs first point:
togFox wrote: Sun Jul 17, 2022 10:56 am
  • Starting with the square, each time the player receives a reward, two segments (or one 'point') are moved so that the shape gets larger, i.e. the inside area increases.
Edit:
togFox wrote: Sun Jul 17, 2022 12:56 pm How would I avoid choosing a corner at the very start and fail instantly? Should I try, fail, then choose another point at random and repeat? It's a 50/50 shot so not a terrible approach.
Should the point to move be chosen by the player or randomly?
User avatar
togFox
Party member
Posts: 779
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Coding challenge: convex shapes

Post by togFox »

Not the player. The algorithm needs to choose a point and then move that line 'outwards' without breaking the rules.

My first problem: deciding which points can legally be moved (the four corners can'tbe moved first)

Then: with a point that can be moved legally, know which direction is a legal move.

I suspect solving step 1 will solve step 2.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
Xugro
Party member
Posts: 110
Joined: Wed Sep 29, 2010 8:14 pm

Re: Coding challenge: convex shapes

Post by Xugro »

You can just use brute force. This will work within a few attempts (most of the time). I attached a simple example.
Attachments
random_polygon.love
(1.16 KiB) Downloaded 96 times
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

Re: Coding challenge: convex shapes

Post by pgimeno »

There are some shapes for which no moves that extend the area and keep the shape convex are possible. What do you plan to do in that case?
User avatar
togFox
Party member
Posts: 779
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: Coding challenge: convex shapes

Post by togFox »

Xugro wrote: Sun Jul 17, 2022 5:56 pm You can just use brute force. This will work within a few attempts (most of the time). I attached a simple example.
Nice one. A lot of hypothesising on Discord but this addresses all the criteria. :) I notice not all segments 'move' - either a bug or your algorithm keeps moving the same three points because that reaches success the fastest?

Edit: I see you used a physics object to calculate mass. Very sneaky. I approve. :)
There are some shapes for which no moves that extend the area and keep the shape convex are possible. What do you plan to do in that case?
Is this possible? With 8 segments surely there will always be one option?
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

Re: Coding challenge: convex shapes

Post by pgimeno »

togFox wrote: Mon Jul 18, 2022 12:10 amIs this possible? With 8 segments surely there will always be one option?
Well, the minimum shape where that is outright impossible that I can think of has 9 vertices, but I assumed the playfield to be limited, and then yes, it will eventually happen. Maybe that assumption was wrong, but you've been talking about integer coordinates which suggests that.

With 8 vertices there's a shape where you can move just 1 vertex, and once the corner of the playfield is reached, there are no moves. It's also pretty boring, as the game then consists of moving that vertex towards the corner as slowly as possible.

Edit: I was wrong, sorry.
Last edited by pgimeno on Mon Jul 18, 2022 8:48 am, edited 2 times in total.
User avatar
darkfrei
Party member
Posts: 1182
Joined: Sat Feb 08, 2020 11:09 pm

Re: Coding challenge: convex shapes

Post by darkfrei »

Xugro wrote: Sun Jul 17, 2022 3:21 pm Just a small nitpick:
darkfrei wrote: Sun Jul 17, 2022 12:21 pm Just keep in the green, cyan or magenta areas.
You cannot move point A into the magenta area, because this would decrease the overall area of the polygon which is against togFoxs first point:
You are right, green area makes area bigger, the magenta area makes is tinier, the line through the point A and parallel to the magenta line don't change the polygon area.

So you can take any random point from the triangle and it will be often near the this not drawn line.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 60 guests