Physics Engine Woes Part 3
/
0 Comments
I know, I know. you have been saying: "Corndog! whats the deal?! I already know how to set up the debug view for Farseer, thats not what my problem was. I would just like a straight forward guide to how to make it do stuff!!"
Well, maby thats not what you were thinking of. but I sure was...anyway.
Well here you go, the moment you have been waiting for. this is where the fun begins. the how and why of the Farseer Physics Engine.
The How:
After you have succeeded at getting it all set up. now the question is what code to use to get stuff to start bouncing around the screen?
to start off you need to make a physics body. or as the most recent version of Farseer calls it: a "fixture."
in the loadContent method of your XNA game add the following line (or something like it.)
rectangleFixture = FixtureFactory.CreateRectangle(world, 4, 4 , 1, new Vector2(0, 0));
This will create a physics fixture in the shape of a "rectangle." actually this one will be a square because we have set the height and width to: '4'
The First parameter if the CreateRectangle Method takes a "world" object that is created as a private member variable of the game class. (for more info on this see the previous post labeled "Physics engine woes part 2.")
The next to parameters are the width and height, (in that order.) You should be aware that in the new version of Farseer the values are in the metric system. this means that our previously determined size is 4 meters by 4 meters. Why the Farseer programmers decided to do this is beyond me. If you figure that out please let me know. because I thought pixels were a much better convention.
Next is the mass of the fixture. this will affect how the object behaves in the world based on the forces applied to it. currently it is set to '1' as far what values to use in your game, you will have to mess around with the values until you see the behavior you are looking for.
the last parameter is a Vector2 that contains the
Well that was a mouth full. NEXT!!
to make this more interesting we will need to set a few more variables on this fixture to make it more interesting. add the following lines (or ones like them.)
rectangleFixture.Body.BodyType = BodyType.Dynamic;
rectangleFixture.Friction = 2.0f;
rectangleFixture.Restitution = 0;
The common types of body are Dynamic and Static. A static Body will not move from where it is placed. (that Vector2 from earlier.)
If its Dynamic then it will be affected by gravity and any other forces you see fit to subject it to.
Friction should be kind of obvious but in case someone missed that day of science class because you were out on the town with your pet chinchilla...*cough*
Friction determins how "slippy" the surface is when it comes in contact with other surfaces.
Restitution is, simply, how bouncy it is.
And thats how to create a physics Fixture.
here after you can make it do things by applying various forces such as linear velocity, angular velocity, etc. if you need to know the how to on those I suggest you look at the farseer Documentation but mostly just use visual studios auto complete function. that is honestly the way that I figured out most of what I know about Farseer.
Happy coding.
CornDog
Well, maby thats not what you were thinking of. but I sure was...anyway.
Well here you go, the moment you have been waiting for. this is where the fun begins. the how and why of the Farseer Physics Engine.
The How:
After you have succeeded at getting it all set up. now the question is what code to use to get stuff to start bouncing around the screen?
to start off you need to make a physics body. or as the most recent version of Farseer calls it: a "fixture."
in the loadContent method of your XNA game add the following line (or something like it.)
rectangleFixture = FixtureFactory.CreateRectangle(world, 4, 4 , 1, new Vector2(0, 0));
This will create a physics fixture in the shape of a "rectangle." actually this one will be a square because we have set the height and width to: '4'
The First parameter if the CreateRectangle Method takes a "world" object that is created as a private member variable of the game class. (for more info on this see the previous post labeled "Physics engine woes part 2.")
The next to parameters are the width and height, (in that order.) You should be aware that in the new version of Farseer the values are in the metric system. this means that our previously determined size is 4 meters by 4 meters. Why the Farseer programmers decided to do this is beyond me. If you figure that out please let me know. because I thought pixels were a much better convention.
Next is the mass of the fixture. this will affect how the object behaves in the world based on the forces applied to it. currently it is set to '1' as far what values to use in your game, you will have to mess around with the values until you see the behavior you are looking for.
the last parameter is a Vector2 that contains the
Well that was a mouth full. NEXT!!
to make this more interesting we will need to set a few more variables on this fixture to make it more interesting. add the following lines (or ones like them.)
rectangleFixture.Body.BodyType = BodyType.Dynamic;
rectangleFixture.Friction = 2.0f;
rectangleFixture.Restitution = 0;
The common types of body are Dynamic and Static. A static Body will not move from where it is placed. (that Vector2 from earlier.)
If its Dynamic then it will be affected by gravity and any other forces you see fit to subject it to.
Friction should be kind of obvious but in case someone missed that day of science class because you were out on the town with your pet chinchilla...*cough*
Friction determins how "slippy" the surface is when it comes in contact with other surfaces.
Restitution is, simply, how bouncy it is.
And thats how to create a physics Fixture.
here after you can make it do things by applying various forces such as linear velocity, angular velocity, etc. if you need to know the how to on those I suggest you look at the farseer Documentation but mostly just use visual studios auto complete function. that is honestly the way that I figured out most of what I know about Farseer.
Happy coding.
CornDog


