Spiral movement

Discuss issues specific to the Flash/AS3 port of Box2D
Avatara
Posts: 55
Joined: Mon Oct 13, 2008 5:30 am

Spiral movement

Postby Avatara » Sun Mar 01, 2009 3:38 pm

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?

Avatara
Posts: 55
Joined: Mon Oct 13, 2008 5:30 am

Re: Spiral movement

Postby Avatara » Sun Mar 01, 2009 5:37 pm

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);

Avatara
Posts: 55
Joined: Mon Oct 13, 2008 5:30 am

Re: Spiral movement

Postby Avatara » Sun Mar 01, 2009 5:51 pm

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.

Avatara
Posts: 55
Joined: Mon Oct 13, 2008 5:30 am

Re: Spiral movement

Postby Avatara » Sun Mar 01, 2009 6:41 pm

Solved it... spiral gravity now working:

Code: Select all

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

Handyman
Posts: 1
Joined: Sun Mar 01, 2009 8:35 pm
Contact:

Re: Spiral movement

Postby Handyman » Sun Mar 01, 2009 8:38 pm

This appears a good movement. Gonna try it.

Neosano
Posts: 177
Joined: Fri Jan 23, 2009 12:53 pm

Re: Spiral movement

Postby Neosano » Mon Mar 02, 2009 2:39 am

Can you show us how is it working?

Avatara
Posts: 55
Joined: Mon Oct 13, 2008 5:30 am

Re: Spiral movement

Postby Avatara » Mon Mar 02, 2009 1:39 pm

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);
         }
      }

Neosano
Posts: 177
Joined: Fri Jan 23, 2009 12:53 pm

Re: Spiral movement

Postby Neosano » Mon Mar 02, 2009 2:50 pm

Really I was asking for a swf example..eh..

gamist
Posts: 17
Joined: Wed May 30, 2012 11:05 am

Re: Spiral movement

Postby gamist » Fri Apr 26, 2013 5:36 am

How did you calculate the testAngle?


Return to “Flash”



Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest