b2EdgeShape

Discuss issues specific to the Flash/AS3 port of Box2D
brunoimbrizi
Posts: 10
Joined: Thu Feb 14, 2008 12:11 pm

b2EdgeShape

Postby brunoimbrizi » Wed May 16, 2012 10:27 am

How is b2EdgeShape supposed to be used in Box2DFlashAS3 2.1a?
Searching the forum I found code samples (cpp, monkey) where edge shapes are used just like any other shape (circle, box), but I'm getting an error when I try to do the same with Flash.

Here is a quick copy-paste-run code to reproduce the error:

Code: Select all

package 
{
   import Box2D.Collision.Shapes.*;
   import Box2D.Common.Math.*;
   import Box2D.Dynamics.*;
   import flash.display.Sprite;
   
   public class MainEdgeShape extends Sprite
   {
      private var _world:b2World;
      public static var WORLD_SCALE:Number = 1 / 30;
      
      public function MainEdgeShape()
      {
         _world = new b2World(new b2Vec2(0, 10.0), false);
         createEdge();
      }
      
      private function createEdge():void
      {
         var bodyDef:b2BodyDef = new b2BodyDef();
         bodyDef.position.Set(10 * WORLD_SCALE, 10 * WORLD_SCALE);
         
         var edge:b2EdgeShape = new b2EdgeShape(new b2Vec2(50 * WORLD_SCALE, 50 * WORLD_SCALE), new b2Vec2(30 * WORLD_SCALE, 30 * WORLD_SCALE));
         //var edge:b2CircleShape = new b2CircleShape(20 * WORLD_SCALE); // b2CircleShape compiles fine
         
         var fixtureDef:b2FixtureDef = new b2FixtureDef();
         fixtureDef.shape = edge;
         fixtureDef.density = 1;
         
         var body:b2Body = _world.CreateBody(bodyDef);
         body.CreateFixture(fixtureDef);
      }
   }
}


Runtime error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Box2D.Dynamics::b2Fixture/http://www.box2d.org/ns/b2internal::CreateProxy()[C:\Documents\...\src\Box2D\Dynamics\b2Fixture.as:316]
at Box2D.Dynamics::b2Body/CreateFixture()[C:\Documents\...\src\Box2D\Dynamics\b2Body.as:129]
at MainEdgeShape/createEdge()[C:\Documents\...\src\MainEdgeShape.as:32]
at MainEdgeShape()[C:\Documents\...\src\MainEdgeShape.as:16]

Line 316 of b2Fixture.as is:

Code: Select all

m_shape.ComputeAABB(m_aabb, xf);


Am I missing something?

brunoimbrizi
Posts: 10
Joined: Thu Feb 14, 2008 12:11 pm

Re: b2EdgeShape

Postby brunoimbrizi » Mon May 21, 2012 10:45 am

After a bit more investigation my conclusion is that b2EdgeShape cannot be used in Box2DFlashAS3 2.1a.

m_shape is null on b2Fixture.as line 316

Code: Select all

m_shape.ComputeAABB(m_aabb, xf);


... because b2EdgeShape doesn't implement the method Copy() which is called on b2Fixture.as line 291:

Code: Select all

m_shape = def.shape.Copy();


Even if we implement a very rough Copy() just to get passed the error

Code: Select all

public class b2EdgeShape extends b2Shape
{
   override public function Copy():b2Shape
   {
      return this;
   }


... it still fails on b2DistanceProxy.as line 60, because there is no b2Shape.e_edgeShape in the switch/case.
I added something rather similar to this in b2Distance.cpp

Code: Select all

case b2Shape.e_edgeShape:
{
   var edge:b2EdgeShape = shape as b2EdgeShape;
   m_vertices = new Vector.<b2Vec2>(1, true);
   m_vertices[0] = edge.m_v1;
   m_vertices[1] = edge.m_v2;
   m_count = 1;
   m_radius = edge.m_radius;
}


... which dismisses the error, but it doesn't work in the simulation of course.

---
Parts of b2EdgeShape are there, but there are some bits missing. I wonder if there is any way of making it work in this version of Box2DFlashAS3.
If there is, please let me know.

Thanks.

Jon
Posts: 19
Joined: Sun Nov 23, 2008 1:58 am

Re: b2EdgeShape

Postby Jon » Sat Dec 08, 2012 12:39 pm

Bruno - check out the post you made on the NME forums.
http://www.haxenme.org/community/forums ... edgeshape/

I haven't gotten it fully working. However, as you may have found it by now, the problem is that the implementations are left as TODO's in Box2DFlash, so the "fix" is to port over an implementation from a later version of Box2D (the commented out code is NOT suitable for porting over).

I got the Edge<->Circle case working several days ago. Less lucky with the Edge<->Polygon case, which I ported but does not register collisions. See the other post for details.

Any help would be much appreciated.


Return to “Flash”



Who is online

Users browsing this forum: No registered users and 3 guests