This project has moved and is read-only. For the latest updates, please go here.

New to Farsser.

Oct 20, 2011 at 7:40 PM

Hello people.
I'm new to both Farseer Physics and Box2d.
I've got some questions:

I want to make my first Farseer Physics app.
I need simple ground and a rectangle wich is moving on it only forward. This rectangle must move forward without any user actions.
But it must jump when user pressed "space" =)

1. This is my code to create my Rectangles Body:

BodyNode = BodyFactory.CreateRectangle(GameController.Instance.PhysicsWorld, 64, 64, 1f, Vector2.Zero);

BodyNode.BodyType = BodyType.Dynamic;

BodyNode.Mass = 1;

BodyNode.LinearVelocity = new Vector2(10, 0);�


Should I attach a shape to my body? Like this?

NodeShape = new PolygonShape(PolygonTools.CreateRectangle(32, 32, 1);

Fixture fixture = BodyNode.CreateFixture(NodeShape);�

And why?

2. How to get my rectangle moving? 


Oct 21, 2011 at 9:22 PM

If you define a body as a rectangle, and then define a shape of a rectangle and then you attach it to the body then you're creating it twice.


To make your rectangle moving you have different ways. The physic way, which is by applying a force to the body continuously, which would make you program have to make calculations continuously. And the pixel way, which consist in increment the position of the body every seconds. In both cases you just need to ad a line or two in the update method.

Oct 22, 2011 at 9:30 AM
Edited Oct 22, 2011 at 9:31 AM

Thanks for your answer, but if I dont create a shape. only rectangle body it does not collide with other objects =(

I found a great tutorial:
But it is for farseer 2.x =(
Is there the same tutorial for 3.x?

I was on many forums and it seems like in farseer 2.x they used pixels. And in 3.x they are using metters. Am I right? 

Oct 22, 2011 at 10:59 AM

most of it is the same for 3.x

and yes in 3.x you should use meters

take a look here:

Oct 22, 2011 at 5:40 PM


Oct 22, 2011 at 6:03 PM

I have got a question more =)
I read about DebugView. It seems realy cool! And for me, novice in farseer it may be very useful! But when I added it to my project and make all needed operations I stopped on last one:
I dont know how to create Projection and View matrices for the  DebugView method - RenderDebugData. I was on forums, but people just say about their variants of matrices, an noone of ther variants does not work in my project =(
Help please. Is there a stable method to make these matrices and Render Debug Data? 

Oct 22, 2011 at 9:06 PM

Thanks to all for your answers. I have done what I want =)))
The last question is hoe to configure my camera position in platformer game.

Now I use this code:

Matrix projection = Matrix.CreateOrthographic(Graphics.PreferredBackBufferWidth / PixelsPerMetter, -Graphics.PreferredBackBufferHeight / PixelsPerMetter, 0, 1000000);

Matrix view;

Vector3 campos = new Vector3();

campos.X = (-Graphics.PreferredBackBufferWidth / 2) / PixelsPerMetter;

campos.Y = (Graphics.PreferredBackBufferHeight / 2) / -PixelsPerMetter;

campos.Z = 0;
Matrix tran = Matrix.Identity;

tran.Translation = campos;

view = tran;

DebugView.RenderDebugData(ref projection, ref view);

But it works only for static screen =( not for moving one =(


Oct 23, 2011 at 10:27 AM

i use this one for my 3d things (not for debug view) but it should work as well

Matrix.CreateOrthographicOffCenter(0, device.PresentationParameters.BackBufferWidth, device.PresentationParameters.BackBufferHeight, 0, 1, 50, out projMatrix);
Vector3 camPos = new Vector3(0f, 0f, 10f);
Vector3 target = new Vector3(0f, 0f, 0f);
viewMatrix = Matrix.CreateLookAt(camPos, target, Vector3.UnitY) *
            Matrix.CreateTranslation(-cam.Position.X, -cam.Position.Y, -1f) *
            Matrix.CreateRotationZ(cam.Rotation) *
            Matrix.CreateTranslation(cam.Origin.X, cam.Origin.Y, 0f) *
            Matrix.CreateScale(new Vector3(cam.Scale, cam.Scale, 1f));

Oct 23, 2011 at 12:11 PM

It does not work =(
I tried many combinations, but nothing helps...
I have got a game: platformer.
My person is always in the center of the screen.
Other objects in a world are drawing relative to main persons position.
And it looks like the world is scrolling. (E. G. in mario)
I dont use any cameras or other stuff.
I only have my persons position, for example {2390; 230}, but it draws in the center of screen.
Other objects are drawing at object.Position - person.Position.
Where is , my camera now =( At what position? Should I consider it's position or i can calculate matrices without it?
Please, help! 

Oct 24, 2011 at 12:17 PM

i also making a platformer using my camera class. it much easyer than moving every sprite

i just make a matrix that contains the position, the zoom and the rotation and add it to spriteBatch.Begin


btw. did you added Matrix.CreateScale(new Vector3(1f/PixelsPerMetter, 1f/PixelsPerMetter, 1f)) ?

Oct 24, 2011 at 2:48 PM

Thanks =)
Now I have bad (but working) DebugView =)

But I dont know what should I write in places marked //! HERE IS SOMETHING WRONG

Now my debug view looking like this:

1. But the world dont scrolling when rectangle runs.
2. Rectangle and floor are not in the center of the screen =( 

Matrix projMatrix;
Matrix.CreateOrthographicOffCenter(0, Graphics.PreferredBackBufferWidth, Graphics.PreferredBackBufferHeight, 0, 1, 50, out projMatrix);

Vector3 camPos = new Vector3(0, 0, 10f); //! HERE IS SOMETHING WRONG

Vector3 cam = new Vector3(Graphics.PreferredBackBufferWidth / 2, -Graphics.PreferredBackBufferHeight / 2, 0f); //! 100%

Vector3 target = new Vector3(0, 0f, 0f); //! HERE IS SOMETHING WRONG
Matrix viewMatrix =

	Matrix.CreateLookAt(camPos, target, Vector3.UnitY) * //! 100%

	Matrix.CreateTranslation(-cam.X, -cam.Y, -1f) * //! 100%

	Matrix.CreateTranslation(Graphics.PreferredBackBufferWidth / 2, -Graphics.PreferredBackBufferHeight / 2, 0f) * //! 100%

	Matrix.CreateScale(new Vector3(1.0f * PixelsPerMetter, 1.0f * PixelsPerMetter, 1.0f)); //! 100%

DebugView.RenderDebugData(ref projMatrix, ref viewMatrix);

Thanks for any help.

Oct 24, 2011 at 3:21 PM


Vector3 camPos = new Vector3(0, 0, 10f);

Vector3 target = new Vector3(0, 0f, 0f);

was made to fit 3D things for 2D things (for spriteBatch)

you have to find the values out yourself (try to search in the simple samples)

Oct 24, 2011 at 3:53 PM

Ahahahahaha =D
All is working as I want =)))
But it does not in the center of the screen =))))))) 

Matrix projMatrix;

Matrix.CreateOrthographicOffCenter(0.0f, Graphics.PreferredBackBufferWidth, Graphics.PreferredBackBufferHeight, 0.0f, 1.0f, 50.0f, out projMatrix);

Vector3 cam = new Vector3(Graphics.PreferredBackBufferWidth / 2, -Graphics.PreferredBackBufferHeight / 2, 0f);

Matrix viewMatrix = Matrix.Identity;

viewMatrix *= Matrix.CreateTranslation(-cam.X, -cam.Y, -1.0f);

viewMatrix *= Matrix.CreateTranslation(Graphics.PreferredBackBufferWidth / 2, -Graphics.PreferredBackBufferHeight / 2, 0f);

viewMatrix *= Matrix.CreateTranslation(-CameraPosition.X / PixelsPerMetter, -CameraPosition.Y / PixelsPerMetter, 0.0f);

viewMatrix *= Matrix.CreateScale(new Vector3(1.0f * PixelsPerMetter, 1.0f * PixelsPerMetter, 1.0f));