XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Bullet Firing for Tower Defense Game

Last post 7/1/2008 7:45 PM by Baskinein. 4 replies.
  • 7/1/2008 9:40 AM

    Bullet Firing for Tower Defense Game

    Hey all, I've just started playing around with XNA to get used to some very basic games programming and i figured I'd start on my favorite game type, a tower defense game!

    I'm pretty happy with what I have so far, but my main problem is that when a tower fires a bullet it seems to just be missing the enemy every time, like the aiming is off. I'm just not sure what I've done wrong! If you run the provided source code and place a tower you'll see what I mean.

    Source Code

    Sorry the code is a bit messy but I'm just getting started! I really appreciate any help!

     

  • 7/1/2008 11:27 AM In reply to

    Re: Bullet Firing for Tower Defense Game

    I'm not on my XNA enabled laptop @ the moment so I can't check out the code ... but can you describe the behavior a bit? is it not going in the right direction at all? or is it just missing the enemy because the enemy is moving? If it's missing because the enemy is moving, then you might want to calculate how far the enemy is from the tower, where the enemy is likely to be by the time the bullet reaches it, and then aim at that location.
    Joel Martinez - XNA MVP
    Blog: http://codecube.net
    XNA Unit Testing: Scurvy Test
  • 7/1/2008 5:48 PM In reply to

    Re: Bullet Firing for Tower Defense Game

    your source doesn't load, I'd help, but I can't see it
  • 7/1/2008 7:00 PM In reply to

    Re: Bullet Firing for Tower Defense Game

    Speak of the devil. I'm almost done with a tower defense game for the 360. I have the engine and user interface completely finished, I'm just working on creating new towers, creeps, game types and balancing everything. :) (sorry, I'm excited to be almost ready to release it for testing).

     

    As for the bullets, I figured that since the tower are supposed to shoot the creeps, and it really isn't dependent on the player's skill, the bullets should just kind of 'follow' the creep until it hits it. The bullet should be moving fast enough to make the effect barely noticeable. Just get the angle between the creep that the bullet is locked on to every frame and move the bullet at that speed in that direction. When it gets within a certain distance of the creep, it has shot the creep. Mark the bullet for deletion and apply damage to the creep that it was locked onto. It works great, is easy on processing power (especially when you are running a couple thousand creeps at once), and it looks completely fine. No one who has been testing my game has noticed, or even if they did, haven't cared about the little bit of curve that the bullets take when they are moving really slow.

     

    Also, if you run into anymore problems, feel free to message me and I'll help you out the best that I can.
  • 7/1/2008 7:45 PM In reply to

    Re: Bullet Firing for Tower Defense Game

    Make sure that the algorithm you are using is a chasing interception algorithm. Something like this...

    Closing velocity, Vc = Vprey – Vpredator

    Range to close, Sc = Pprey – Ppredator

    Time to close, Tc = | Sc | / | Vc |


    Calculate intercept position:

    Intercept position = Pprey + Vprey x Tc

    This way you lead the target. I didn't look at your code, but from the sounds of it this might be a cause to your problems- you're shotting to were the prey was at the time of the fire and not predicting were the prey is going to be. Hopefully this helps!

    Game Dev Blog: http://punipunikingdom.blogspot.com/
    Programming Blog: http://levelupprogramming.blogspot.com/
Page 1 of 1 (5 items) Previous Next