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

Raycast Bullet logic

Topics: Developer Forum, User Forum
Jan 24, 2012 at 4:08 PM

I decided to re-do the way I create bullets in my 2d shooter game.  Before, each bullet would be a seperate body but I was getting undesirable results, such as bullets flying through walls or getting stuck.  Now I am using RayCasts which works better but I have some issues with my logic.  What I do here is when the gamer fires the charaters gun, I create a RayLine and check for the first collision in the line.  Once I recieve that point, I will draw my bullet and move it to that collision point and react to it based on what fixture I collide with.  My issue here is that, what if the ray hits a fixture, such as an 'enemy' and I start to draw my bullet towards that enemy.  What if during this time, another enemy moves in front of the bullet?  My thoughts around this were to leave the Ray active until the bullet reaches its end point, but if I am firing 5 - 10 bullets a second I think my performance will go down with so many active ray casts.  Can anyone walk me through the logic they have done to accomplish this?  Thanks in advance.

Jan 24, 2012 at 8:41 PM

Raycasting sounds like the right tool for the job TBH. I've found raycasts are quite fast and efficient. In fact I've been doing some tests for a particle engine and so far so good. Particle engine is like 1000 of your "bullets" that need to be updated every frame so as you can see.  More specifically been thinking of doing some Farseer and Mercury intergration. This thread got me started on the idea: