Sign in to follow this  
  • entries
    455
  • comments
    639
  • views
    422562

A Farseer Physics Gotcha

Sign in to follow this  

277 views

So, I've got an idea for a game kicking around in my head and I've been building a proto-type today in order to test out the idea.

Part of this requires some 2D Physics, for which I've opted for Farseer as it's a perfect fit with C#, and on the whole it's been pretty easy to work with even for someone like myself who is a complete newbie to this sort of thing [grin]

Something which did trip me up for a bit however was bullets. You see I was creating a pool of bullets to use and then when fire was pushed (thank you SlimDX and XInput) a bullet was shot out into the world from the current location.

All well and good, however sometimes a bullet would get 'stuck' to the player, being drawn but not being updated by the physics system. This puzzled me and after checking my code was correct I took a look inside Farseer to see what was going on.

At which point the gotcha became clear;
Farseer queues up add/removes from the simulation until the next frame and it applies them by first dealing with 'add' then with 'remove'. This is where my code had a slight disagreement with it as I was removing objects and then readding them in the same frame. So, Farseer would add the object and then remove it right away.

My main issue here was I didn't realise the operations were cached until later, so a little reworking of my main loop later to defer returning objects to the pool by a frame and it all works fine.

On with the playing about then...
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now