Reputation: 35
I have a top-down view tank project written in C++(QT). The tank consists of a "base" which is connected through a revjoint to the "turret" which can rotate and shoot. Turret is a circle shape and a rectangle attached to it as the barrel. Quite basic.
When a projectile is shot, its initial velocity vector should be added to the velocity vector of the tip of the barrel, otherwise the shot is unrealistic if the tank or the barrel moves. I can get only the velocity vector of a body, and it is not enough here, since the velocity vector of the tip of the barrel is a different thing then the velocity vector of the turret body.
I tried to weldjoint a fixtureless body to the tip of the barrel as a "muzzle". The muzzle body velocity vector is exactly the one I'm looking for. It didn't work though, the welded fixtureless body constrained the turret from rotating for some reason.
Then I did the same with a body with a fixture. It works, but seems overcomplicated solution, brings in new issues.
My questions are:
Is there any issues with fixtureless bodies I'm not aware of? Why I can't them just weld somewhere for easily accessing the speed vector of that point?
Is there any "standard" solution to get the velocity vector of an arbitrary point of a fixture?
Daniel
Upvotes: 1
Views: 514
Reputation: 35
I'm answering my own question after some research.
If a body is used without any fixtures, be sure to set some mass to it manually, otherwise it'll behave unexpected. After doing so, the muzzle body's paramaters were exactly I was about using them. Here's how to set mass to a body without any fixtures.
b2MassData md;
md.center = b2Vec2(0,0);
md.I = 0.000001f;
md.mass = 0.000001f;
body->SetMassData(&md);
An other solution might be the one iforce2d posted. Thanks for that, I'll check it out!
Upvotes: 0
Reputation: 8262
I think the function you are looking for is b2Body::GetLinearVelocityFromWorldPoint https://code.google.com/p/box2d/source/browse/trunk/Box2D/Box2D/Dynamics/b2Body.h#273
You can get the 'world point' of the end of the gun barrel with b2Body::GetWorldPoint.
To find the direction of the gun in world coordinates, b2Body::GetWorldVector might be useful.
Upvotes: 1