One small algorithm for man…

So after being in a slump between being sick for the past couple weeks and then just existential/midlife crises…  But in the back of my mind I haven’t stopped mulling the issue around, and last night as I sat down with notebook and pen in hand started to work on the idea, but ended up distracted by stuff online.  But I had a foundation.

Today during my lunch break I sat down and did it, and damn it wasn’t nearly as hard as I was making it out to be.

The problem was the second part of my joystick movement.  My friend helped me figure out which direction to move.  The issue then is that I need to ease in and ease out between where my ship is and where the joystick is located on the circle, otherwise it’s jarring, or I can only move 1 degree per unit of time which is agonizingly slow.

I found Robert Penner’s easing equations along with some other resources on the topic.  The main issue is finding out… well essentially I’m creating a Vector2… A direction and a single magnitude.  The direction I have from my friend’s help, but the magnitude is simply the difference in position between the joystick and the ship.  Oh great!  Until you realize that the shortest way to travel will get you to travel over the starting point on a circle (i.e. moving left  from 360 degrees back to 0) so you can’t just subtract because you’ll have an obvious problem at that juncture.  You have to take that into account.

The solution is different depending on whether you’re traveling left or right towards the joystick…

I tried to think it out and last night I started to draw what amounts to a logic/truth table.  Just two columns: direction.

Then two rows for each column: Ship.position < Joy.position and ship.position > joy.position.

Drew it out, played a bit and within a few minutes had what I think is the solution.
Now to implement it tonight and see how it really turns out, but I got a good feeling about this, and a good feeling about getting back on track on this game…

I have a feeling there’s a more algorithmic solution, after googling and seeing some trigonometric type answers.  But for my purposes I think my solution will be fast enough.  If it isn’t, I’ll try to figure out a better solution.

Leave a Reply

Your email address will not be published. Required fields are marked *