I am making a space shooter app and I have just started to implement enemy movement. I want the enemies to shoot lasers in the direction that they are facing and then travel at a constant speed at the angle the enemy was facing when it was shot. What is the best place to go for information on this?
I’ve made a number of games for the Corona Geek hangout:
http://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/ICanMakeThat
Among them is the most recent:
The player shoots in the direction the right joystick is pointing. However, this could easily be slaved to the direction the ship is flying.
Alternately, you can look at the turrets in this game:
http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/ICanMakeThat/TheyNeedToBeFed.zip
Whatever the case, you’re talking about a little vector math.
-- Pseudo code (close to correct based on math2d library) local vec = angle2Vector( enemy.rotation, true) vec = scaleVec( vec, laserSpeed ) laser.rotation = enemy.rotation laser:setLinearVelocity( vec.x, vec.y )
Thank you! This looks like what I am looking for! My main issue right now is to get the bullets to appear in front of the enemy ship (no matter what the rotation), any suggestions on this?
Is there a way I can say like “start firing the bullet from 1/10th of the way along the vector”?
If you are wanting to find a point-extended from the gun barrel… you can try something like this:
local base = display.newImage("gun.png") base.x = 100 base.y = 150 base.rotation = 144 local orb = display.newImage("bullit.png") orb.x = 100 orb.y = 100 local radius = (base.height \* .5) + (orb.height \* .5) function rotateAroundCenter() orb.x = base.x + radius\*math.cos(math.rad( (base.rotation - 90) )) orb.y = base.y + radius\*math.sin(math.rad( (base.rotation - 90) )) end local function onTouch(e) if e.phase == "ended" then base.rotation = base.rotation + 45 rotateAroundCenter() end end Runtime:addEventListener("touch", onTouch)
just change the radius factor to match wherever the end of the laser gun barrel is. I just used the texture size as a starting point, assuming you wanted the laser to be right at the tip of the barrel. You may have to experiment with different radius numbers to get the look you want.
The function used in this example, I think I found on the forums or somewhere else on the internet a long time ago. I do not understand the math specifically, but it worked for me in a tower defense app.
You should also check out the corona geek show #119 https://www.youtube.com/watch?v=RRiE2ut7QLM
RoamingGamer covers this concept much better then I can… I was unable to watch but a few minutes of it, and it appears to cover some concepts particular to your question.
Good Luck
Bob
I’ve made a number of games for the Corona Geek hangout:
http://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/ICanMakeThat
Among them is the most recent:
The player shoots in the direction the right joystick is pointing. However, this could easily be slaved to the direction the ship is flying.
Alternately, you can look at the turrets in this game:
http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/ICanMakeThat/TheyNeedToBeFed.zip
Whatever the case, you’re talking about a little vector math.
-- Pseudo code (close to correct based on math2d library) local vec = angle2Vector( enemy.rotation, true) vec = scaleVec( vec, laserSpeed ) laser.rotation = enemy.rotation laser:setLinearVelocity( vec.x, vec.y )
Thank you! This looks like what I am looking for! My main issue right now is to get the bullets to appear in front of the enemy ship (no matter what the rotation), any suggestions on this?
Is there a way I can say like “start firing the bullet from 1/10th of the way along the vector”?
If you are wanting to find a point-extended from the gun barrel… you can try something like this:
local base = display.newImage("gun.png") base.x = 100 base.y = 150 base.rotation = 144 local orb = display.newImage("bullit.png") orb.x = 100 orb.y = 100 local radius = (base.height \* .5) + (orb.height \* .5) function rotateAroundCenter() orb.x = base.x + radius\*math.cos(math.rad( (base.rotation - 90) )) orb.y = base.y + radius\*math.sin(math.rad( (base.rotation - 90) )) end local function onTouch(e) if e.phase == "ended" then base.rotation = base.rotation + 45 rotateAroundCenter() end end Runtime:addEventListener("touch", onTouch)
just change the radius factor to match wherever the end of the laser gun barrel is. I just used the texture size as a starting point, assuming you wanted the laser to be right at the tip of the barrel. You may have to experiment with different radius numbers to get the look you want.
The function used in this example, I think I found on the forums or somewhere else on the internet a long time ago. I do not understand the math specifically, but it worked for me in a tower defense app.
You should also check out the corona geek show #119 https://www.youtube.com/watch?v=RRiE2ut7QLM
RoamingGamer covers this concept much better then I can… I was unable to watch but a few minutes of it, and it appears to cover some concepts particular to your question.
Good Luck
Bob