SmilyOrg / quickb2

Automatically exported from code.google.com/p/quickb2
0 stars 0 forks source link

Please implement Ray Casting #24

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Can't get Box2DAS.RayCast to work in QB2 world, don't really know why, so 
please make a QB2 Ray Cast as soon as possible.

Cheers

Original issue reported on code.google.com by t.wisnie...@gtempaccount.com on 19 May 2011 at 11:12

GoogleCodeExporter commented 9 years ago
A workaround you can do for now is this:

Do a raycast on the box2d world, then read the "userData" of the box2d objects 
returned.  The userDatas point to the QuickB2 objects.

Raycasts aren't implemented in QuickB2 yet because I haven't thought of an 
elegant way to work them into QuickB2's event-driven framework.

Original comment by doug...@gmail.com on 19 May 2011 at 2:54

GoogleCodeExporter commented 9 years ago
I can't get Box2D RayCast to work, there is no error thrown, so I guess my 
callback is not good somehow, could you please post a snippet of how it should 
be done?

Original comment by t.wisnie...@gtempaccount.com on 20 May 2011 at 6:35

GoogleCodeExporter commented 9 years ago
Mhmm, does qb2world operate on 30 times greater scale then b2_world ?

Original comment by t.wisnie...@gtempaccount.com on 21 May 2011 at 12:34

GoogleCodeExporter commented 9 years ago
Yes, qb2World::pixelsPerMeter is the conversion.

I've never actually used raycasting before, so your guess is as good as mine.  
If you have issues, I'd suggest looking around in the box2d forums as well.

Original comment by doug...@gmail.com on 22 May 2011 at 4:56

GoogleCodeExporter commented 9 years ago
I got it to work, but it's hard to use with qb2, ray casting ends on qb2 
objects like sensors, terrain, effect fields. but got it work work here's a 
part of the raw unoptimized code:

        public static function RayCast(world2d:qb2World, caster:qb2Object, p1:amPoint2d, p2:amPoint2d):Object{
            var rFixture:b2Fixture;
            var rPoint:amPoint2d = p2.clone();
            var rNormal:amVector2d;
            var rFraction:Number = 1;
            var rQB2:*;
            var hits:Number = 0;
            function cb(fixture:b2Fixture, point:V2, normal:V2, fraction:Number):Number
            {
                trace("RayCast hit", hits, fraction, fixture.m_userData);
                hits++;
                var t:qb2PolygonShape = fixture.m_userData as qb2PolygonShape;
                if (t){
                    // skip all but desired bodies, and caster body to allow raycasting from within body, id this callback returns 1 then RayCast function scans for other bodies,
                    if (t.parent is tdDestroyable && t.parent != caster){
                        if (rFraction > fraction){
                            rFixture = fixture;
                            rPoint = new amPoint2d(point.x*world2d.pixelsPerMeter, point.y*world2d.pixelsPerMeter);
                            rNormal = new amVector2d(normal.x, normal.y);
                            rFraction = fraction;
                            rQB2 = fixture.m_userData;
                        }
                    }else{
                        return 1;
                    }

                }
                return fraction;
            }
            var out:Object;
            world2d.b2_world.RayCast(cb, new V2(p1.x/world2d.pixelsPerMeter,p1.y/world2d.pixelsPerMeter), new V2(p2.x/world2d.pixelsPerMeter,p2.y/world2d.pixelsPerMeter));
            out = {p1:p1, p2:p2, f:rFraction, fixture:rFixture, point:rPoint, normal:rNormal, qb2:rQB2};
            return out;
        }

Original comment by t.wisnie...@gtempaccount.com on 22 May 2011 at 9:58

GoogleCodeExporter commented 9 years ago
Cool, thanks for the code.

I'll definitely reference this when I get around to designing the raycasting 
API.

sensors, terrains, and effect fields all use the "isSensor" property in the 
box2d object (b2Fixture I think), so that could be the problem.

Original comment by doug...@gmail.com on 23 May 2011 at 2:04