We're basically talking about something as simple as plotting a line from one point to another, and then have another point travel along that line. Just like a bullet from a gun (or in this case, an orange dot from the nose of a spaceship).
A shot is created like this:
Code: Select all
table.insert(projectiles.playershots, {
start={ -- The starting position of the shot
x=player.x,
y=player.y
},
target = { -- Where we want the shot to go (x and y are mouse coordinates)
x=x,
y=y
},
position={ -- The current position of the shot
x=player.x,
y=player.y
},
direction = math.atan2((y-player.y+player.images.normal.height),(x-player.x+player.images.normal.width/2)), --The angle of the shot
v = 100 -- the speed
})
Code: Select all
local i = 1
while i <= table.getn(projectiles.playershots) do
projectiles.playershots[i].position.y = projectiles.playershots[i].position.y - math.abs( math.cos( projectiles.playershots[i].direction + math.rad( -90 ) ) * projectiles.playershots[i].v * dt )
projectiles.playershots[i].position.x = projectiles.playershots[i].position.x - math.sin( projectiles.playershots[i].direction + math.rad( -90 ) ) * projectiles.playershots[i].v * dt
i = i+1
end
It might not be obvious from the screenshot, but the angle of the shots is a bit off, and I can't figure out why. I'm doing something wrong, but for the life of me I can't figure out what. I've checked whether I'm just drawing the images wrong, or something dumb like that, but even when I replace them with just points it's still off. The offset seems to vary depending on the distance between the two point, so something must be wrong with what I'm doing.
I've never studied trigonometry, and man do I regret that now! It's a small miracle that I've even managed to get this far, but now I really need some help.