Are BodyDef / FixtureDef available in Farseer Physics? - xna

I've searched through all of the documentation that I can find for Farseer, and cannot find how to use BodyDef / FixtureDef in my projects. Have these been renamed to something else when they were ported over from Box2D?
My main concern is accomplishing something similar to the following:
FixtureDef fdef;
fdef.isSensor = true;

Although Farseer Physics has origins in Box2D, it is not an exact port of it. It has some similarities but does things it's own way (more familiar with C# programmers I guess). It doesn't use Def classes. I believe the following 2 examples are equivalent:
C++
// create a body
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(-10, 20);
b2Body* body = world->CreateBody(&bodyDef);
// create a shape
b2CircleShape circleShape;
circleShape.m_radius = 0.5f;
// create a fixture
b2FixtureDef myFixtureDef;
myFixtureDef.shape = &circleShape;
body->CreateFixture(&myFixtureDef);
C#
// create a body
var body = world.CreateBody();
body.BodyType = BodyType.Dynamic;
body.Position = new Vector2(-10, 20);
// create a shape
var circleShape = new CircleShape(0.5f);
// create a fixture
var fixture = body.CreateFixture(circleShape);
As you can see from the body, the main difference is that you set the properties after creation on the actual instance rather than a def class.
Farseer Physics also makes use of handy Factory classes. Take a look at the documentation for more information.
https://farseerphysics.codeplex.com/documentation

Related

How to copy a Rectangle in Dart

I have two rectangles, one that is occasionally reset to some other rectangle. In C++ I'd just do:
_rect = _resetRect;
But in Dart that actually means that _rect now refers to the same object as _resetRect which is not what I want.
My current solution is this:
_rect.left = _resetRect.left;
_rect.width = _resetRect.width;
_rect.top = _resetRect.top;
_rect.height = _resetRect.height;
This is idiotic. Other questions suggest that there is no built-in way to copy objects (like there is in C++), and you have to rely on the object providing a clone() method. But Rectangle doesn't have one so what do I do?
Also even if it did have a clone() method, wouldn't that allocate an entirely new Rectangle rather than just setting the fields of the existing one (like C++'s operator=), and therefore be less efficient?
C++ also does not have a way to deep-copy an object which contains pointers/references to other objects. In Dart, all values are references, so that restriction applies to all objects.
I assume this is a MutableRectangle since the Rectange in dart:math is unmodifiable.
That class indeed does not have a way to clone the values of another rectangle, so you have to copy each of them. I would use a cascade for that:
_rect
..left = _resetRect.left
..top = _resetRect.top
..width = _resetRect.width
..height = _resetRect.height;
Alternatively, if it happens often enough, you can create a helper function:
void copyRectangle(MutableRectangle target, Rectangle source) {
target
..left = source.left
..top = source.top
..width = source.width
..height = source.height;
}

How to use ApplyForce with box2DWeb

I have a Box2DWeb sketch working ok but I am unable to figure out how to use the ApplyForce method with a body. I have attached the working codepen. On line 85, I have commented out the line that I thought would work but everything disappears when I include it.
If anyone could let me know the correct way to use it, I would be very happy. I have RTFM and seen similar posts on StackO but I still cannot work it out.
http://codepen.io/anon/pen/vOJByN?editors=101
Thanks a lot,
Steven
// single dynamic object----------------------
var fixDef2 = new b2FixtureDef;
fixDef2.density = 1.0
fixDef2.friction = 0.2;
fixDef2.restitution = 0.5;
var bodyDef2 = new b2BodyDef;
bodyDef2.type = b2Body.b2_dynamicBody;
fixDef2.shape = new b2PolygonShape;
fixDef2.shape.SetAsBox((300/SCALE)/2, (60/SCALE) / 2);
bodyDef2.position.x = canvas.width/4/SCALE;
bodyDef2.position.y = canvas.height/2/SCALE;
bodyDef2.angle = 5;
world.CreateBody(bodyDef2).CreateFixture(fixDef2);
// Apply force to object----------------------
/*bodyDef2.ApplyForce(new b2Vec2(500,50) , bodyDef2.GetWorldCenter());
*/
You should call ApplyForce method of b2Body, not of b2BodyDef. You can get b2Body object as result of world.CreateBody(bodyDef2) method.
I've changed your codepen here: http://codepen.io/anon/pen/NqZvqG
Your code:
world.CreateBody(bodyDef2).CreateFixture(fixDef2);
// Apply force to object----------------------
/*bodyDef2.ApplyForce(new b2Vec2(500,50) , bodyDef2.GetWorldCenter());
*/
My code:
var myBody = world.CreateBody(bodyDef2);
var myFixture = mybody.CreateFixture(fixDef2);
// Apply force to object
myBody.ApplyForce(new b2Vec2(500,50), myBody.GetWorldCenter());

V3: Make two bodies of same collisionType collide with each other

I am a newbie developer in cocos2d, so please be patient to me. Thanks.
What I am trying to implement is two bodies of the same collision type (They are also not in a custom class, just CCSprite) colliding only with each other and the ground (e.g. debris) and not with anything else. What I don't understand is how to format the ccPhysicsCollisionBegin method. If I format it like this:
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair debris:(CCSprite *)debris debris:(CCSprite *)debris2
and return YES it will still affect other unwanted bodies, and after a couple of CCLOGs I found out it's never called anyway. So what is the correct way to achieve what I want? Hope what I have requested is possible.
Code used:
debris.physicsBody = [CCPhysicsBody bodyWithRect:[debris boundingBox] cornerRadius:0];
debris.name = #"Debris";
debris.physicsBody.collisionType = #"debris";
[physicsNode addChild:debris];
You need to set-up the collisionMask, which defines what the body can collide with. You should also probably set-up the collisionCategories as well:
debris.physicsBody = [CCPhysicsBody bodyWithRect:[debris boundingBox] cornerRadius:0];
debris.name = #"Debris";
debris.physicsBody.collisionType = #"debris";
debris.physicsBody.collisionCategories = #[ #"debris" ];
debris.physicsBody.collisionMask = #[ #"debris" ];
[physicsNode addChild:debris];

car moving body in box2d with cocos2d-x

i have car body and 2 tiers as seprate images .
how to fix as car body(box2d with cocos2d-x) and moving animation.(i need to rotate tier) and jumping etc.,?
in normal cocos2d-x i can make something like this..
car = CCSprite::spriteWithFile("car.png");
car->setPosition(ccp(car->getContentSize().width/2+30, car->getContentSize().height-19));
this->addChild(car, 10);
tire = CCSprite::spriteWithFile("tire.png");
tire->setPosition(ccp(tire->getContentSize().width/2+43, tire->getContentSize().height+8));
this->addChild(tire, 10);
tire1 = CCSprite::spriteWithFile("tire.png");
tire1->setPosition(ccp(tire->getContentSize().width/2+136, tire->getContentSize().height+8));
this->addChild(tire1, 10);
CCRotateBy *Rot = CCRotateBy::actionWithDuration (1.0f, 360);
CCRepeatForever *rep = CCRepeatForever::actionWithAction(Rot);
CCRotateBy *Rot1 = CCRotateBy::actionWithDuration(1.0f, 360);
CCRepeatForever *rep1 = CCRepeatForever::actionWithAction(Rot1);
tire->runAction(rep);
tire1->runAction(rep1);
but in box2d how to make? any example..
for collison detction for all thing planing to use box2d.
cocos2D-x Doc contains an article about Box2D..
http://www.cocos2d-x.org/docs/manual/framework/native/physics/physics-integration/en
not that detailed, but good enough to start with.
Also cocos2D-x framework test contains example of both Box2D and Chipmumk physics example.
http://www.cocos2d-x.org/docs/manual/framework/native/getting-started/v3.0/how-to-run-cpp-tests-on-win32/en

Adding a second fixture as sensor to body in Box2Dweb

I have two problems adding a sensor fixture in the following code. The repositioning vector b2Vec2(0,50) is not working; the second fixture is still centered at the origin of the body. Both fixtures can be seen in debug mode but I can't move the sensor fixture to the foot of the main fixture.
Secondly, when isSensor is true I am not receiving any PostSolve events. When it is set to false I get the appropriate events (along with collision). How do I make this a sensor that will not collide with other bodies but still raise events. Thank you for your help.
// FIXTURE DEF
var fixDef = new box2d.b2FixtureDef();
fixDef.shape = new box2d.b2PolygonShape;
fixDef.shape.SetAsBox((25 / 2 / SCALE), (46 / 2 / SCALE));
fixDef.density = 0.99;
fixDef.friction = 0.39;
fixDef.restitution = 0.0;
fixDef.userData = "SBody";
fixDef.filter.categoryBits = CAT.SOLDIER;
fixDef.filter.maskBits = CAT.GROUND;
this.view.body.CreateFixture(fixDef);
// ADD FOOT SENSOR
fixDef.density = 0.1;
fixDef.friction = 1.;
fixDef.restitution = 0.1;
fixDef.userData = "Foot";
fixDef.shape.SetAsBox((10 / 2 / SCALE), (100 / 2 / SCALE), new box2d.b2Vec2(0,50), 0);
fixDef.isSensor = true;
fixDef.filter.categoryBits = CAT.SOLDIER_FOOT_SENSOR;
fixDef.filter.maskBits = CAT.SHIP | CAT.GROUND;
this.view.body.CreateFixture(fixDef);
I was using the tip in this answer https://stackoverflow.com/a/4707127/1172891 that said to add positioning as the 3rd parameter, but I recently found that SetAsBox cannot take a 3rd argument, maybe it used to. Instead I found SetAsOrientedBox is a similar method and accepts the 3rd parameter for positioning. Found on the Box2dFlash reference http://www.box2dflash.org/docs/2.1a/reference/Box2D/Collision/Shapes/b2PolygonShape.html
For the sensor callbacks, I was only trying the PostSolve at first. I recently thought to try the other events like EndContact and found that it worked. I then found this page that seems to be the only place that explicitly states that sensors only raise BeginContact and EndContact events: http://www.box2dflash.org/docs/2.1a/updating in the Events section.
Hope that saves someone some time!

Resources