Example gragging object for XCode

I want to do an example abaut draggin objects, but I don't know how to do it.

I put you an example I want to do it.
http://www.emanueleferonato.com/2008/11 ... x2d-flash/
Under the source code there is an example of dragging a boxes.

Can anyone say to me how to do it?

Thank you.
You can use a mouse joint. Assuming you also use Cocos2D, this should work:

In your .h file add:

Code: Select all

   b2MouseJoint* mouseJoint;
   b2Body* aGroundBody;


In your .mm file add:

In -(id) init:

Code: Select all

   b2BodyDef aGroundBodyDef;
      aGroundBodyDef.position.Set(0, 0);
      aGroundBody = world->CreateBody(&aGroundBodyDef);


And now add these functions:

Code: Select all

class QueryCallback : public b2QueryCallback
{
public:
   QueryCallback(const b2Vec2& point)
   {
      m_point = point;
      m_fixture = NULL;
   }
   
   bool ReportFixture(b2Fixture* fixture)
   {
      b2Body* body = fixture->GetBody();
      if (body->GetType() == b2_dynamicBody)
      {
         bool inside = fixture->TestPoint(m_point);
         if (inside)
         {
            m_fixture = fixture;
            
            return false;
         }
      }
      
      return true;
   }
   
   b2Vec2 m_point;
   b2Fixture* m_fixture;
};

-(void) registerWithTouchDispatcher
{
   [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
}

-(BOOL)ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
{
       CGPoint touchZone = [touch locationInView:[touch view]];
   touchZone = [[CCDirector sharedDirector] convertToGL:touchZone];
   b2Vec2 p;
   p.Set(touchZone.x/PTM_RATIO, touchZone.y/PTM_RATIO);
   
   if (mouseJoint != NULL)
   {
      return NO;
   }
      
   b2AABB aabb;
   b2Vec2 d;
   d.Set(0.001f, 0.001f);
   aabb.lowerBound = p - d;
   aabb.upperBound = p + d;
   
   QueryCallback callback(p);
   world->QueryAABB(&callback, aabb);
   
   if(callback.m_fixture)
   {
      b2Body* body = callback.m_fixture->GetBody();
      b2MouseJointDef md;
      md.bodyA = aGroundBody;
      md.bodyB = body;
      md.target = p;
      md.maxForce = 1000.0f * body->GetMass();
      
      mouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
      body->SetAwake(true);
   
      return YES;
   }
   return YES;   
}

- (void)ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
       CGPoint touchZone = [touch locationInView:[touch view]];
   touchZone = [[CCDirector sharedDirector] convertToGL:touchZone];
   b2Vec2 p;
   p.Set(touchZone.x/PTM_RATIO, touchZone.y/PTM_RATIO);

   if (mouseJoint)
   {
         mouseJoint->SetTarget(p);
   }
}

- (void)ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event
{   
   if(mouseJoint)
   {
      world->DestroyJoint(mouseJoint);
      mouseJoint = NULL;
   }
}


Of course, that's just the Box2D code. In order to draw the Cocos2D sprite is place you'd have to reposition them in the tick() method.
Thank you, but I don't understand.

Can you put to me an example or download and prove it?
Here's a very simple example - just extract the zip file and open the Box2dMouseJoint.xcodeproj file: Download
Thank you, yyy, very much.

That's example is very good.

Can you put me more examples about explosions and effect dominated?

Thank you
Sorry but I'm very busy so I won't be able to create more examples for you. By the way, there are many examples in the "cocos2d-ios.xcodeproj" file which you can find in the Cocos2D folder.
and only an example of explosions.

Thank you
yyy, I will say thanks for taking the time to answer this. It is appreciated by most of us
very helpful. thanks for sharing.

Original thread: https://box2d.org/forum/viewtopic.php?t=7039