## Help for create bullet of tank

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
lucstanislash
Prole
Posts: 3
Joined: Mon Jan 21, 2019 7:15 pm

### Help for create bullet of tank

Hello,
I begin in the programmation and in my code, I can't get my bullet of the end of the cannon and not deflect it when I turn the tank. Do you have a code solution with a explication.

This is the code of my game :
Mon jeu de tank.zip

PS: sorry for my bad english (I'm french)

NotARaptor
Citizen
Posts: 56
Joined: Thu Feb 22, 2018 3:15 pm

### Re: Help for create bullet of tank

Salut, bienvenu aux forurms.

Le problème ici est que l'obus ne garde pas en mémoire sa direction/vélocité, il le recalcule à chaque update, donc il suit la tourelle.

La solution c'est d'ajouter les membes dx et dy à l'objet obus, et de les garder au moment du tir.

De plus, il me semble que l'angle tourelle.angle est en degrés alors que les fonctions cos et sin attendent un angle en radians.

Je crois que le fonctionnement principal du tir et de l'obus est à refaire, mais déjà de garder la vélocité initiale au lieu de la recalculer à chaque fois sera un bon début!

-----

Hi and welcome to the forums

The problem here is that the shell is not saving its direction/velocity; it's recalculating it every update, so it follows the turret.

The solution is to add two members "dx" and "dy" to the shell object, and set them at the moment you fire.

Another point is that the angle tourelle.angle seems to be in degrees whereas the sin and cos functions expect an argument in radians.

I do think the main shell firing system needs to be reworked, but just keeping the initial velocity instead of recalculating it will be a great first step.

lucstanislash
Prole
Posts: 3
Joined: Mon Jan 21, 2019 7:15 pm

### Re: Help for create bullet of tank

I have make what do you propose but the bullet don't appear in the love window when I presse on the x keyboard !
What is the problem ?
tanki battle.zip

NotARaptor
Citizen
Posts: 56
Joined: Thu Feb 22, 2018 3:15 pm

### Re: Help for create bullet of tank

You need to work out your logic better; it's a bit confusing.

The lifecycle of the projectile (obus) should be:

- Does not exist / is inactive - obus.lance is false - do nothing
- Is being fired right now : set x and y coordinates, set dx and dy, set obus.lance to true
- Is being fired : obus.lance is true - updated and move the projectile, detect collisions, set obus.lance to false if it collides or goes offscreen

For the "is being fired right now", you should either do this in love.keypressed. In love.update, checking love.keyboard.isDown("x") checks if the key is down right now - not if it's just been pressed this frame.

Remember that once the projectile is fired, its angle and velocity WILL NOT CHANGE - unless you want something like a heat-seeking missile.

Something valuely like this (not tested, just writing it here)

Code: Select all

function love.update(dt)
-- All the other stuff here
if obus.lance then
obus.x = obus.x + obus.dx * dt
obus.y = obus.y + obus.dy * dt
if obus.x < 0 or obus.x > love.graphics.getWidth() or obus.y < 0 or obus.y > love.graphics.getHeight() then
obus.lance = false
end
end
end

function love.keypressed(key)
if key == "x" then
obus.x = tank.x
obus.y = tank.y
obus.angle = tank.angle + tourelle.angle
obus.lance = true
end
end

function love.draw()
-- Other stuff here...
if obus.lance then
love.graphics.draw(obus.imgObus, obus.x, obus.y, math.rad(obus.angle + 90), 1, 1, 155 , 155)
end
end

Hope this helps

[S'il y a des mots ou des phrases que tu ne comprends pas, dis-le moi, je peux l'écrire en français aussi]

lucstanislash
Prole
Posts: 3
Joined: Mon Jan 21, 2019 7:15 pm

### Re: Help for create bullet of tank

It works !! Thank you so much !!
It's really cool and actually it's just that I do not have to set up when obus.lance = true or false.
But I would like to know when he was better use love.keypressed or love.keyboard.isDown ?

I now have another problem is that I tried to make sure that we can see several bullet in the window, clearly left the bullet launched will destroy and in the meantime launched another bullet.

I followed the explication of this topic : viewtopic.php?t=82527
but it does not work! What's the problem ? In fact, I do not really understand how it works ?
Can you help me ?

this is my work with my try :
tanki battle.zip

NotARaptor
Citizen
Posts: 56
Joined: Thu Feb 22, 2018 3:15 pm

### Re: Help for create bullet of tank

I figured this would be your next question!

Your issue is that you're trying to treat the variable "obus" both as an array of objects, and as a single object at the same time - which obviously doesn't work.

Of course, the noun "obus" in French is both singular and plural, so that doesn't really help clarify the situation!

You need to separate out when you're dealing with the collection, and when you're dealing with an individual projectile, and use different variable names.

When you want to CREATE a new projectile (firing), create a new object instance and add it to the global array - probably with table.insert()

When you want to DESTROY a projectile (it goes out of range, or hits something), you need to remove it from the global array - with table.remove()

If you're iterating the array with the intention of removing one or more items, make sure you iterate in reverse.

### Who is online

Users browsing this forum: Google [Bot] and 8 guests