How to spawn / shoot projectiles in farseer

Topics: Developer Forum
Apr 9, 2013 at 1:51 PM
Hello there!

I've ended up rather empty handed when it comes to documentation concerning projectiles / bullets in farseer. Could someone help me with that?

Either by writing a small tutorial here, or linking me to some great tutorial that I obviously haven't found.
Apr 9, 2013 at 8:28 PM
Should be pretty straightforward. Add a dynamic body at the end of your gun, set the IsBullet property on the body (to enable CCD and avoid tunneling), give the body an impulse in the direction you're shooting. If you don't want the body to physically impact the target (i.e. knock the target over), set the IsSensor property. If it's a sensor you still get collision events but it won't affect other dynamic bodies.

Hope that helps,
Apr 18, 2013 at 1:57 PM
I've also been wonder about this... specifically how would you pool projectile bodies efficiently?
Apr 18, 2013 at 5:51 PM
Athyl, would be clearer if you stated what's the specific problem?

I use
  Body.IsBullet = true;
  Body.IsSensor = true;
For the bullets body properties. Bullet for fast moving bodies.

When launching a bullet from e.g. a spaceship, get the direction of the Body who is shooting by:
  Vector2 directionVector = ownerEntity.Body.GetWorldVector(new Vector2(0, -1));
  Vector2 forceVector = directionVector * BulletForce;
The first line gets a world unit vector related to the direction of the ship (local vector is one unit upwards).
Second line creates a force vector, multiply the unit vector with the force to get a directional force.
Then on third line, apply a the force as an Impulse to the bullets body, it gets to the max velocity immediately.

in the above code "ownerEntity" is the players spaceship which is a Sprite. (Spaceship owns a Weapon which generates Bullets.)