Box2D Forums

It is currently Tue May 21, 2013 7:57 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Feb 11, 2011 7:57 am 
Offline

Joined: Sat Mar 27, 2010 1:47 pm
Posts: 34
Hi! In own implementation of World (by extending it) I'm detecting by RayCast if there are some fixtures between 2 points:
Code:
            var f:b2Fixture;
            var b:b2Body;
            var p1:V2 = new V2(line.startPoint.x / this.scale, line.startPoint.y / this.scale);
            var p2:V2 = new V2(line.endPoint.x / this.scale, line.endPoint.y / this.scale);
            var bodies:Vector.<b2Body> = new Vector.<b2Body>();
            
            var cb = function(fixture:b2Fixture, point:V2, normal:V2, fraction:Number):Number{
               f = fixture;
               if (bodies.indexOf(f.GetBody()) == -1)
                  bodies.push(f.GetBody());
               return fraction;
            }
            b2world.RayCast(cb, p1, p2);
            trace("End of RC");
            trace(bodies.length);
            

Then I'm copying detected dynamic bodies to vector:
Code:
for (var i:int = 0; i < bodies.length; i++ ) {
               b = bodies[i];
               if (b.GetType() == b2Body.b2_dynamicBody)
                  bodiesToDestroy.push(b);
            }

After timestep doing this:
Code:
while (bodiesToDestroy.length > 0) {
            var b:b2Body = bodiesToDestroy.pop();
            if (b != null) {
               removeChild(b.GetUserData());
               b2world.DestroyBody(b);
               b = null;
            }
         }

It works fine until I delete the last dynamic body (or sometimes randomly?). The error is
Code:
Assertion failed: (m_bodyCount > 0), function DestroyBody, file ../Box2D/Dynamics/b2World.cpp, line 129.
[object AlchemyExit]
   at Function/<anonymous>()
   at Function/<anonymous>()
   at Box2DAS.Dynamics::b2Body/destroy()
   at Box2DAS.Dynamics::b2World/DestroyBody()
   at GWorld/step()
Cannot display source code at this location.

For some strange reason, RayCast isn't accurate. I can draw a line (with 2 points: startPoint and endPoint) above 2 bodies, but RayCast returns only one body. StartPoint is outside of body...


Top
 Profile  
 
PostPosted: Tue Feb 15, 2011 2:27 pm 
Offline

Joined: Fri Dec 14, 2007 8:07 pm
Posts: 913
Are you using WCK or just the raw alchemy port? If you're using WCK this will generate an error:

Code:
removeChild(b.GetUserData());
b2world.DestroyBody(b);


Because removing the BodyShape destroys the b2body automatically. You're trying to destroy the body twice.

Also, if there is an error in your ray cast callback function, it will fail silently. Alchemy catches the error and discards it. Might be why you're not getting both bodies in the ray cast.


Top
 Profile  
 
PostPosted: Tue Feb 15, 2011 2:42 pm 
Offline

Joined: Sat Mar 27, 2010 1:47 pm
Posts: 34
I am using WCK. Commenting DestroyBody() works. Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group