走っている最中の車の情報を取得する方法を表示するlua アプリを作ってみました。
車のステータス情報を取得する方法のサンプルになればと思います。
(サンプルに含まれるもの)
自車のtrack上の座標
自車の向き
自車の左右、前後のGの大きさ
自車で使用しているcar skin名の取得
track上を走るすべての車の以下の情報
・カーナンバー
・車の名前
・自車と対象車との距離(m)
・自車から見た対象車の相対座標(m)
スクリプトの中身はこれです。
local xforce, zforce
local max
max = 0
local sp, tp
local cars = {}
local totalCars = ac.getSim().carsCount
local ifor i = 0 , totalCars - 1 do
cars[i] = {}
cars[i].name = ac.getCarName(i)
cars[i].number = ac.getCarLeaderboardPosition(i)
end
function update(dt)
xforce = math.abs(ac.getCar().acceleration["x"])
zforce = math.abs(ac.getCar().acceleration["z"])
if xforce > max then
max = xforce
endfor i = 0, totalCars - 1 do
tp = ac.getCarState(i+1).position
cars[i].dist = math.sqrt ( (sp.x - tp.x)^2 + (sp.y - tp.y)^2 + (sp.z - tp.z)^2 )
cars[i].rx = tp.x - sp.x
cars[i].ry = tp.y - sp.y
cars[i].rz = tp.z - sp.z
end
endfunction script.windowMain(dt)
sp = ac.getCar().position
ui.text('position: ' .. sp.x .. ' , ' .. sp.y .. ' , ' .. sp.z)
ui.text('rotation: x= ' .. ac.getCar().compass)
ui.text('x-force: ' .. xforce .. ' / ' .. max)
ui.text('z-force: ' .. zforce)
ui.text('car skin name: ' ..ac.getCarSkinID())
ui.text('--- entry cars ---')
for i = 0 , totalCars - 1 do
ui.text(i .. ' carNumber: ' .. cars[i].number .. ' : ' .. cars[i].name .. ' dist: ' .. cars[i].dist)
ui.text(' relative coordinate: ' .. cars[i].rx .. ' , ' .. cars[i].ry .. ' , ' .. cars[i].rz)
end
end
ちなみに全車の情報のうち、一番最初( i=0)は自車の情報です。
luaアプリで情報を取得して表示させる方法がわかると、自分が取得したい情報がうまくとれているか、意図する値になっているかを確認できるようになるので、便利です。
ついでに上記を実装したlua アプリはこちらです。
いろいろ試してみたい方向けです。