Box2D Forums

It is currently Mon May 20, 2013 8:04 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Sat Mar 12, 2011 10:43 am 
Offline

Joined: Mon Apr 26, 2010 5:35 am
Posts: 20
I am currently using a raycast in my game for a red laser sight on a weapon. It works pretty well but it is colliding with the sensors which are used throughout my levels. The code is almost identical to the testbed version, for a temporary solution i have added the line in below, but this causes the raycast to go through everything!

Code:
var cb = function(fixture:b2Fixture, point:V2, normal:V2, fraction:Number):Number{
   f = fixture;
   if (!f.IsSensor()) {
      lambda = fraction;
   }
   return fraction;
}


Any help is appreciated!


Top
 Profile  
 
PostPosted: Fri Mar 18, 2011 3:39 am 
Offline

Joined: Mon Apr 26, 2010 5:35 am
Posts: 20
Thought I would bump this :-)


Top
 Profile  
 
PostPosted: Sat Mar 19, 2011 5:06 pm 
Offline

Joined: Fri Dec 14, 2007 8:07 pm
Posts: 913
Huh. I changed the testbed so it has a sensor that it can ignore. My code is essentially the same as yours, but works. Take a look in the repository on github.

Code:
         var cb = function(fixture:b2Fixture, point:V2, normal:V2, fraction:Number):Number{
            f = fixture;
            if(f.IsSensor()) {
               return fraction;
            }
            lambda = fraction
            return fraction;
         }


Top
 Profile  
 
PostPosted: Mon Mar 21, 2011 2:36 am 
Offline

Joined: Mon Apr 26, 2010 5:35 am
Posts: 20
Thanks yes this is pretty much the same as mine. This doesnt work for my purpose however - I need the raycast to still collide with objects behind the sensor...

Like gun with laser >>>>>>>>> Sensor >>>>>>>> Wall

At the moment it would ignore the sensor, then ignore the wall also.


Top
 Profile  
 
PostPosted: Mon Mar 21, 2011 3:53 pm 
Offline

Joined: Fri Dec 14, 2007 8:07 pm
Posts: 913
Ok, you're right. I didn't test objects behind the sensor. it seems like half the time it can detect it and half the time it can't. Very strange... will have to look into.


Top
 Profile  
 
PostPosted: Tue Mar 22, 2011 2:07 am 
Offline

Joined: Mon Apr 26, 2010 5:35 am
Posts: 20
Thanks mayobutter :)


Top
 Profile  
 
PostPosted: Tue Mar 22, 2011 8:23 pm 
Offline

Joined: Tue Feb 24, 2009 4:10 pm
Posts: 564
Location: Michigan
**edit** this was my original misconception. my next post has it right.

I'm not sure if returning fraction is the right approach.

I've been testing b2World.as RayCast(); in the WCK framework.

the raycast would return multiple walls, but would seemingly draw the closest one if i graphics.clear() at the start of drawing on the callback function.

but would trace 3 sometimes 4 fixtures. and if i moved the graphics.clear() to the EnterFrame before the RayCast call, and made my lineStyle transparent, i could see that it was drawing each of the results.

simply returning 0 seems to stop further results. and leaves me with just the closest result.
returning 1 seemed to keep searching farther away, returning all possible results.

you may want to try returning 1 for sensors, and 0 for walls.

i hypothesize that returning a value other than 1, and 0, would be used if the RayCast hit an object that Amplified(greater than 1) or Depleted(greater than 0, less than 1) the range of the RayCast.

i was also pleased to see the Normal of the raycast easily accessible for creating reflections (new RayCasts along the normal) very exciting!

(side note, older RayCasts allowed you to pass a FilterFunction of sorts, but were not easy to learn to use)


Last edited by sketchbookgames on Tue Mar 22, 2011 8:46 pm, edited 2 times in total.

Top
 Profile  
 
PostPosted: Tue Mar 22, 2011 8:30 pm 
Offline

Joined: Tue Feb 24, 2009 4:10 pm
Posts: 564
Location: Michigan
oops i was making assumptions

http://www.box2d.org/manual.html#_Toc258082976

this was informative

return -1 to filter your contact.

also i need to reprogram mine to not assume the order of the callbacks are by distance.
this explains a lot of my problems :)

here is my new callback function;
i set closestFixture and closestPoint to null before i call the RayCast, and if they are still null afterwards i draw the full line.

Code:
protected function rcCallback(fixture:b2Fixture, point:V2, normal:V2, fraction:Number):Number{
   if(fixture.IsSensor()){
      return -1 //filter
   }
   
   //set new closest
   closestFixture = fixture;
   closestPoint = point;
   
   return fraction; //clips the RayCast shorter, and check for any closer results.
}



hope this helps.


Top
 Profile  
 
PostPosted: Wed Mar 23, 2011 8:07 am 
Offline

Joined: Mon Apr 26, 2010 5:35 am
Posts: 20
Brilliant this has fixed my problem, instead of returning fraction to filter the sensor I now just return -1 :D


Top
 Profile  
 
PostPosted: Tue Apr 02, 2013 5:57 am 
Offline

Joined: Mon Mar 23, 2009 5:52 am
Posts: 13
For ignoring sensors by raycast you just need to add in RayCast function (b2World.as class) this:

Code:
if (hit && !fixture.IsSensor())


instead of this:

Code:
if (hit)


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 0 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