Object not moving mystery... [SOLVED]

Discuss issues specific to the Flash/AS3 port of Box2D
Kabomb
Posts: 2
Joined: Sat May 23, 2009 2:17 am

Object not moving mystery... [SOLVED]

Postby Kabomb » Sat May 23, 2009 2:35 am

I'm making a top-down game where you can 'launch' a ball by moving the mouse to set angle and speed and clicking to launch it. I had this working perfectly before, but as I made some adjustments something went wrong and now something weird happens...

With gravity vector = (0, 0) , I click the mouse and the ball does not move. I have traced the x position and velocity, where the velocity is correct but the x position stays constant.

However, when I do something to the ball when I'm defining it, such as SetLinearVelocity(0, 1) and click while it's moving, it shoots off exactly how it should.
Same as when I set the gravity = (0, 10), but I don't want gravity and I don't want an initial velocity! SetLinearVelocity(0, 0) initially doesn't work either. If I get it working, let it stop and try to launch again, it does not work.

This is why I think it's something to do with something inside Box2D. But I can't figure out what. What really annoys me is that I'm tracing the position and velocity, and the velocity is constant non-zero and the position stays constant! How can this be? Help please...

BTW I have connected the body to a movieclip from the library, and it's not the "m_timeStep bug".

Here's the code

Code: Select all

package {

   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.display.MovieClip;

   // Classes used in this example
   import Box2D.Dynamics.*;
   import Box2D.Collision.*;
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;

   public class PokerChips extends Sprite {

      var chip:b2Body;
      var speedX:Number;
      var speedY:Number;
      var launched:Boolean;
      public function PokerChips() {

         launched = false;
         speedX = speedY = 0;
         // Add event for main loop
         addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
         addEventListener(MouseEvent.MOUSE_DOWN, MD);



         // Creat world AABB
         var worldAABB:b2AABB = new b2AABB();
         worldAABB.lowerBound.Set(-100.0, -100.0);
         worldAABB.upperBound.Set(100.0, 100.0);

         // Define the gravity vector
         var gravity:b2Vec2 = new b2Vec2(0.0, 0.0);

         // Allow bodies to sleep
         var doSleep:Boolean = true;

         // Construct a world object
         m_world = new b2World(worldAABB, gravity, doSleep);

         // set debug draw

         // Vars used to create bodies
         var body:b2Body;
         var bodyDef:b2BodyDef;
         var boxDef:b2PolygonDef;
         var circleDef:b2CircleDef;

         var radius:Number = 25/30;
         bodyDef = new b2BodyDef();
         bodyDef.position.x = 5;
         bodyDef.position.y = 5;
         circleDef = new b2CircleDef();
         circleDef.radius = radius;
         circleDef.density = 1.0;
         circleDef.friction = 0.1;
         circleDef.restitution = 1;
         bodyDef.userData = new Chip();
         bodyDef.userData.width = radius*2*30;
         bodyDef.userData.height = radius*2*30;
         bodyDef.userData.gotoAndStop(1);
         bodyDef.linearDamping = 0.5;
         bodyDef.angularDamping = 0.5;
         chip = m_world.CreateBody(bodyDef);
         chip.CreateShape(circleDef);
         chip.SetMassFromShapes();
         addChild(bodyDef.userData);

      }
      public function MD(e:MouseEvent) {
         if (!launched) {
            launched = true;
            launch();
         }
      }
      function launch() {
         speedX = mouseX/30-chip.GetPosition().x;
         speedY = mouseY/30-chip.GetPosition().y;
         chip.SetLinearVelocity(new b2Vec2(speedX*3, speedY*3));
      }
      public function Update(e:Event):void {

         m_world.Step(m_timeStep, m_iterations);

         if (chip.IsSleeping()) {
            launched = false;
         }
         if (!launched) {
            // irrelevant
         } else {
            // irrelevant
         }
         // Go through body list and update sprite positions/rotations
         for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next) {
            if (bb.m_userData is Sprite) {
               bb.m_userData.x = bb.GetPosition().x * 30;
               bb.m_userData.y = bb.GetPosition().y * 30;
               bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
            }
         }
      }
      public var m_world:b2World;
      public var m_iterations:int = 10;
      public var m_timeStep:Number = 1.0/30.0;

   }

}
Last edited by Kabomb on Sat May 23, 2009 4:01 am, edited 1 time in total.

android
Posts: 280
Joined: Fri May 16, 2008 2:52 pm
Contact:

Re: Object not moving mystery...

Postby android » Sat May 23, 2009 3:05 am

When you 'launch' the ball, don't forget to wake it up first. Bodies that don't move are put to sleep by box2d to increase performance.

Code: Select all

chip.WakeUp()

Kabomb
Posts: 2
Joined: Sat May 23, 2009 2:17 am

Re: Object not moving mystery...

Postby Kabomb » Sat May 23, 2009 4:02 am

android wrote:When you 'launch' the ball, don't forget to wake it up first. Bodies that don't move are put to sleep by box2d to increase performance.

Code: Select all

chip.WakeUp()


Oh that makes sense...

Cheers man.


Return to “Flash”



Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests