Intersection issue with box2d in iOS

Discuss issues specific to using Box2D on the iPhone
Biggerplay
Posts: 2
Joined: Thu Apr 10, 2014 6:14 am

Intersection issue with box2d in iOS

Postby Biggerplay » Mon Oct 06, 2014 8:16 am

I'm trying to use the code here

http://www.iforce2d.net/b2dtut/raycasting

So I can know whether a line cross a box2d object or not. It half works, in that when the line crosses the object it does show as an intersection, however, if you click before the object, it still shows as an intersection, as if it doesn't know the line stops before the object. From reading about this code, it should not do this.

Here's a screen shot of the issue.

[1]: http://i.stack.imgur.com/8GWZe.jpg

And here's the method I'm using for the check

-(b2Vec2)rayCheckWithInput:(b2Vec2)p1 andX:(b2Vec2)p2
{
b2RayCastInput input;
input.p1 = p1;
input.p2 = p2;
input.maxFraction = 1;

//check every fixture of every body to find closest
float closestFraction = 1; //start with end of line as p2
b2Vec2 intersectionNormal(0,0);
//for (b2Body* b = self.world.world->GetBodyList(); b; b = b->GetNext()) {
for (b2Fixture* f = box.body->GetFixtureList(); f; f = f->GetNext()) {

b2RayCastOutput output;
if ( ! f->RayCast( &output, input, 0 ) )
{
NSLog(@"Not Intersected");
continue;
}
if ( output.fraction < closestFraction ) {
closestFraction = output.fraction;
intersectionNormal = output.normal;

NSLog(@"Intersected");
NSLog(@"%f %f,", output.normal.x, output.normal.y);
NSLog(@"%f", output.fraction);
}
else{
NSLog(@"Intersected2");
NSLog(@"%f %f,", output.normal.x, output.normal.y);
NSLog(@"%f", output.fraction);

}
}
//}

b2Vec2 intersectionPoint = p1 + closestFraction * (p2 - p1);

NSLog(@"I point %f, %f", intersectionPoint.x, intersectionPoint.y);

return intersectionPoint;
}

I can't see how in that check I can tell if the click point is before (no intersection) or beyond (intersection) the bird. Any ideas?

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Intersection issue with box2d in iOS

Postby irresistible force » Tue Oct 07, 2014 5:55 pm

If the value of closestFraction is 1, then there was no intersection.


Return to “iPhone”



Who is online

Users browsing this forum: No registered users and 1 guest