Box2D Forums

It is currently Fri May 24, 2013 8:19 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Dec 21, 2010 11:19 pm 
Offline

Joined: Thu Dec 16, 2010 8:56 pm
Posts: 4
Hey sorry this is a basic question, but I'm trying to get the body intersected with from a raycast, I used the sample on the testbed but I can't work out how to get it.

Here's what I'm doing:
the call back function here with m_body added:

Code:
class RayCastAnyCallback : public b2RayCastCallback
{
public:
   RayCastAnyCallback()
   {
      m_hit = false;
   }
   
   float32 ReportFixture(   b2Fixture* fixture, const b2Vec2& point,
                    const b2Vec2& normal, float32 fraction)
   {
      b2Body* body = fixture->GetBody();
      void* userData = body->GetUserData();
      if (userData)
      {
         int32 index = *(int32*)userData;
         if (index == 0)
         {
            // filter
            return -1.0f;
         }
      }
      
      m_hit = true;
      m_point = point;
      m_normal = normal;
      m_body = body;

      return 0.0f;
   }
   
   bool m_hit;
   b2Vec2 m_point;
   b2Vec2 m_normal;
   b2Body* m_body   //this is what I've added
};


and here is how I've implemented:
Code:
RayCastAnyCallback callback;
   world->RayCast(&callback, point1, point2);
   
   if (callback.m_hit)
   {
      NSLog(@"hit %@ ",callback.m_body);
         }


I'm doing this for an iphone app but I'm not savvy with c++ maybe that's the problem.

Thanks, Dave.


Top
 Profile  
 
PostPosted: Wed Dec 22, 2010 4:51 pm 
Offline

Joined: Mon Jul 27, 2009 6:46 am
Posts: 230
That all looks fine. In the testbed, m_body in the raycast is what is used to store the body. There are three different types of raycasts in the testbed, one of whom captures the first, and the other captures any of them, and the one that captures multiple points; I believe you used the second one.

Anyway, that all looks fine; is m_body NULL? I don't think you can print the body like you have shown here:

Code:
NSLog(@"hit %@ ",callback.m_body);


I don't remember %@ doing anything in C++, assuming that's what language you're using.

Is m_hit ever getting set to true?

-P


Top
 Profile  
 
PostPosted: Wed Dec 22, 2010 6:26 pm 
Offline

Joined: Thu Dec 16, 2010 8:56 pm
Posts: 4
Thanks for getting back,

It's being written as a cpp.objcpp file for iphone which uses both c++ and obj c. %@ is part of the obj-c to trace out an object.

without trying to access the body m_hit returns true, I tried changing m_body to hit_body but still doesn't work, it doesn't even return null, it just crashes.

Hey, sorted! trying to NSLog the b2Body was the problem, it does work fine, thanks. I imagine this would trip a bunch of iphone developers up.

Thanks again, Dave.


Top
 Profile  
 
PostPosted: Thu Dec 23, 2010 12:32 am 
Offline

Joined: Mon Jul 27, 2009 6:46 am
Posts: 230
Yeah, I didn't think the logging was working. Besides, if it was hitting the trace, then you know it was working, because the trace is only under the case where the body isn't NULL.

-P


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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