sprite not acting as a costume on my body

Hi I am using box2d and I have a circle shape, I am trying to get a sprite to act as a costume to mask the body, but the sprite doesent move, only inserted in the world. I have an actor class and a ballActor that extends actor.

Actor:

Code: Select all

      public function Actor(myBody:b2Body, myCostume:DisplayObject)
      {
         _body = myBody;
         _body.SetUserData(this);
         _costume = myCostume;
         
         updateMyLook();
         
      }


...

Code: Select all

private function updateMyLook():void
      {
         _costume.x = _body.GetPosition().x * PhysiVals.RATIO;
         _costume.y = _body.GetPosition().y * PhysiVals.RATIO;
         _costume.rotation = _body.GetAngle() * (180 / Math.PI);
      }


ballActor:

Code: Select all

public var fixtureDef:b2FixtureDef = new b2FixtureDef();
      
      public function ballActor(parent:DisplayObjectContainer ,location:Point)
      {
         
         var fred:Sprite = new Fred();
         fred.scaleX = BALL_SIZE / fred.width;
         fred.scaleY = BALL_SIZE / fred.height;
         parent.addChild(fred);
         
         var wheelShape:b2CircleShape = new b2CircleShape(BALL_SIZE / PhysiVals.RATIO);
         
         fixtureDef.shape = wheelShape;
         fixtureDef.density=1;
            fixtureDef.friction=5;
            fixtureDef.density = 0.1;
         
          var wheel:b2Body;
         var BodyDef:b2BodyDef = new b2BodyDef();
         
         BodyDef.type=b2Body.b2_dynamicBody;
         BodyDef.allowSleep = false;
         BodyDef.position.Set(location.x / PhysiVals.RATIO, location.y / PhysiVals.RATIO);
         wheel = PhysiVals.world.CreateBody(BodyDef);
         wheel.CreateFixture(fixtureDef);
         
         
         super(wheel, fred);
         
      }


the problem that happens is when I add it to main class and add it to an array so I can call updateLook() in the new frameevent listenr:

Code: Select all

var ball:ballActor = new ballActor(_camera, new Point(240 / PhysiVals.RATIO, 20 / PhysiVals.RATIO));
         _allActors.push(ball);

the sprite and body are only added at the top left corner, not the point i ask it and more importantly **the sprite doesent move**, what am I doing wrong? Is there a more proper way to update the sprite location?
It's because you divided the location coordinates by PhysiVals.RATIO twice. When you make the new ballActor, you passed in a Point object as a location with coordinates that are divided by the ratio, and in the ballActor constructor, you divided by the ratio again.
jayther wrote:It's because you divided the location coordinates by PhysiVals.RATIO twice. When you make the new ballActor, you passed in a Point object as a location with coordinates that are divided by the ratio, and in the ballActor constructor, you divided by the ratio again.


thank you again for the help! This solves one problem, but now I have to solve the big actual problem, the costume sprite doesent follow the ballActor, it just stays there,

Code: Select all

this is the code in the newFrameEvent:

      private function newFrameEvent(e:Event):void
      {
          PhysiVals.world.Step(1.0 / 20.0, 10, 10);
         PhysiVals.world.ClearForces();
         PhysiVals.world.DrawDebugData();
         for each (var actor:Actor in _allActors) {
            actor.updateNow();
            trace("is this actually working?");
         };
      }


the trace statment works fine but the updateNow() method doesent seem to do anything, this is the code for that method in the actor class which ballActor extends:

Code: Select all

private function updateMyLook():void
      {
         _costume.x = _body.GetPosition().x * PhysiVals.RATIO;
         _costume.y = _body.GetPosition().y * PhysiVals.RATIO;
         _costume.rotation = _body.GetAngle() * (180 / Math.PI);
      }


thank you once again, I really appreciate your help!
Sorry if this is too obvious, but does actor.updateNow() actually call the actor's updateMyLook() function?
jayther wrote:Sorry if this is too obvious, but does actor.updateNow() actually call the actor's updateMyLook() function?


ah I completely forgot, I was just using things from other tutorials and update now is:

Code: Select all

      public function updateNow():void
      {
         if(!_body.IsActive()) {
         updateMyLook();
         }
         childSpecificUpdating();
         
      }


it turns out I was using a tutorial that used an older version and it used to be:

Code: Select all

      public function updateNow():void
      {
         if(! _body.IsStatic()) {
         updateMyLook();
         }
         childSpecificUpdating();
         
         
      }


sorry i forgot that crucial detail and thanks again
So it's fixed or is it not fixed?

In case it's not fixed (as just looking at that bit of code), it is supposed to be IsStatic(). IsStatic() doesn't exist in Box2D 2.1, so the equivalent would be:

Code: Select all

_body.GetType() == b2Body.b2_staticBody;


In your case, the code should look like this so it only updates the costume for bodies that are not static (i.e. dynamic and kinematic):

Code: Select all

      public function updateNow():void
      {
         if(_body.GetType() != b2Body.b2_staticBody) {
         updateMyLook();
         }
         childSpecificUpdating();
         
      }


The IsActive() function does not determine if the body is static or dynamic. Bodies can be deactivated (via SetActive(false)) so the calculations for that body isn't processed and nothing will collide with it until you set it active again.
ah sorry forgot to thank you but that worked! Thanks a bunch for the help

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