Spiral movement

In my game I have multiple actors called reqjecs that move towards an actor called the requester that is located at the center of the screen.

I do this by applying force towards the requester on all the objects on every frame. This simulates a center point gravity:

Code: Select all

      public function forceTowardsRequester()
      {
         for each(var actor:Actor in reqjecActors)
         {
            var a:Number = requesterActor.body.GetPosition().x - actor.body.GetPosition().x;
            var b:Number = requesterActor.body.GetPosition().y - actor.body.GetPosition().y;
            var c:Number = Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2));
            
              var c2:Number = c / reqjecSpeed;
            var aa = a / c2;
            var bb = b / c2;
            
            var point:b2Vec2 = new b2Vec2(actor.body.GetPosition().x, actor.body.GetPosition().y);
            var force:b2Vec2 = new b2Vec2(aa, bb);
            
            actor.body.ApplyForce(force, point);
         }
      }


However in my new version I want the reqjecs to move in a spiral motion (circling) towards the requester. How can I achieve this?
I made a plan however it's not working yet:

I calculate the angle at which the reqjecs are moving towards the requester (center):

Code: Select all

var a:Number = requesterActor.body.GetPosition().x - actor.body.GetPosition().x;
var b:Number = requesterActor.body.GetPosition().y - actor.body.GetPosition().y;
var testAngle:Number = Math.atan(a/b)/(Math.PI/180);

After that I add a certain amount of degrees to the angle:

Code: Select all

testAngle += 45;

Once that is done I apply the force to the actor:

Code: Select all

var aa = Math.sin(testAngle) * reqjecSpeed;
var bb = Math.cos(testAngle) * reqjecSpeed;
            
var point:b2Vec2 = new b2Vec2(actor.body.GetPosition().x, actor.body.GetPosition().y);
var force:b2Vec2 = new b2Vec2(aa, bb);
            
actor.body.ApplyForce(force, point);
Got it working halfway:

Code: Select all

var testAngle:Number = Math.atan(a/b);


It's working correctly on the top half of the screen... but as soon as a reqjec hits the horizontal center of the screen they stop moving. The reqjecs in the lower half aren't working correctly.
Solved it... spiral gravity now working:

Code: Select all

if ( actor.body.GetPosition().y > stageHeigth/2/m_physScale )
{
   testAngle -= 3.1415926;
}
This appears a good movement. Gonna try it.
Can you show us how is it working?
This is the full code for the spiral gravity simulation.

Just make certain that you tweak linearDamping correctly on the actors!

Code: Select all

      public function forceTowardsRequester()
      {
         for each(var actor:Actor in reqjecActors)
         {
            var a:Number = requesterActor.body.GetPosition().x - actor.body.GetPosition().x;
            var b:Number = requesterActor.body.GetPosition().y - actor.body.GetPosition().y;

            var testAngle:Number = Math.atan(a/b);

            testAngle += 1.1471975;   // spiral angle
            
            if ( actor.body.GetPosition().y > stageHeigth/2/m_physScale )
            {
               testAngle -= 3.1415926;
            }

            var aa = Math.sin(testAngle) * reqjecSpeed;
            var bb = Math.cos(testAngle) * reqjecSpeed;
            
            var point:b2Vec2 = new b2Vec2(actor.body.GetPosition().x, actor.body.GetPosition().y);
            var force:b2Vec2 = new b2Vec2(aa, bb);
            
            actor.body.ApplyForce(force, point);
         }
      }
Really I was asking for a swf example..eh..
How did you calculate the testAngle?
Thanks for the info
จีคลับผ่านเว็บ

Original thread: https://box2d.org/forum/viewtopic.php?t=2662