Box2d Flash AS3 (1.4.3 released)

Ygryggil wrote:

Code:
public function DrawDisplayObject(bb:b2Body):void
{
         // get actor
         var dispObj:DisplayObject = DisplayObject(bb.m_userData);
         if(dispObj==null){return;}         
         // add actor if needed
         if(!m_sprite.getChildByName(dispObj.name)){
            m_sprite.addChild(dispObj);
         }
         // position actor
         dispObj.x = bb.m_position.x * m_physScale;
         dispObj.y = bb.m_position.y * m_physScale;
         //dispObj.rotation = bb.m_rotation;
         dispObj.rotation = bb.m_rotation * (180.0 / Math.PI);
}



Where should the DrawDisplayObject function be called? I did the following:
Code:
body = m_world.CreateBody(bd);
body.m_userData = new Actor("char", myDisplayObject);
DrawDisplayObject(body);


My displayObject appears, but the line figure of the body also (apparently) appears, because moving the body, moves only the line figure. The DisplayObject seems to be fixed.


edit: I've solved my problem. For anyone else wondering about the same problem, I edited the end of the Update function:
Code:
                                  // bodies
         for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next)
         {
            if(bb.m_userData != null)
            {
               DrawDisplayObject(bb);
            }
            else
            {
               for (var s:b2Shape = bb.GetShapeList(); s != null; s = s.GetNext())
               {
                  DrawShape(s);
               }
            }
         }
Hey everyone, this is a great project! I started playing with it and it seems to be super cool! In skatehead's TestBed (Test.as) there is little section of code that defines the boundaries of the world:
Code:
// Create border of boxes
         var wallSd:b2BoxDef = new b2BoxDef();
         var wallBd = new b2BodyDef();
         wallBd.AddShape(wallSd);
         
         // Left
         wallSd.extents.Set(100/m_physScale, 400/m_physScale/2);
         wallBd.position.Set(-95 / m_physScale, 360/m_physScale/2);
         m_world.CreateBody(wallBd);
         // Right
         wallBd.position.Set((640+95) / m_physScale, 360/m_physScale/2);
         m_world.CreateBody(wallBd);
         // Top
         wallSd.extents.Set(680/m_physScale/2, 100/m_physScale);
         wallBd.position.Set(640/m_physScale/2, -95/m_physScale);
         m_world.CreateBody(wallBd);
         // Right
         wallBd.position.Set(640/m_physScale/2, (360+95)/m_physScale);
         m_world.CreateBody(wallBd);

I'm just having a difficult time understanding how to manipulate these border boxes. Every time I change a number it does something I totally don't understand. Ultimately what I want to do is make the area bigger and also make it respond to stage scaling. Any advice or help would be great! Thanks!

-Chris
Oh... Nevermind... I see now that it's in Vectors and you define the Box size then set it's position. Here's what I'm working on: http://chrispaterson.net/projects/regime/itlooksgood/
Ahaha, that's really cool!

Glad you got it figured out.

-Matt
Yeah it's fun! How do I remove a shape? I got AddShape(BoxDefinition) but how would I remove it?
I just noticed that the PhysTest.fla has a frame rate of 120 fps. The flash player uses 90% of CPU power continuously (as seen in the task manager). Normal flash movies and games run at 12 fps, if I'm not mistaken.

What's the guarantee that the swf will run smoothly on older computers?
t_anjan wrote:
I just noticed that the PhysTest.fla has a frame rate of 120 fps. The flash player uses 90% of CPU power continuously (as seen in the task manager). Normal flash movies and games run at 12 fps, if I'm not mistaken.

What's the guarantee that the swf will run smoothly on older computers?


I don't think many games run at 12fps -- that's just the default setting in the authoring environment. I always use 60,90, or 120fps and use code internally to keep the game running as smoothly as possible.
how can I apply force or impulse on a body?
for example I want to shoot a ball from a gun, and for doing that I need to apply a force to the ball
I tried the following:
Body.ApplyForce(new b2Vec2 (100,100),new b2Vec2(300,100));
my ball is at position 300:100
what am i doing wrong?
(i tried to use other values, for example 0,0 for the second parameter, but nothing worked)
I think I remember reading that you have to call wakeup() first.
It might be a good idea to have the applyforce function call wakeup body
internally since it seems the user would always want the body to respond
even if it's sleeping?
Mike
mike950 wrote:
I think I remember reading that you have to call wakeup() first.

yes, I called wakeup

here's the code:

74 B.Body.WakeUp();
77 B.Body.ApplyImpulse(new b2Vec2(1000,1000),new b2Vec2(320,40));
78 B.Body.ApplyForce(new b2Vec2(1000,1000),new b2Vec2(320,40));
79 B.Body.ApplyImpulse(new b2Vec2(1000,1000),new b2Vec2(0,0));
80 B.Body.ApplyForce(new b2Vec2(1000,1000),new b2Vec2(0,0));
81 B.Body.ApplyImpulse(new b2Vec2(320,40),new b2Vec2(1000,1000));
82 B.Body.ApplyForce(new b2Vec2(320,40),new b2Vec2(1000,1000));
83 B.Body.ApplyImpulse(new b2Vec2(320,40),new b2Vec2(1000,1000));
84 B.Body.ApplyForce(new b2Vec2(320,40),new b2Vec2(1000,1000));
85 B.Body.WakeUp();

you can see, I've tried everything 😀
but nothing happens 🙁

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