bullets in RTS?

Started by
2 comments, last by Joakim_ar 17 years, 7 months ago
Hi In my 2d-rts i will have guys with guns (duh) and i want fire to be realistic in a way that if you fire in a crowd and miss probably someone else will be hit (and harder to hit in distance etc). So i want "real bullets" My idea is this: 1. Upon firing, missfactor is calculated with shooter skill, weaponStat, distance, targetMovement etc. This factor is then used to randomise angle of the "bullet". 2. Bullet path is calculated with a line (directly, bullets do not live over several frames) and the closest object within this line will be hit. 3. If within view, a trail/line will be drawn between shooter and target. Is this a good idea? And how can i check if a unit(a rectangle) is being cut by a line? Thanks a bunch E
Advertisement
Intersection between a line and a quad is fairly simple (naively, it is the intersection between the line and 4 segments, and segment intersection is very well documented on the internet). You can have a look here or here for some math background (and C++ code) - or just google for segment intersection.

Now, your idea is quite good, but using a line/quad intersection might not be precise enough. You can add more precision (but still be simple) by using bounding circles or ellipses (this page has some code about many intersection methods).

Regards,



It should work fine.

>> And how can i check if a unit(a rectangle) is being cut by a line?

It depends on your implementation.

If you are grid based, just step along each tile using your favorite line drawing algorithm. If there is a unit in the tile, check for a hit against that particular unit (*see below).

If you are not grid based, you will need to work with whatever spatial system you have created to determine the line that the projectile travels. Once you have that list of sprites, check for a hit against that particular uint (*see below).



(*) When checking intersection...

I believe Emmanuel D's suggestion of using more complex bounding shapes is probably overkill for this style of 2D system.

Just Trace a line through the unit's sprite to see if it hits. Checking against a prebuilt bitmask is very quick especially if your sprites are small. When a shot is fired, you will probably only be checking against a very small number of sprites, so a simple DDA or more complex Bresenham's line algorithm would both work.
Then again, a bounding circle (which is in no way complex) might even be easier to implement than pixel perfect checking (which will require you to check pixels along lines and create and keep special colission tiles in memory), as the circle/line intersection is fairly simple to implement.
In case you were wondering what to put in your next christian game; don't ask me, because I'm an atheist, an infidel, and all in all an antitheist. If that doesn't bother you, visit my site that has all kinds of small utilities and widgets.

This topic is closed to new replies.

Advertisement