Is there any way in Box2D to get the impulse of a collision without registering a contact listener?
Ie, I can get a list of all contacts for a body using GetContactList() and contact->GetManifold()->localNorm, but this returns a unit vector.
I couldn't seem to find any way of getting the impulse information from the b2Contact class.
No, there may be more than one impulse per time step. Use a listener.
Related
I'm using the C++ Box2D library. I've created a body and have added multiple square fixtures to it. These square fixtures are offset from the body position (similar to what is done in the "multiple fixtures" section here: https://www.iforce2d.net/b2dtut/fixtures).
My idea is for a rendering system to iterate over the fixture and draw them to the screen. However, I can't seem to find a method for retrieving the X, Y and angle of the fixture. When stepping my Box2D world, the body X/Y/rotation position changes which changes the fixture's X/Y/rotation psoition.
Is there an easy way to extract this information from a square fixture? I could do the math myself to derive the position, however, it seems odd that Box2D wouldn't offer a solution to do this.
Fixtures do not have any location or transform on their own.
Shapes have local points (e.g. b2CircleShape has a center, b2PolygonShape has a list of vertices). However, shapes can be reused in multiple fixtures/bodies. Therefore it makes no sense for them to know about any of the fixtures/bodies they are attached to.
To get a shape's location as it is attached to a body in world space, simply use the body's GetWorldPoint method:
body.GetWorldPoint(p);
where p is any local point of a shape attached to that body (e.g. the location of a b2CircleShape or a vertex of a b2PolygonShape).
From wat you described, it sounds like you need a tool to calculate where everything is. I could be wrong on this but the closet thing I seen in the documentation for the product your using is "Ray Casting" and "AABB testing". Both are described in the World Querying section of the documentation.
I've searched extensively on how to make a Box2d body move along a Bezier curve or arc-like path with a start point, end point and possibly another control point. I know cocos2d objects can be moved around using ccBezier but how can it be done for Box2d bodies? Please help. Thanks in advance.
I have tried to move a box2d body using a curve (making a spider walk around an asteroid). I found that using SetTransform every frame worked, but it made the collision response in the engine fail.
I was able to find a much better solution by using prismatic joints. You create a joint starting at the position (p0) you want to start from and pointed towards the next waypoint (p1). Then you let the joint push the body from p0 to p1 using the motor. When the body gets very close to the next point, destroy the joint and form a new one for the next two points. The body will still have its velocity so the motion looks smooth.
I put a more complete post on this on Stack Overflow here.
Was this helpful?
I'm making a game in which a certain object (modelled as a box2d body) has to follow a fixed path. Is there a way by which I can specify the path coordinates and make the object advance over it for each dt?
Thanks
Another option:
Attach a mouse joint to your body
Use setTarget method of the mouse joint to move the body
You should use a Kinematic body, but you can't change its position manually, you have to change its speed for the dynamics and collisions to be applied correctly.
I suggest the following algorithm:
1st - Calculate the position on the track that the body should be in on the next dt.
2nd - Make a vector going from the position where the body is to the next position.
3rd - Normalize it.
4rd - Calculate how much speed you need so that the body will be in that position on the next loop, and multiply that speed on the vector.
5th - Apply this vector to the Linear Velocity of the body.
Note: make sure the kinematic body has zero drag so that calculating the 4th step is easier.
I never did something like this, I think it can be done this way.
Hope it helps :)
i am trying to understand how to implement the physics of sprite when it hit a wall.
lets say i have a wall, and a sprite is is hitting the wall with velocity and gravity using box2d(cocos2d), what is the simplest way to apply a physics of what happen next to the hit, regrading the velocity,gravity,angle of collision, etc ? contact listener ? do i have to calculate what happen next by my self and apply a new speed and force to the body ??
or box2d does it for me ?
any direction would be great.
thanx.
box2d calculates everthing for you. You don't have to worry about what happens after two bodies collide.
If your body hits ground it will bounce as its natural response. You dont have to apply a new force opposite to the gravity. All calculations are done by box2d physics engine. Physics engines are made for that.
In addition to this, if you want you can apply your own forces or impulses like below. It is up to you.
b2Vec2 force = b2Vec2(100, 200);
yourBody->ApplyLinearImpulse(force, yourBodyDefinition.position);
Have been working on building a Bridge builder using box2d.
Basically you build a bridge and stress test it by passing objects on top of it. The joints break if the stress applied is high.
Can you please help how best to join the b2Body. Have tried revolute joint, but they are not breaking. Any suggestions.
Thanks
"You can get the reaction force and torque off the joint. You can query these forces after each time step and destroy the joint when your threshold is exceeded." - Quote from Erin Catto - http://www.box2d.org/forum/viewtopic.php?f=3&t=1079
I think the joints will not break by themselves. You have to break them using DestroyJoint function. You can setup b2ContactListener and get the impulse velocity of the object that hits your bridge and you can take the decision if you want to break the joint or not.