Best Practice for firing out of a cannon

Of course, I’m not referring to firing myself out of a cannon. Let’s get that cleared up right from the start :wink:

I am well on the way to creating my tower defence game. My cannon turrets now need to fire at a target. Now I don’t think I need to use physics here because the turret rotates to point at the target, and remains “locked” until it is either destroyed or goes out of range… Basically, I have my direction sorted.

So… I am thinking of creating multiple spheres, giving them a velocity and setting them to travel a certain distance until they reach their target. Then a quick explosion, and this then repeats depending on the firing rate. This all seems logical and do-able, but there could be 20 or more turrets busily firing 2-3 spheres at a time here, and I’m wondering if I might be going down the wrong road early on.

For instance, is their any mileage in NOT using spheres, but an orthographic circular image that gives the impression of moving towards the target? I wouldn’t even know where to start with this, but it seems much more efficient to me. I can also see this being a cleaner approach if I were to use a machine-gun effect.

I’m already reading my post and wondering if it’s going to make much sense. I hope so. I am really after feedback from somebody that has done something similar. Is firing 60-100 spheres around the screen going to be ridiculously expensive? From a PC requirements spec, I would say I’m looking to get this running using pretty standard hardware (say, an i3 processor using its internal graphics processor).

Thanks in advance.

don’t move around actual objects to check for collisions, use raytests that you perform over multiple frames, determining where the bullet would be and if it would hit anything

Thanks Normen, but I’m not ever actually doing any collision checking. I’m going to send my little sphere’s in a direction for a known distance, and then get them to explode. That wasn’t quite my question.

I’m more worried about the number of spheres that are going to be created here, and whether or not there’s an optimized way of handling this. I’m already thinking that I should have a number of sphere geometries and just re-use them when they’ve reached their target.

Any other ideas?

… so your bullets can go through walls? for object count use the normal countermeasures, e.g. the BatchNode.

No they won’t travel through walls, but I do know how far they need to travel (I’ll use the distance method from the cannon to the target). They’ll be travelling fast enough (and the targets travelling slow enough), that I don’t need collision checking. They may go through the target slightly (or even fall short slightly), but as long as it’s close enough to be acceptable to the eye then I’ll be happy.

BatchNode eh? Now that does sound promising. Thank you.

So how do you avoid that they go through walls if you do no collision checks :?

I don’t recall me saying there were any walls !

It’s a tower defence game, so in the majority of cases, it will be line-of-sight shooting. My only concern is that one turret could potentially shoot through another turret to get to its target. However, I do want both turrets firing if possible, so I am going to get round this with a crafty terrain layout (ie further turrets will be higher).

1 Like

Duh, well obviously I didn’t mean “walls” literally. So your projectiles just go through everything, thats what I thought.

as it seems to me you don’t need any collision checks at all. Every shot is auto hit so the only things in question is can you render all those spheres (like 200 at a time?). Yes, why not, that is not so many.

Do you want any lighting etc on your cannon balls? If so then really they do need to be at least some sort of 3d shape.

If not then you could indeed just use a simple square (or even triangle) and uv map a texture with a picture of a cannonball onto it. You just need some logic in the square movement to keep it pointed towards the camera.

To be honest though I think you are over-thinking it. Try it first, and see what it looks like and how bad the speed is. Once you see that you know if you need to do anything at all.

Guys, that’s excellent and exactly what I was after. I do appreciate I am probably over-complicating my thought processes. I juse wondered if anyone would go “STOP! Don’t fire that many projectiles you lunatic!”. Turns out that’s not the case, so happy days all round.



Have you tried the canonball demo in the tutorials? Those cannonballs are full physics objects as are all the bricks and it copes with that…

I’m developing a tower defense game as well.

As soon as I got the removal of objects (when they disappear from the game, eg. projectiles, creeps etc) correct, there was no problem placing 50-100 turrets, all firing projectiles every second (850 fps). The projectiles are physical and have a limited lifespan after which they are removed. If a creep is hit, the projectile is also removed.

Also, taking advantage of the PhysicsCollision groups makes it easy to define which objects in the game can physically hit which other objects.

I’m aiming for a game that could well have 1000 turrets on screen at the same time, and I dont think it will be a problem.

Best of luck to you

@richard_hawkes said:
Of course, I'm not referring to firing myself out of a cannon. Let's get that cleared up right from the start ;)

Damn, the thread title was so promising...