Or here is a slightly different take. Again no constant rate of rotation and you can’t boost and fire at the same time, but I feel it has a pleasing fluidity (YMMV). Again very rough and was written several years ago - I’ve updated the sprite assets to remove the deprecation warning.
-- touchcontrol
-- Use this function to perform your initial setup
function setup()
displayMode(FULLSCREEN)
touches={}
tsup={} --supplemental touch info
activetouches=0
spacedust={}
ship=physics.body(CIRCLE,10)
ship.x=WIDTH/2
ship.y=HEIGHT/2
ship.gravityScale=0
ship.linearDamping=0.3
shipangle=0
shipoffset=-90
shipsize=30
--bullets
bullet={}
bulletspeed=5
scale=1
globalx=0
globaly=0
bgx=WIDTH/2
bgy=HEIGHT/2
end
function draw()
globalx=ship.x
globaly=ship.y
background(40, 40, 50)
activetouches=0
oldesttouchid=0
longesttouch=0
for k,touch in pairs(touches) do
activetouches = activetouches + 1
tsup[touch.id].time = tsup[touch.id].time + 1
if tsup[touch.id].time>longesttouch then
longesttouch=tsup[touch.id].time
oldesttouchid=touch.id
shipangle=math.deg(math.atan2(touch.y-ship.y,touch.x-ship.x))
end
end
if activetouches==2 then
local thrust=2
ship:applyForce(vec2(thrust*math.cos(math.rad(shipangle)),thrust*math.sin(math.rad(shipangle))))
table.insert(spacedust,
{x=ship.x,y=ship.y,dir=shipangle+shipoffset+math.random(20)+170,
fade=175+math.random(50),size=2+math.random(5),speed=5+math.random(5)})
end
if ship.x<0 then ship.x=WIDTH end
if ship.x>WIDTH then ship.x=0 end
if ship.y<0 then ship.y=HEIGHT end
if ship.y>HEIGHT then ship.y=0 end
for i,b in pairs(bullet) do
sprite(asset.builtin.Tyrian_Remastered.Bullet_Fire_A,b.x,b.y)
b.x = b.x + scale*bulletspeed*math.sin(math.rad(-b.a))
b.y = b.y + scale*bulletspeed*math.cos(math.rad(-b.a))
if b.x>WIDTH or b.x<0 or b.y>HEIGHT or b.y<0 then
table.remove(bullet,i)
end
end
for s,d in pairs(spacedust) do
--add transparency to the space dust so it fades away
tint(240,120,150,d.fade)
pushMatrix()
translate(d.x,d.y)
sprite(asset.builtin.Cargo_Bot.Star_Filled,0,0,d.size,d.size)
popMatrix()
d.x = d.x + d.speed*math.sin(math.rad(-d.dir))
d.y = d.y + d.speed*math.cos(math.rad(-d.dir))
d.fade = d.fade -5
if d.fade<0 then
table.remove(spacedust,s)
end
end
tint(255)
pushMatrix()
translate(ship.x,ship.y) --executed 3rd -move sprite by x along x axis and y along y axis
rotate(shipangle) --executed 2nd - rotate sprite by 20 degrees anticlockwise
rotate(shipoffset)
sprite(asset.builtin.Space_Art.Part_Yellow_Hull_4,0,0,shipsize) --executed 1st - draw sprite at 0,0
popMatrix()
end
function touched(touch)
if touch.state == ENDED then
if tsup[touch.id].time<15 and activetouches==1 then
table.insert(bullet,{x=ship.x,y=ship.y,a=shipangle+shipoffset})
end
touches[touch.id] = nil
tsup[touch.id]=nil
else
touches[touch.id] = touch
if tsup[touch.id]==nil then
tsup[touch.id]={time=0}
end
end
end