forums

TNT Basic Forums > Programming Q&A
Hitscan bullets
< Last Thread     Next Thread >
Author
Thread        Post A Reply

11-17-2010 17:19

Posted by:
Yakkers

Find more posts by Yakkers

For anyone not familiar with hitscan, take a look here: http://en.wikipedia.org/wiki/Hitscan

To put it simply, the idea is for the program to draw a line from the player in the direction he is aiming, sense if any enemies are in the line, and (if there are more than one) damage whichever is closest to the player.

My orignal idea was to use a traditional moving bullet object, but have it make the entire movement before rendering the frame. But the problem with this is it would have to check for collisions (I'll be using about 30-50 enemies) many times between each frame, which would seriously hurt the program's efficiency.

My other idea was to draw a line of a color I won't be using anywhere (pink for example) and use the "pixel color" command to sense if each enemy is touching it. Although since the command is for only one pixel, I'd have to repeat the check for each pixel in a box around each enemy, which may end up being even less efficient than the other method.

So I feel like I can pretty much cross off both those ideas, can anyone think of a better method, possibly using mathematics? Or does anyone know how actual FPS hitscans are coded?

By the way, this is for a 2D top-down perspective, aimed with the mouse.

11-18-2010 01:03

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

Yup, it's me again...

OK, if you actually want to physically 'draw' the targeting line and have it stop when it hits an obstacle then I'm afraid there is only one answer, It used to be called 'LINEA' (or 'Line A') back in my day and it uses - you guessed it - Bresenham's mid point line algorithm (Google it if you like, I didn't just post that for fun, and no, I'm not on commission ;o)

You'd need to set up a 'phantom' bullet (just coordinates, no images), fire it off towards the mouse cursor and check for sprite proximity on each loop. If this detects a hit you pass these coordinates to your line drawing call with the current coordinates as the end point. In over 20 years of doing this I've not found a better way (yes I'm old), and Graphics card manufacturers use the same theory, so i'm pretty close to a definitive answer...

'Till next time...

Danny (nod the mod)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.