Programming detection of a circle drawn in 2D space
Hi!
I'm mid-way through developing my 2D game, and can't figure this one out!
I have a player sprite attached to the touch location (I'm developing for mobile), and on movement the sprite forms a trail behind it. This trail is created by spawning in a pre-rendered line at the mid-point of delta-distance-moved between updates and is rotated to match the change in location.
Question 1. Is spawning in a pre-rendered art asset for the trail the best solution?
Secondly, I want the player to be able to create a closed loop with this trail, forming a circle. This circle will then need to be transformed into a seperate object so I can do some stuff. When the player does close the loop, they'll collide with one of the spawned trail objects, so I can detect WHEN to calculate the spawned circle, but I can't for the life of me think how I'm going to calculate what shape this circle is or how to then create a replication into the game world. All the spawned objects for the trail are stored in an array so I can access all of their locations.
To clarify, in example A below, the 5 points are in a circle, so calculating the position of x and then spawning a circle at this point is a solution. However, in example B the points are not circular, meaning that a simple circle cannot be spawned - I need to spawn in an irregular shape!
Can I create this circle with code? The shape is going to be irregular, so spawning in pre-rendered art won't work!
Thank you!
Answer by cjdev · Sep 29, 2015 at 07:28 PM
To your first question, I can't think of any other way to implement this without spawning assets and as long as there aren't more than a couple handfuls at a time you shouldn't see much of a performance hit. Also, if this player trail is a persistent thing I would definitely recommend object pooling for the spawned assets so that the overhead is even lower.
In order to calculate the shape of the trail it depends on how you are going to render the trail after you have it. Are you using a line renderer and approximating the curve as closely as possible or just spawning more objects at closer distances to each other? One way you could do it is by recording player positions regularly at much shorter intervals than the spawn points have and then using them as your points on the curve for your new shape. This way you can have an arbitrarily detailed curve by adjusting how often positions get recorded rather than just using the handful of spawn points that are far away from each other.
Thank you for your reply!
As for the first question, that's quite okay, spawning assets is working good so far, just wanted confirmation that it was the best solution!
That's an interesting solution, I'll give it some thought!
Your answer
Follow this Question
Related Questions
Keyboard input to Mobile Input 0 Answers
Shooting Mechanic in 2D Mobile (Unity C#) 1 Answer
2d mobile Multi Scene inventory 0 Answers