You can use a ray cast or b2Distance.
one body see another?
General DiscussionBabelduo wrote:
ok thanks... what do you mean with "ray cast" ?
and how can i use a b2Distance Join to check if there are other
bodies between my two bodies?
thx... babel
and how can i use a b2Distance Join to check if there are other
bodies between my two bodies?
thx... babel
Raycast along your joint. I suggest you go to the api and search for "ray" or something.
The function you're looking for is b2Shape::TestSegment(), though you'll need to use b2World::Query() to collect the relevant shapes. It's not hard, just a bit involved. Given a test segment (segment), collision category bits (mCategoryBits) and mask bits (mMaskBits):
There may be a better way to do this, but this is how I handled laser beams.
Code:
// get nearby shapes
b2AABB aabb;
aabb.lowerBound.Set(std::min(segment.p1.x, segment.p2.x) - 1, std::min(segment.p1.y, segment.p2.y) - 1);
aabb.upperBound.Set(std::max(segment.p1.x, segment.p2.x) + 1, std::max(segment.p1.y, segment.p2.y) + 1);
const int32 maxCount = 256;
b2Shape* shapes[maxCount];
int32 count = world->Query(aabb, shapes, maxCount);
// impact point
float lambda = 1.0f;
b2Vec2 normal(0.0f, 0.0f);
// for each shape...
for (int32 i = 0; i < count; ++i)
{
// skip unhittable shapes
if (shapes[i]->IsSensor())
continue;
if ((shapes[i]->GetFilterData().maskBits & mCategoryBits) == 0)
continue;
if ((shapes[i]->GetFilterData().categoryBits & mMaskBits) == 0)
continue;
// get the parent body
b2Body* body = shapes[i]->GetBody();
// ray-test
if (!shapes[i]->TestSegment(body->GetXForm(), &lambda, &normal, segment, lambda))
continue;
// ray collided with something...
// (do something here)
}
b2AABB aabb;
aabb.lowerBound.Set(std::min(segment.p1.x, segment.p2.x) - 1, std::min(segment.p1.y, segment.p2.y) - 1);
aabb.upperBound.Set(std::max(segment.p1.x, segment.p2.x) + 1, std::max(segment.p1.y, segment.p2.y) + 1);
const int32 maxCount = 256;
b2Shape* shapes[maxCount];
int32 count = world->Query(aabb, shapes, maxCount);
// impact point
float lambda = 1.0f;
b2Vec2 normal(0.0f, 0.0f);
// for each shape...
for (int32 i = 0; i < count; ++i)
{
// skip unhittable shapes
if (shapes[i]->IsSensor())
continue;
if ((shapes[i]->GetFilterData().maskBits & mCategoryBits) == 0)
continue;
if ((shapes[i]->GetFilterData().categoryBits & mMaskBits) == 0)
continue;
// get the parent body
b2Body* body = shapes[i]->GetBody();
// ray-test
if (!shapes[i]->TestSegment(body->GetXForm(), &lambda, &normal, segment, lambda))
continue;
// ray collided with something...
// (do something here)
}
There may be a better way to do this, but this is how I handled laser beams.
kdmiller3 wrote:
There may be a better way to do this, but this is how I handled laser beams.
You have laser beams in your game? I definitely wanna see that. Instant raytraced weapons would be cool but it would also be hard to make it produce the same kind of fun and "impact" that explosions offer.
I think the ray racing is a lot more valuable for such things as AI routines. Enemies could scan the area around them and figure out their paths. I thought about doing dijkstra's on a graph but that means updating/generating a graph. somewhat difficult.
I just added them earlier this week for Shmup-Dev's second rapid prototype session, though I haven't committed the changes to the SVN repository. I'm still working on polishing up extended-duration beams, but instantaneous beams work pretty well.
kdmiller3 wrote:
I just added them earlier this week for Shmup-Dev's second rapid prototype session, though I haven't committed the changes to the SVN repository. I'm still working on polishing up extended-duration beams, but instantaneous beams work pretty well.
Also theres the problem of how much range are you going to give them.. if something's just barely off the screen and doesnt get damaged one bit but is blown to smithereens once it comes close, it's kinda iffy.
But the idea of a giant bolt of energy that carves things up is definitely cool. I'm almost done with my dynamic breakage code and I must say the results are nothing short of spectacular. It works excellently with collision callbacks and is quite hard to crash (in fact I have a couple trivial bugs that I can't track down because of how rarely they occur, and running in debug mode = massive slowdown way before I can trigger one). If I can trigger them robustly using collision forces, having weapons doing it will be a piece of cake.
Crap i've hijacked the thread.
Beam range is configurable, and a sudden stop is no worse than regular bullets so I don't mind.
Getting back to the topic at hand... 🙂
Occlusion tests are useful for all kinds of things, and it'd be nice if the TestSegment functionality was integrated into b2World. I don't know how it should handle multiple shapes intersecting the segment, though. There is no one "right" answer to that.
Getting back to the topic at hand... 🙂
Occlusion tests are useful for all kinds of things, and it'd be nice if the TestSegment functionality was integrated into b2World. I don't know how it should handle multiple shapes intersecting the segment, though. There is no one "right" answer to that.
kdmiller3 wrote:
Occlusion tests are useful for all kinds of things, and it'd be nice if the TestSegment functionality was integrated into b2World. I don't know how it should handle multiple shapes intersecting the segment, though. There is no one "right" answer to that.
What would one do currently to test a ray for collisions? Loop through all b2shapes?
Babelduo wrote:
thx for your answers... but i currently dont understand how i can use
TestSegment...
i have to Points! the position of BODY1 and the position of BODY2
how can i check with the function TestSegment if those bodies see each other?
TestSegment...
i have to Points! the position of BODY1 and the position of BODY2
how can i check with the function TestSegment if those bodies see each other?
send the positions of body 1 and body 2 to testsegment, and run testsegment on all other bodies in box2d (loop through them using b2world::getBodyList()) with a lambda of 1.
The segment is what you need to fill in. In your case, you'd use the positions of BODY1 and BODY2 because you're trying to see if anything got in the way. You'd also want to ignore any intersection with shapes belonging to BODY1 or BODY2 since you expect those. 🙂
By the way, you can ignore the bits test parts (or use 0xFFFF) if you don't use collision and mask bits. I happen to use them a lot, but you might not need them.
By the way, you can ignore the bits test parts (or use 0xFFFF) if you don't use collision and mask bits. I happen to use them a lot, but you might not need them.
Original thread: https://box2d.org/forum/viewtopic.php?t=1045