FreeSpace Releases > Scripting Releases
remote controlled turrets revisited
Nuke:
i revamped the script to run on current builds. fly a ship with a turret01a (medusa works) while using this script. switch to outside view. now you can manipulate and fire the turret with the mouse. now there is a problem im just not figuring out. so long as your ship is in its default orientation (its position as placed in fred) it works fine. but you roll or turn into some funky position and your aim goes way off. anyone have any ideas?
--- Code: ---#Global Hooks
$GameInit:
[
w = gr.getScreenWidth()
h = gr.getScreenHeight()
cx = w/2
cy = h/2
if w >= 1024 then
wf = w / 1024
hf = h / 768
awh = (wf + hf) / 2
hires = true
else
wf = w / 640
hf = h / 480
awh = ((wf + hf) / 2) * 0.625
hires = false
end
mousejoy = function(deadzone) --mousejoy function, returns a value from -1 to 1 depending on the mouse's position
local X = io.getMouseX()
local Y = io.getMouseY()
if hires then
X = (X / 511.5) - 1
Y = (Y / 383.5) - 1
else
X = (X / 319.5) - 1
Y = (Y / 239.5) - 1
end
local Mx = X --theese are for the menu, -1 to 1 without deadzone applied
local My = Y
local tweak = 1 / (1 - deadzone)
if X < deadzone and X > -deadzone then
X=0
else
if X > 0 then
X = (X - deadzone) * tweak
elseif X < 0 then
X = (X + deadzone) * tweak
end
end
if Y < deadzone and Y > -deadzone then
Y=0
else
if Y > 0 then
Y = (Y - deadzone) * tweak
elseif Y < 0 then
Y = (Y + deadzone) * tweak
end
end
return X, Y, Mx, My
end
mousegauge = function(eks,why) --just a bunch of loops to draw the various hashes for the gauges
gr.setColor(150,255,150,200) --big hashes
for i=cx-(54*awh), cx+(54*awh), 12*awh do
gr.drawLine(i,cy - (108*awh),i,cy - (96*awh))
end
for i=cy-(54*awh), cy+(54*awh), 12*awh do
gr.drawLine(cx - (108*awh),i,cx - (96*awh),i)
end
gr.setColor(50,255,50,150) --small hashes
for i=cx-(48*awh), cx+(48*awh), 12*awh do
gr.drawLine(i,cy - (108*awh),i,cy - (100*awh))
end
for i=cy-(48*awh), cy+(48*awh), 12*awh do
gr.drawLine(cx - (108*awh),i,cx - (100*awh),i)
end
gr.setColor(200,200,255,255) --indicators
gr.drawCircle(7.5*awh,cx+(eks*54*awh),cy-(104*awh))
gr.drawCircle(7.5*awh,cx-(104*awh),cy+(why*54*awh))
end
]
$Simulation:
[
player = mn.Ships["Alpha 1"]
X,Y,U,V = mousejoy(0.1)
if player:isValid() then
pgun = player["turret01a"]
ori = pgun.Orientation
ori.h = ori.h + X *2*ba.getFrametime()
pgun.Orientation = ori
ori = pgun.GunOrientation
ori.p = ori.p + Y *2*ba.getFrametime()
if ori.p > 0 then
ori.p = 0
elseif ori.p < -math.pi/2 then
ori.p = -math.pi/2
end
pgun.GunOrientation = ori
if io.isMouseButtonDown(MOUSE_LEFT_BUTTON) then
pgun:fireWeapon(1, 1000)
end
end
]
$HUD:
[
mousegauge(X, Y)
]
#End
--- End code ---
Herra Tohtori:
--- Quote from: Nuke on January 18, 2007, 06:44:41 am ---i revamped the script to run on current builds. fly a ship with a turret01a (medusa works) while using this script. switch to outside view. now you can manipulate and fire the turret with the mouse. now there is a problem im just not figuring out. so long as your ship is in its default orientation (its position as placed in fred) it works fine. but you roll or turn into some funky position and your aim goes way off. anyone have any ideas?
--- Code: ---#Global Hooks
$GameInit:
[
w = gr.getScreenWidth()
h = gr.getScreenHeight()
cx = w/2
cy = h/2
if w >= 1024 then
wf = w / 1024
hf = h / 768
awh = (wf + hf) / 2
hires = true
else
wf = w / 640
hf = h / 480
awh = ((wf + hf) / 2) * 0.625
hires = false
end
mousejoy = function(deadzone) --mousejoy function, returns a value from -1 to 1 depending on the mouse's position
local X = io.getMouseX()
local Y = io.getMouseY()
if hires then
X = (X / 511.5) - 1
Y = (Y / 383.5) - 1
else
X = (X / 319.5) - 1
Y = (Y / 239.5) - 1
end
local Mx = X --theese are for the menu, -1 to 1 without deadzone applied
local My = Y
local tweak = 1 / (1 - deadzone)
if X < deadzone and X > -deadzone then
X=0
else
if X > 0 then
X = (X - deadzone) * tweak
elseif X < 0 then
X = (X + deadzone) * tweak
end
end
if Y < deadzone and Y > -deadzone then
Y=0
else
if Y > 0 then
Y = (Y - deadzone) * tweak
elseif Y < 0 then
Y = (Y + deadzone) * tweak
end
end
return X, Y, Mx, My
end
mousegauge = function(eks,why) --just a bunch of loops to draw the various hashes for the gauges
gr.setColor(150,255,150,200) --big hashes
for i=cx-(54*awh), cx+(54*awh), 12*awh do
gr.drawLine(i,cy - (108*awh),i,cy - (96*awh))
end
for i=cy-(54*awh), cy+(54*awh), 12*awh do
gr.drawLine(cx - (108*awh),i,cx - (96*awh),i)
end
gr.setColor(50,255,50,150) --small hashes
for i=cx-(48*awh), cx+(48*awh), 12*awh do
gr.drawLine(i,cy - (108*awh),i,cy - (100*awh))
end
for i=cy-(48*awh), cy+(48*awh), 12*awh do
gr.drawLine(cx - (108*awh),i,cx - (100*awh),i)
end
gr.setColor(200,200,255,255) --indicators
gr.drawCircle(7.5*awh,cx+(eks*54*awh),cy-(104*awh))
gr.drawCircle(7.5*awh,cx-(104*awh),cy+(why*54*awh))
end
]
$Simulation:
[
player = mn.Ships["Alpha 1"]
X,Y,U,V = mousejoy(0.1)
if player:isValid() then
pgun = player["turret01a"]
ori = pgun.Orientation
ori.h = ori.h + X *2*ba.getFrametime()
pgun.Orientation = ori
ori = pgun.GunOrientation
ori.p = ori.p + Y *2*ba.getFrametime()
if ori.p > 0 then
ori.p = 0
elseif ori.p < -math.pi/2 then
ori.p = -math.pi/2
end
pgun.GunOrientation = ori
if io.isMouseButtonDown(MOUSE_LEFT_BUTTON) then
pgun:fireWeapon(1, 1000)
end
end
]
$HUD:
[
mousegauge(X, Y)
]
#End
--- End code ---
--- End quote ---
It sounds like you're missing a co-ordinate conversion from ship-relative to global XYZ co-ordinates in some point. It doesn't matter as long as ship's axes are aligned with global co-ordinate axes, but when they arent, it screws things up.
What does the turret orientation function do? Does it return turret's alignment vector in relation to global co-ordinates or in relation to ship-relative co-ordinates?
Nuke:
ok, all the important stuff is in $Simulation: everything else is just my usual vars for multires and mouse functions, same from my mouse script. all they do is return mouse coords in a -1 to 1 format.
essentially it lets you move your turret, base with mouse x and gun with mouse y. pgun.Orientation is the orientation of the turret base in p,b,h. its read, stored in a var, which is modified then put back. this is for the turret base yaw. it does the same thing for the barrel object, pgun.GunOrientation, but it locks it to 90 degrees. what i dont understand is why the turret rotation can work fine, yet the firing normal is somehere else. as far as i know, GunOrientation is also supposed to affect the turret normal.
WMCoolmon:
Basically it looks like the problem is in aiturret.cpp
--- Code: ---ship_get_global_turret_gun_info(&Objects[parent_objnum], ss, &gpos, &gvec, use_angles, &predicted_enemy_pos);
// Fire in the direction the turret is facing, not right at the target regardless of turret dir.
vm_vec_sub(&v2e, &predicted_enemy_pos, &gpos);
--- End code ---
"gvec" appears to be the direction that the turret is actually facing. It's used for checks about whether the weapon can fire or not. However, the actual firing vector used is the vector from the turret to the enemy. Because the decision to fire is based on the actual turret position, it doesn't cause a gross inaccuracy (Like firing from the turret's ass), but if you're seeing a minor inaccuracy in your tests, that might just be it.
I think I might be able to fix this easily, but I have to proceed carefully, since this particular segment of code is used everytime a turret is fired, and the last time turrets were broken, nobody spoke up for months. :rolleyes:
Nuke:
well when it all merges youl also have to shuffel in your turret changes with bobs. turrets will just need to be scrutinized more when code freeze time comes. if it helps i got a screenshot of the orientation matric being applies, the top 3 rows are for the base and the bottom 3 are for the gun. the first 3 rows are the matrix row 4 is the p'h'b and row 5 is row 4 converted to degrees. i was thinking that the matrix was merely incomplete, but if it was then why is the turret rotating properly, regaurdless of the ships orientation.
[attachment deleted by admin]
Navigation
[0] Message Index
[#] Next page
Go to full version