LoginSignup
0
0

More than 1 year has passed since last update.

ストワで始めるLua 第九回 アクティブソナー

Posted at

activve sonar

--Initialize
pi=math.pi
pi2=pi*2
dt=1/60
waterSoundSpeed=1480*dt
range=property.getNumber('range')

behindAngle=math.rad(3) --Backward angle not visible.

activeData={}
pingTick=0
isPing=false

function onTick()

	isPingPush=input.getBool(19)
	
	if isPingPush and isPing==false then
		isPing=true
		activeData={}
		pingTick=0
	end

	if isPing then
		--Sonar data input
		for i=0,15 do
			if input.getBool(i+1) then
				local x,y
				x=input.getNumber(i*2+1)*pi2
				y=input.getNumber(i*2+2)*pi2
				if pi-behindAngle >= x and -pi+behindAngle <= x then
					table.insert(activeData,{distance=(pingTick*waterSoundSpeed)/2,bearing=x,elevation=y})
				end
			end
		end
		
		pingTick=pingTick+1
	end

	if (pingTick*waterSoundSpeed)/2 >range then
		isPing=false
	end
	output.setBool(1,isPing)
end

function onDraw()
	local w,h,cw,ch,radius
	--resolution
	w=screen.getWidth()
	h=screen.getHeight()
	--center
	cw=w/2
	ch=h/2
	--display sonars circle radius
	radius=h/2-3
	
	--drawing echo
	screen.setColor(0,255,0,128)
	for i=1,#activeData do
		local echoX,echoY
		echoX=cw+(activeData[i].distance/range)*radius*math.sin(activeData[i].bearing)
		echoY=ch-(activeData[i].distance/range)*radius*math.cos(activeData[i].bearing)
		screen.drawCircleF(echoX,echoY,2)
	end
	
	--drawing circle
	screen.setColor(0,255,0)
	screen.drawCircle(cw,ch,radius)
	screen.drawText(0,0,string.format("%.0f",range))
end
0
0
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
0