Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411423 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 19, 2024, 12:37:08 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Box2d joints still attached after Explosion simulation
Pages: [1]
Print
Author Topic: Box2d joints still attached after Explosion simulation  (Read 1048 times)
umen
Level 0
**


View Profile
« on: May 10, 2015, 03:36:40 AM »

I ported simple Box2d Explosion simulation the function is called once when there is some event with the "Truck Car"
the problem is that the Explosion working fine but it looks like when the explosion done the "parts" of the car Returning to
the initial car position instead of bean scatter on the floor.
what am i doing wrong here ?

here is the code for the Explosion
Code:
class MyQueryCallback : public b2QueryCallback {
public:
std::vector<b2Body*> foundBodies;

bool ReportFixture(b2Fixture* fixture) {
foundBodies.push_back(fixture->GetBody());
return true;//keep going to find all fixtures in the query area
}
};
void BackgroundSprite::explode()
{
  float m_blastRadius = 40.0f;
float m_blastPower = 50.5f;
MyQueryCallback queryCallback;
b2Vec2 locationPnt = _truck->truckBody->GetPosition();
b2AABB aabb;
aabb.lowerBound.Set((locationPnt.x - m_blastRadius), (locationPnt.y - m_blastRadius));
aabb.upperBound.Set((locationPnt.x + m_blastRadius), (locationPnt.y + m_blastRadius));
m_world->QueryAABB(&queryCallback, aabb);

for (int i = 0; i < queryCallback.foundBodies.size(); i++) {
b2Body* pBody = queryCallback.foundBodies.at(i);
b2Vec2 pBodyPos = pBody->GetWorldCenter();
b2Vec2 pHitVector(pBodyPos.x - locationPnt.x, pBodyPos.y - locationPnt.y);
float radDist = pHitVector.Normalize();
radDist = (radDist * PTM_RATIO);
if ((pBody->GetType() == b2_dynamicBody) == (radDist <= m_blastRadius)){

float nHitForce = ((m_blastRadius - radDist) / m_blastRadius) * m_blastPower;
b2Vec2 appForce(pHitVector.x*nHitForce, pHitVector.y*nHitForce);
pBody->ApplyLinearImpulse(appForce, pBody->GetWorldCenter(),true);
}
}
 }
Logged
BorisTheBrave
Level 10
*****


View Profile WWW
« Reply #1 on: May 10, 2015, 12:54:49 PM »

You have to manually undo joints. (though I think there was a patch floating around for doing it automatically).

Either delete all the joints on affected objects, or delete any joints that have large reaction forces on them. The latter means you have to wait a step before you can delete the joints, which has an odd looking effect on the remaingin debris.
Logged
umen
Level 0
**


View Profile
« Reply #2 on: May 10, 2015, 07:20:37 PM »

Thanks for the replay , i trying to delete the joints but i got exception doing it.
Apparently there was still Physics going on while i tried to delete it .
how can i know when is the right time to delete  it ?
Logged
oahda
Level 10
*****



View Profile
« Reply #3 on: May 10, 2015, 11:34:48 PM »

Thanks for the replay , i trying to delete the joints but i got exception doing it.
Apparently there was still Physics going on while i tried to delete it .
how can i know when is the right time to delete  it ?

You can't modify stuff within the Box2D update loop. Run all simulations for the current frame first and perhaps within that loop you might have to set flags to indicate which objects should have their joints removed, or put them in a vector or something, and then after the simulation loop, go through those objects and remove their joints.
Logged

umen
Level 0
**


View Profile
« Reply #4 on: May 11, 2015, 01:37:46 AM »

This is exactly what im doing after the world->step(..)
i loop all the joints to remove with this function ( all joints pointer in vector )
Code:
for (int i = 0; i < carJointsVector.size(); i++){
_world->DestroyJoint(carJointsVector[i]);
}

but it throws exception
Logged
oahda
Level 10
*****



View Profile
« Reply #5 on: May 11, 2015, 03:23:55 AM »

Dunno if it matters since I wrote this such a long time ago now, but I have this after my step loop:

Code:
m_world.ClearForces();

You could try it out if you're not already doing that.
Logged

umen
Level 0
**


View Profile
« Reply #6 on: May 11, 2015, 03:42:03 AM »

ok thanks for the help , in the end the problem was that i updated other parts of the car after i removed the joints
so once fixed that every thing was working.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic