How can I pin or stick a box2d body (circle or polygon shape) to the ground (body). Sorry if this sounds quite simple, I've not been able to find anything relevant on this. Please help.
Just use that body as a static body ..! And it wont move anywhere ..!
Or you can also create a weld joint between the body and the ground.
Related
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
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.
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!
How can I make a box2d body move along a prolate cycloid curve? I've searched extensively and haven't found anything leads. Below is an example of how the movement should look like. I would appreciate any help.
I'm struggling to learn and understand box2d. Here is my problem:
I'm trying to make a simple car, the car body looks like this for now:
Now I'm trying to add a wheel, I create the wheel body and I assume I'll link the wheel to the car body using a b2RevolutionJoint.
Until I get to that part I just want to be able to position the wheel in its place, so in relation to the bodyCar Body. How do I do this correctly?
What I've seen online is something like this:
bodyDef2.position.Set(axle2->GetWorldCenter().x + 1.3*cos(-M_PI/3), axle2->GetWorldCenter().y + 0.3*sin(-M_PI/3));
However, I don't understand the cos and sin parts of that expression.
What I'm trying to do is this:
backWheelDef.position = b2Vec2(carBody->GetWorldCenter().x - 20.0/PTM_RATIO,carBody->GetWorldCenter().y);
Since my carBody vertices definition looks like this:
b2Vec2 vertices[] = {
b2Vec2(100.0/PTM_RATIO,15.0/PTM_RATIO),
b2Vec2(100.0/PTM_RATIO,30.0/PTM_RATIO),
b2Vec2(50.0/PTM_RATIO,65.0/PTM_RATIO),
b2Vec2(20.0/PTM_RATIO,65.0/PTM_RATIO),
b2Vec2(0.0/PTM_RATIO,45.0/PTM_RATIO),
b2Vec2(0.0/PTM_RATIO,15.0/PTM_RATIO)
};
The result of the above way of positioning the wheel looks somehow correct (see below screenshot) but if you look at the above numbers it doesn't make much sense.
Since the carBody width is 100.0/PTM_RATIO I assume the GetWorldCenter().x would return a value equal to 50.0/PTM_RATIO.
So the wheel position would be after calculation at 30.0/PTM_RATIO, right?
Well, if you look at the result below the wheel x positioning seems to be at 20.0/PTM_RATIO.
Can someone please explain how things work with positioning bodies in relation to other bodies works in Box2D? Once I'm clear with these kind of things I'll move on to linking them using joints
Thanks a bunch!
LATER EDIT BASED ON iforce2d ANSWER:
Thank you iforce2d for answering and thank you for all the good insight you provide in the box2d world. What you're saying is perfectly true but this wasn't my question.
Once I've figured out that GetWorldCenter returns the center of gravity and GetPosition returns the position origin of the object it was easy to place my wheel exactly where I needed it to be in relationship to the carBody. Even though I still got the behaviour of it appearing under the body I knew this would be fixed once I add joints into equation.
Now I have another question that somehow relates to this positioning problem. Take a look at the image below:
Is this the right way my joints should look once I set their anchor points as below? I'm a little confused by them going from origin instead of them going from anchorPointA to anchorPointB
Thanks again!
You are placing the wheel inside the car body to begin with, so in the first two or three time steps the two bodies will move to correct the overlap. It probably happens so fast that you don't see it, but the tell-tale signs are there, namely the wheel being positioned exactly at the edge of the car, and the car body being rotated a tiny bit.
If you pause the simulation (just don't call Step) you should see the bodies where you expect to see them.
Once you add the joint, the default behavior is for the two joined bodies not to collide with each other, so you wont see this problem.
I'm new to using Box2d and really can't find a consistent answer to my question so please excuse me.
I'm writing a game for the iPhone -- who isn't these days -- and I'm using Cocos2d and Box2D. I have objects that move around in the game and I would like to give them a more of a curved path. From my studies I know that I should use body of type b2_kinematicBody which allows me to change the linear velocity every tick and keeps them from colliding with my other flying bodies. I do understand that if I know my current position (x,y) and my next position in the step (x1,y1) I would be able to compute the vector for velocity. Does anybody have a good way to give an object a defined curved path? I'm thinking it has to be some kind of parametric equation.
Take a look at bezier curves. After curve defenition you can compute the derivative (it's easy for bezier) and use it as a velocity for your body
OH whats the game called? sounds like a good one. have you looked into maybe just doing that in version 2.0
I have been looking all over for help on how to make a CCSprite move and rotate with the movement of a virtual joystick. I have tried examples and sample code, but, none have worked for me. If someone could tell me how to implement a joystick into the HelloWorld layer, that would be wonderful. P.S. The joystick does not need to move the sprite with variable velocity, it just needs to move it in a direction at a constant speed.
Thanks in advance.
First of all, this is a poorly worded question. Normally I would vote you down for this but for some reason I am not going to today.
You say there is no need for variable velocity, but it needs to move in a direction at a constant speed. I assume then that you need a full 360 degree range. What you do is have a joypad sprite on your layer. When you touch the sprite you calculate the angle of the touch (ignoring distance from center since you don't care about variable velocity). Convert the angle to a CGPoint ( ccpForAngle(CGFloat) is helpful for that ) and then use ccpMult(CGPoint, CGFloat) to decide how fast to move your sprite. Then you can use CCMoveBy to move your sprite.
That's one way of doing it at least.