0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ストワで始めるLua第6回偵察レーダーのコード

Last updated at Posted at 2023-04-29

新レーダーで単純な偵察レーダーを作りました。
動画はこちら

search_radar.lua
pi=math.pi
pi2=pi*2
sin=math.sin
cos=math.cos
data={}
--delete frame
removeFrame=property.getNumber("remove frame")

function onTick()


	angle=input.getNumber(4)*pi2 --radar rotation
	range=input.getNumber(8) --display range
    --radar data {distance, azimuth , elevation, frame}
	for i=0,7 do
		if input.getBool(i+1) then
			table.insert(data,{input.getNumber(i*4+1),input.getNumber(i*4+2)*pi2,input.getNumber(i*4+3)*pi2,removeFrame})
		end
	end

    --remove echo
	for i=#data,1,-1 do
		data[i][4]=data[i][4]-1
		if data[i][4]<=0 then
			table.remove(data,i)
		end
	end
end

function onDraw()
	local cw,ch,radius,lineX,lineY,echoX,echoY,colorG

	w=screen.getWidth()
	h=screen.getHeight()
	
    --center
	cw=w/2
	ch=h/2
	
    --display radius
	radius=h/2-3
	
    --radar beam 
	lineX=cw+radius*sin(angle)
	lineY=ch-radius*cos(angle)
	
    --draw radar echo
	for i=1,#data do
		if data[i][1]<range then
			echoX=cw+(data[i][1]/range)*radius*sin(data[i][2])
			echoY=ch-(data[i][1]/range)*radius*cos(data[i][2])
			colorG=255*(data[i][4]/removeFrame)
			screen.setColor(0, colorG, 0)
			screen.drawCircleF(echoX,echoY,2)
		end
	end
	
	screen.setColor(0, 255, 0)
	screen.drawText(0,0,string.format("%.0f",range))
	screen.drawCircle(cw,ch,radius)
	screen.drawLine(cw,ch,lineX,lineY)

end

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?