Box2D Forums

It is currently Thu May 23, 2013 4:09 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Fri Sep 03, 2010 12:05 pm 
Offline

Joined: Sat Aug 21, 2010 1:37 am
Posts: 36
Box2D is just way too much fun!

Working on IPhone, btw.

I'm trying to make it such that I can touch a fixture to make it active and then touch again later to make it inactive.

Init body and fixture:

Code:
b2BodyDef MyBodyDef;
MyBodyDef.type = b2_dynamicBody;
MyBodyDef.type = b2_dynamicBody;
MyBodyDef.position.Set(1050/PTM_RATIO, 220/PTM_RATIO);
MyBodyDef.userData = MySprite;
MyBody = _world->CreateBody(&MyBodyDef);

b2PolygonShape MyShape;
MyShape.SetAsBox(MySprite.contentSize.width/PTM_RATIO/2,MySprite.contentSize.height/PTM_RATIO/2);
b2FixtureDef MyShapeDef;
MyShapeDef.shape = &MyShape;
MyShapeDef.density = 1.0f;
MyShapeDef.friction = 1.0f;
MyShapeDef.restitution = 0.3f;
MyFixture = MyBody->CreateFixture(&MyShapeDef);


All works fine. But then I try in ccTouchEnded:

Code:
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {   
    CGPoint locationEndTouch = [touch locationInView: [touch view]];      

    if ( MyFixture->TestPoint(locationEndTouch) )
    {
     printf("fixture touched!");
    }
....

}


I get an error saying "no matching function for call to b2Fixture::TestPoint(CGPoint&)"

I checked b2Fixture.h and it's in there.

What am I doing wrong here?


Top
 Profile  
 
PostPosted: Sat Sep 04, 2010 12:59 am 
Offline
Site Admin

Joined: Thu Sep 06, 2007 12:34 am
Posts: 2931
Box2D doesn't know about CGPoint. TestPoint expects a b2Vec2.


Top
 Profile  
 
PostPosted: Tue Sep 21, 2010 2:35 pm 
Offline

Joined: Sat Aug 21, 2010 1:37 am
Posts: 36
Thanks for the reply.

I just got back to this feature and tried your suggestion.

I converted touch position to b2Vec2 & tried TestPoint again
Code:
CGPoint locationStartTouch = [touch locationInView: [touch view]];      
locationStartTouch = [[CCDirector sharedDirector] convertToGL: locationStartTouch];
b2Vec2 convertedTouch(locationStartTouch.x/PTM_RATIO,locationStartTouch.y/PTM_RATIO);

if(MyFixture->TestPoint(convertedTouch) ){
   printf("MyFixture touched!");
}



Never registers that MyFixture is touched when I touch it ...

I decided to printf some values for convertedTouch.
Top right edge of the screen (landscape mode) the values for convertedTouch are roughly x=14.9, y=9.8. Seem reasonable?

Also tried:
Code:
b2Transform transform;
transform.SetIdentity();
MyFixture->TestPoint(transform, convertedTouch);

But I get an error during compile:

:1279: error: no matching function for call to 'b2Fixture::TestPoint(b2Transform&, b2Vec2&)'

Perhaps I'm not using transform correctly?


Top
 Profile  
 
PostPosted: Wed Sep 22, 2010 8:59 am 
Offline
Site Admin

Joined: Thu Sep 06, 2007 12:34 am
Posts: 2931
You should just go to the Box2D code on your machine and look at the function interface.


Top
 Profile  
 
PostPosted: Wed Sep 22, 2010 11:29 am 
Offline

Joined: Sat Aug 21, 2010 1:37 am
Posts: 36
Quote:
You should just go to the Box2D code on your machine and look at the function interface.

That's usually the first thing I try (along with the manual & google search) and most of the time I get it myself.
This is my first attempt at programming so apologize if I come off annoying ...

I assume this is what you mean by the function interface?
in b2Fixture.h:
Code:
/// Test a point for containment in this fixture.
/// @param xf the shape world transform.
/// @param p a point in world coordinates.
bool TestPoint(const b2Vec2& p) const;
.
.
.
inline bool b2Fixture::TestPoint(const b2Vec2& p) const
{
   return m_shape->TestPoint(m_body->GetTransform(), p);
}


It would seem I could just pass the fixture's corresponding body to GetTransform ...

So I tried:
Code:
b2Transform trans = MyBody->GetTransform();
if( MyFixture->TestPoint(trans, convertedTouch) ) doSomething;

error: no matching function for call to 'b2Fixture::TestPoint(b2Transform&, b2Vec2&)'
Started trying different permutations of calls with no luck.

Searching the forum I see plenty of examples of doing something like this but I don't see what I'm doing wrong here.

This example does the same thing I do believe within the code snippet:
Code:
b2Transform trans = body->GetTransform();

http://www.box2d.org/forum/viewtopic.php?f=3&t=5582&p=26037&hilit=GetTransform#p26037

Again sorry for the noobish whining ...


Top
 Profile  
 
PostPosted: Wed Sep 22, 2010 12:03 pm 
Offline

Joined: Sat Aug 21, 2010 1:37 am
Posts: 36
Actually it DOES in fact work, sorta.

Code:
if(MyFixture->TestPoint(convertedTouch)){
printf("MyFixture touched!");
}


At first it only registers when I click the far right of screen.

My fixture is a car always centered at x=240 (actually the camera "follows" it). I achieve scrolling the rest of world with the camera.

If I then move the fixture (with camera following) in one direction the registered click area moves to where the fixture is but then passes it. Move back in opposite direction and it lines up again then passes.

Not sound corny but like 2 ships passing each other.

If I test touch point with a stationary fixture and scroll side to side the stationary fixture scrolls but the click area always remains at only one area of the screen.

Seems I DO need to do a transform of some sort so back to previous problem of why this:
Code:
b2Transform trans = MyBody->GetTransform();
if( MyFixture->TestPoint(trans, convertedTouch) ) {
printf("MyFixture touched!");
}

gives error: no matching function for call to 'b2Fixture::TestPoint(b2Transform&, b2Vec2&)'

Any clues because I think I'm doing it as others are. I AM passing the proper b2Transform and b2Vec2.

Aren't I?


Top
 Profile  
 
PostPosted: Wed Jul 11, 2012 11:51 am 
Offline

Joined: Wed Jul 11, 2012 11:38 am
Posts: 1
To use TestPoint(transformation, location) you need to access the TestPoint() from shape. So it need to be like this:

Code:
b2Transform trans = MyBody->GetTransform();
if( MyFixture->GetShape()->TestPoint(trans, convertedTouch) ) {
printf("MyFixture touched!");
}


Just add the GetShape() there and it should work. Just remember that you need to go through all the fixtures in the fixtureList. Hope fully some one finds this useful :)


Top
 Profile  
 
PostPosted: Wed Jul 11, 2012 10:12 pm 
Offline

Joined: Sun Oct 25, 2009 3:28 am
Posts: 242
You don't need to use the transform.

If the "camera" follows the car, then the convertedTouch also needs to be offset by the same offset you use to offset the camera. b2Vec2 does not automatically adjust along with the screen. When you use TestPoint(b2Vec2), it's testing in the physics world coordinates. So if you don't offset the b2Vec2 point to reflect the camera following the car, the b2Vec2 point will not coincide with the point you're touching.

It should be something like this (pseudocode):

Code:
//the cameraOffset values are in pixels, so include them in the PTM scaling.
b2Vec2 convertedTouch(
    (locationStartTouch.x + cameraOffset.x)/PTM_RATIO,
    (locationStartTouch.y + cameraOffset.y)/PTM_RATIO
);


You may need to subtract by the cameraOffset values instead of adding, depending on how the camera offsetting works. Also, is dividing by the PTM_RATIO correct? PTM means pixel to meter, and usually you multiply some pixel value to get a meter value. If the PTM_RATIO is less than 1.0, then you should be multiplying.


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

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 4 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