collsions dont work

Hi,

I have a drawArea, where I can add dynamically shapes (circle, rectangle, polygon). This shapes should interact with each other. it means, that the shapes should collide by a hit. But that doesnt work.

Anyone have an idea where my problem is??

thanks niwe

in the following you see how I add the shapes and try to give them correct physics. I draw a sprite and give it to one of this methods...

addCircle method
Code:
private function addCircle(tShape:Sprite):void
         {
            var tDynamicBody:b2Body;
            
            var tCircle:b2CircleDef = new b2CircleDef();
            tCircle.radius = (tShape.width/2)/pPhysScale;
            tCircle.density = 1;
            tCircle.friction = 0.3;
         tCircle.restitution = 0.2
         
         var tMyBodyDef:b2BodyDef = new b2BodyDef();
         tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
         
         tDynamicBody = pWorld.CreateBody(tMyBodyDef);
         tDynamicBody.CreateShape(tCircle);
         tDynamicBody.SetMassFromShapes();
         
         pActor = new Actor(tDynamicBody, tShape);
         tDynamicBody.m_userData = pActor;
         pActors.push(pActor);
            drawArea.addChild(tShape);
         }


add rectangle method
Code:
private function addRectangle(tShape:Sprite):void
         {
            var tDynamicBody:b2Body;
            
            var tRect:b2PolygonDef = new b2PolygonDef();
         tRect.SetAsBox((tShape.width/2) / pPhysScale, (tShape.height/2) / pPhysScale);
         tRect.friction = 0.3;
         tRect.density = 1;
         tRect.restitution = 0.2;
         
         var tMyBodyDef:b2BodyDef = new b2BodyDef();
         tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
         
         tDynamicBody = pWorld.CreateBody(tMyBodyDef);
         tDynamicBody.CreateShape(tRect);
         tDynamicBody.SetMassFromShapes();
         
         pActor = new Actor(tDynamicBody, tShape);
         tDynamicBody.m_userData = pActor;
         pActors.push(pActor);
            drawArea.addChild(tShape);
         }


add polygon method
Code:
private function addConvexPolygon(tShape:Sprite, tVertices:Array):void
         {
            var tLong:int = tVertices.length;
            var tDynamicBody:b2Body;
            
            var tPoly:b2PolygonDef = new b2PolygonDef();
            tPoly.vertexCount = tLong;
            if(pDirection)
            {
               for(var i:int = 0; i<tLong;i++){
                  tPoly.vertices[i].Set(tVertices[i].x / pPhysScale, tVertices[i].y / pPhysScale);
               }
            }else{
               for(var j:int = tLong; j>0;j--){
                  tPoly.vertices[tLong-j].Set(tVertices[j-1].x / pPhysScale, tVertices[j-1].y / pPhysScale);
               }
            }
            tPoly.friction = 0.3;
         tPoly.density = 1.0;
         tPoly.restitution = 0.2;
         
         var tMyBodyDef:b2BodyDef = new b2BodyDef();
         tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
         
         tDynamicBody = pWorld.CreateBody(tMyBodyDef);
         tDynamicBody.CreateShape(tPoly);
         tDynamicBody.SetMassFromShapes();
         
         pActor = new Actor(tDynamicBody, tShape);
         tDynamicBody.m_userData = pActor;
            pActors.push(pActor);
            drawArea.addChild(tShape);
         }


Actor class
Code:
package
{
   import Box2D.Dynamics.b2Body;
   import flash.display.Sprite;
   
   public class Actor
   {
      private var pBody:b2Body;
      private var pGraphic:Sprite;
      
      public function Actor(b:b2Body, g:Sprite)
      {
         pGraphic = g;
         pBody = b;
      }
      
      public function get body():b2Body{
         return pBody;
      }
      public function get graphic():Sprite{
         return pGraphic;
      }

   }
}


update and movements of actors
Code:
   private function update(ev:Event):void{
             pFirstObjekt = false;
             pWorld.Step(pTimeStep, pIterations);
         moveActors();
          }
          
          private function moveActors():void
      {
         for each (var a:Actor in pActors)
         {
            var tBodyPosition:b2Vec2 = a.body.GetPosition();
               var tBodyRotation:Number = a.body.GetAngle();
               a.graphic.x = tBodyPosition.x * pPhysScale;
             a.graphic.y = tBodyPosition.y * pPhysScale;
              a.graphic.rotation = tBodyRotation * (180/Math.PI) % 360; //Don't forget to convert to degrees!
            }
      }
Your code looks fine actually.

Have you tried using DebugDraw too, to see whether the bodies & shapes are actually created?
thanks for the answer!! now I see, that the postions of the sprites and box2d bodys are totally different!!

is there an easy way to align the box2d bodys to the sprites?

regards
hi,

either the Box2D object is to far away from the sprite or the other way around.

the box2D object is always x = 0 and the sprite is on the right place
Code:
private function addCircle(tShape:Sprite, tX:Number, tY:Number):void
         {
            var tDynamicBody:b2Body;
            
            var tCircle:b2CircleDef = new b2CircleDef();
            tCircle.radius = (tShape.width/2)/pPhysScale;
            tCircle.density = 1;
            tCircle.friction = 0.3;
         tCircle.restitution = 0.2
         
         var tMyBodyDef:b2BodyDef = new b2BodyDef();
         tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
         //tMyBodyDef.position.Set(tX / pPhysScale, tY / pPhysScale);
         
         tDynamicBody = pWorld.CreateBody(tMyBodyDef);
         tDynamicBody.CreateShape(tCircle);
         tDynamicBody.SetMassFromShapes();
         
         
         pActor = new Actor(tDynamicBody, tShape);
         tDynamicBody.m_userData = pActor;
         pActors.push(pActor);
         trace("tShapeCoord", tShape.x, tShape.y);
            drawArea.addChild(tShape);
         }


the box2D form is on the correct place but not the sprite
Code:
/**
       * adds a circle to Box2D makes it moveable
       */
         private function addCircle(tShape:Sprite, tX:Number, tY:Number):void
         {
            var tDynamicBody:b2Body;
            
            var tCircle:b2CircleDef = new b2CircleDef();
            tCircle.radius = (tShape.width/2)/pPhysScale;
            tCircle.density = 1;
            tCircle.friction = 0.3;
         tCircle.restitution = 0.2
         
         var tMyBodyDef:b2BodyDef = new b2BodyDef();
         //tMyBodyDef.position.Set(tShape.x / pPhysScale, tShape.y / pPhysScale);
         tMyBodyDef.position.Set(tX / pPhysScale, tY / pPhysScale);
         
         tDynamicBody = pWorld.CreateBody(tMyBodyDef);
         tDynamicBody.CreateShape(tCircle);
         tDynamicBody.SetMassFromShapes();
         
         
         pActor = new Actor(tDynamicBody, tShape);
         tDynamicBody.m_userData = pActor;
         pActors.push(pActor);
         trace("tShapeCoord", tShape.x, tShape.y);
            drawArea.addChild(tShape);
         }


what can I do, that they are on the same place?

regards

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