Problem with triangle skin.

Discuss issues specific to the Flash/AS3 port of Box2D
billyNugz
Posts: 4
Joined: Wed Sep 05, 2012 7:49 am

Problem with triangle skin.

Postby billyNugz » Wed Sep 05, 2012 8:10 am

Hey guys, I have a triangle which I create like so:

Code: Select all

public function draw_tri():void
      {
         //draw_box(250,100,100,100,true);
         var bodyDefP:b2BodyDef = new b2BodyDef();
         bodyDefP.type = b2Body.b2_dynamicBody;
         var polyShape:b2PolygonShape = new b2PolygonShape();
         var vertices:Array = new Array();
         var vertexCount:int;
         vertexCount = 3;
         for (var j:Number = 0; j < vertexCount; ++j)
         {
            vertices[j] = new b2Vec2();
         }
         
         /*vertices[0].Set(0, (17 +Math.random()*17) / scaleFactor);
         vertices[1].Set((-12 -Math.random()*17) / scaleFactor, (-17 -Math.random()*17) / scaleFactor);
         vertices[2].Set(( 12 +Math.random()*17) / scaleFactor, (-17 -Math.random()*17) / scaleFactor);*/
         vertices[0].Set(0,-0.8);
         vertices[1].Set(1,1);
         vertices[2].Set(-1,1);
         
         trace("firstSet = " + (17 +Math.random()*17) / scaleFactor);
         trace("secSet = " + (-12 -Math.random()*17)+ " & " + (-17 -Math.random()*17));
         trace("thirdSet = " + ( 12 +Math.random()*17)+ " & " + (-17 -Math.random()*17));
         /*vertices[0].Set(0, (20 / scaleFactor));
         vertices[1].Set((-20) / scaleFactor, (-17) / scaleFactor);
         vertices[2].Set(( 20) / scaleFactor, (-20) / scaleFactor);*/
         
         
         polyShape.SetAsArray( vertices, vertexCount );
         var fixtureDef:b2FixtureDef = new b2FixtureDef();
         fixtureDef.shape = polyShape;
         fixtureDef.density = 0.1;
         fixtureDef.friction = 0.1;
         fixtureDef.restitution = 0.8;
         //bodyDefP.position.Set((Math.random() * 400 + 120) / scaleFactor, (Math.random() * 150 + 50) / scaleFactor);
         bodyDefP.position.Set(75/ scaleFactor, 75/ scaleFactor);
         
         triBody = mainWorld.CreateBody(bodyDefP);
         triBody.CreateFixture(fixtureDef);
      }


And a clip I attach to the triangle like so:

Code: Select all

private function onTick(a_event:TimerEvent):void
      {
         //scaleFactor:Number = 20;
         graphics.clear();
         graphics.lineStyle(3, 0xff0000);
         //world.Step(0.025, 10, 10);//old
         world.Step(1 / 30, 10, 10);
         world.ClearForces();
         world.DrawDebugData();
         
         triClip.x = createShape.triBody.GetPosition().x*scaleFactor;
         triClip.y = createShape.triBody.GetPosition().y*scaleFactor;
         triClip.rotation = createShape.triBody.GetAngle()*scaleFactor;
         
         trace("TRI -Angle = " + createShape.triBody.GetAngle());
         trace("triClip -Angle = " + triClip.rotation);
         
      }


The problem is that the rotation from the clip and the box2d triangle (in debug draw) dont match. Also I was wondering if anybody would have any pointers on properly "skinning" triangles? As it stands now I just created a clip thats drawn close to the size of the shape I create in box2d and I dont know if that the best way to go about doing things.

My thanks in advance for any and all help.

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: Problem with triangle skin.

Postby jayther » Wed Sep 05, 2012 1:48 pm

You don't need to multiply the angle by the scale factor. Also, the angle from the Box2D body is in radians, so you'll need to convert it into degrees for the sprite's rotation.

As for properly skinning the triangles, there's really no shortcut to match the image to the Box2D triangle. If it's just a pattern, you can take the vertices from the Box2D triangle and use that for the graphics thing in sprites to draw the shape and fill pattern.

billyNugz
Posts: 4
Joined: Wed Sep 05, 2012 7:49 am

Re: Problem with triangle skin.

Postby billyNugz » Fri Sep 14, 2012 7:07 am

Thanks man,

Code: Select all

triClip.rotation = createShape.triBody.GetAngle()*(180/Math.PI);


That was the solution for me in case anybody is lurking.

billyNugz
Posts: 4
Joined: Wed Sep 05, 2012 7:49 am

Re: Problem with triangle skin.

Postby billyNugz » Fri Sep 14, 2012 10:08 am

I have another question about the scale of b2PolygonShapes and specifically my triangle. Basically I would like to easily scale up or down my triangle and the movie-clip that skins it. Im guessing I would have to redraw the triangle each time and set my clip to the appropriate scale based on the vertices but that seems like allot of trouble and Im hoping theres a better way. Again, any help would be great and my thanks in advance.


Return to “Flash”



Who is online

Users browsing this forum: No registered users and 4 guests