object fall through the floor

Topics: Developer Forum
Feb 8, 2009 at 12:07 PM
Hi,
this is my first try with Farseer, and I'm just stuck with this. Created a body and the geometry for a object and for the floor,
but I dont know why the object falls through the floor, any tips ? =)

follow the code:


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;

namespace Physics_poc1
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Texture2D bug;
Body bugBody;
Geom bugGeometry;
Vector2 bugCenter;

Texture2D floor;
Body floorBody;
Geom floorGeometry;
Rectangle floorPosition;

PhysicsSimulator physicsSimulator;

Vector2 JUMP_IMPULSE = new Vector2(50, -50);
const int FLOOR_HEIGHT = 30;
const int GRAVITY_Y = 100;

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
physicsSimulator = new PhysicsSimulator(new Vector2(0, GRAVITY_Y));

floor = Content.Load<texture2d>("black");
floorPosition = new Rectangle(0, GraphicsDevice.Viewport.Height - FLOOR_HEIGHT,
GraphicsDevice.Viewport.Width, FLOOR_HEIGHT);

floorBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator,
floorPosition.Width, floorPosition.Height, 1);
floorBody.IsStatic = true;
floorGeometry = GeomFactory.Instance.CreateRectangleGeom(floorBody,
floorPosition.Width, floorPosition.Height);
floorBody.Position = new Vector2(floorPosition.Location.X, floorPosition.Location.Y);
floorGeometry.RestitutionCoefficient = .4f;
floorGeometry.FrictionCoefficient = .4f;

bug = Content.Load<texture2d>("bug");
bugCenter = new Vector2(bug.Width / 2f, bug.Height / 2f);
bugBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, bug.Width, bug.Height, 1);
bugGeometry = GeomFactory.Instance.CreateRectangleGeom(bugBody, bug.Width, bug.Height);
bugBody.Position = new Vector2(floorPosition.Location.X + bug.Width,
floorPosition.Location.Y - bug.Height);

}

KeyboardState lastKeyboardState = Keyboard.GetState();

protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

KeyboardState state = Keyboard.GetState();

// check if a key was pressed
foreach (Keys wasPressed in lastKeyboardState.GetPressedKeys() )
{
bool found = false;
foreach (Keys isPressed in state.GetPressedKeys())
{
if (wasPressed.Equals(isPressed))
{
found = true;
}
}
if (!found)
{
keyPressed(wasPressed);
}
}

lastKeyboardState = state;
physicsSimulator.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);
base.Update(gameTime);
}

private void keyPressed(Keys key)
{
if (key.Equals(Keys.Space))
{
bugBody.ApplyImpulse(JUMP_IMPULSE);
}
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

spriteBatch.Draw(floor, floorPosition, Color.White);
spriteBatch.Draw(bug, bugBody.Position, null,
Color.White, bugBody.Rotation, bugCenter, 1, SpriteEffects.None, 0);

spriteBatch.End();
base.Draw(gameTime);
}

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
base.Initialize();
}

protected override void UnloadContent()
{
}


}
}
</texture2d></texture2d>
Feb 8, 2009 at 1:20 PM
I believe I found the problem !! wasn't using the physics simulator to create both Geometries,

follow the codw that works =)


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using FarseerGames.FarseerPhysics.Dynamics;
using FarseerGames.FarseerPhysics.Factories;
using FarseerGames;
using FarseerGames.FarseerPhysics;
using FarseerGames.FarseerPhysics.Collisions;

namespace Physics_poc1
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Texture2D bug;
Body bugBody;
Geom bugGeometry;
Vector2 bugCenter;

Texture2D floor;
Body floorBody;
Geom floorGeometry;
Vector2 floorCenter;
Rectangle floorPosition;

PhysicsSimulator physicsSimulator;

Vector2 JUMP_IMPULSE = new Vector2(0, -350);
Vector2 MOVE_LEFT_FORCE = new Vector2(-10, 0);
Vector2 MOVE_RIGHT_FORCE = new Vector2(10, 0);

const int FLOOR_HEIGHT = 30;
const int GRAVITY_Y = 500;

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
physicsSimulator = new PhysicsSimulator(new Vector2(0, GRAVITY_Y));

floor = Content.Load<texture2d>("black");
floorCenter = new Vector2(floor.Width / 2f, floor.Height / 2f);
floorPosition = new Rectangle(GraphicsDevice.Viewport.Width / 2,
GraphicsDevice.Viewport.Height - FLOOR_HEIGHT,
GraphicsDevice.Viewport.Width, FLOOR_HEIGHT);

floorBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator,
floorPosition.Width, floorPosition.Height, 1);
floorBody.IsStatic = true;
floorGeometry = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, floorBody,
floorPosition.Width, floorPosition.Height);
floorBody.Position = new Vector2(floorPosition.Location.X, floorPosition.Location.Y);
floorGeometry.RestitutionCoefficient = .4f;
floorGeometry.FrictionCoefficient = .4f;

bug = Content.Load<texture2d>("bug");
bugCenter = new Vector2(bug.Width / 2f, bug.Height / 2f);
bugBody = BodyFactory.Instance.CreateRectangleBody(physicsSimulator, bug.Width, bug.Height, 1);
bugGeometry = GeomFactory.Instance.CreateRectangleGeom(physicsSimulator, bugBody, bug.Width, bug.Height);
bugBody.Position = new Vector2(bug.Width, floorPosition.Location.Y - bug.Height);

}

KeyboardState lastKeyboardState = Keyboard.GetState();

protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

KeyboardState state = Keyboard.GetState();

// check if a key was pressed
foreach (Keys wasPressed in lastKeyboardState.GetPressedKeys() )
{
bool found = false;
foreach (Keys isPressed in state.GetPressedKeys())
{
if (wasPressed.Equals(isPressed))
{
found = true;
}
}
if (!found)
{
keyPressed(wasPressed);
}
}

if (state.IsKeyDown(Keys.Left))
{
bugBody.ApplyImpulse(MOVE_LEFT_FORCE);
}

if (state.IsKeyDown(Keys.Right))
{
bugBody.ApplyImpulse(MOVE_RIGHT_FORCE);
}

lastKeyboardState = state;
physicsSimulator.Update(gameTime.ElapsedGameTime.Milliseconds * .001f);
base.Update(gameTime);
}

private void keyPressed(Keys key)
{
if (key.Equals(Keys.Space) || key.Equals(Keys.Up))
{
bugBody.ApplyImpulse(JUMP_IMPULSE);
}
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

spriteBatch.Draw(floor, floorBody.Position, null,
Color.White, floorBody.Rotation, floorCenter, 1, SpriteEffects.None, 0);
spriteBatch.Draw(bug, bugBody.Position, null,
Color.White, bugBody.Rotation, bugCenter, 1, SpriteEffects.None, 0);

spriteBatch.End();
base.Draw(gameTime);
}

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.PreferredBackBufferWidth = 640;
graphics.PreferredBackBufferHeight = 480;
Window.AllowUserResizing = true;
}

protected override void Initialize()
{
base.Initialize();
}

protected override void UnloadContent()
{
}


}
}

</texture2d></texture2d>