Mar 10, 2012 at 11:53 PM
Edited Mar 10, 2012 at 11:53 PM
I've searched around a bit and looked at the code examples but cannot seem to get a screen-border working in Farseer 3.3.1.
border = new Body(PhysicsManager.World);
float fHalfWidth = PhysicsManager.ConvertToSimUnits(this.graphics.GraphicsDevice.Viewport.Width) / 2f;
float fHalfHeight = PhysicsManager.ConvertToSimUnits(this.graphics.GraphicsDevice.Viewport.Height) / 2f;
Vertices aBorders = new Vertices(4);
aBorders.Add(new Vector2(-fHalfWidth, fHalfHeight));
aBorders.Add(new Vector2(fHalfWidth, fHalfHeight));
aBorders.Add(new Vector2(fHalfWidth, -fHalfHeight));
aBorders.Add(new Vector2(-fHalfWidth, -fHalfHeight));
border.CollisionCategories = Category.All;
border.CollidesWith = Category.All;
border.Restitution = 0f;
border.Friction = 1f;
The code above results in a border but it is oddly positioned. It would seem the bottom edge of the border is half way across the screen or so and the top edge is way off the screen.
I've tried using vectors from 0, 0 to width, 0, etc, but nothing seems to work - there is always a margin of error.
Any ideas as to how I can construct a screen border like this?
It would seem that it wasn't to do with the way I was creating borders, but the container I made for other bodies that interacted with them. To ensure my methos were correct I've gone back to a basic ground and box texture,
boxTexture = this.Content.Load<Texture2D>("box"); // 64 x 64px
box = BodyFactory.CreateRectangle(PhysicsManager.World,
PhysicsManager.ConvertToSimUnits(new Vector2(this.screenDimensions.Width / 2f, this.screenDimensions.Height / 2f)));
box.BodyType = BodyType.Dynamic;
groundTexture = this.Content.Load<Texture2D>("ground"); // 1280 x 200px
ground = BodyFactory.CreateRectangle(PhysicsManager.World,
PhysicsManager.ConvertToSimUnits(groundTexture.Height), 0f, PhysicsManager.ConvertToSimUnits(new Vector2(this.screenDimensions.Width / 2f, this.screenDimensions.Height - 100f)));
ground.IsStatic = true;
This seems to create a box and the ground in the correct places. In the draw loop I'm using the following code,
this.spriteBatch.Draw(boxTexture, PhysicsManager.ConvertToDisplayUnits(box.Position), null, Color.White, 0f,
new Vector2(boxTexture.Width / 2f, boxTexture.Height / 2f), 1f, SpriteEffects.None, 0f);
this.spriteBatch.Draw(groundTexture, PhysicsManager.ConvertToDisplayUnits(ground.Position), null, Color.White, 0f,
new Vector2(groundTexture.Width / 2f, groundTexture.Height / 2f), 1f, SpriteEffects.None, 0f);
The conversion rate is set to 64f, but the result is this:
I'm unsure why its acting in this way, but I presume its something to do with converting back and fourth from meters to pixels?
Fixed it by lowering the conversion rate, for anyone else who has a similar problem. :)