Thank you!
Been really annoying trying to figure this out, thank you for all your help. In case anyone wants to do the same thing, this is what i ended up with.
cursor script:
local DISPLAY_WIDTH = sys.get_config_int("display.width")
local DISPLAY_HEIGHT = sys.get_config_int("display.height")
local function screen_to_world(x, y, z, camera)--function that converts screen coordinates into world coordinates
local projection = go.get("/camera#camera", "projection")
local view = go.get("/camera#camera", "view")
local w, h = window.get_size()
-- The window.get_size() function will return the scaled window size taking into account display scaling
w = w / (w / DISPLAY_WIDTH)
h = h / (h / DISPLAY_HEIGHT)
local inv = vmath.inv(projection * view)
x = (2 * x / w) - 1
y = (2 * y / h) - 1
z = (2 * z) - 1
local x1 = x * inv.m00 + y * inv.m01 + z * inv.m02 + inv.m03
local y1 = x * inv.m10 + y * inv.m11 + z * inv.m12 + inv.m13
local z1 = x * inv.m20 + y * inv.m21 + z * inv.m22 + inv.m23
return x1, y1, z1
end
function init(self)
msg.post(".", "acquire_input_focus")-- makes sure the script will receive user input
self.vel = vmath.vector3()
sys.set_update_frequency(60)--fps cap
end
function on_input(self, action_id, action)
if not action_id or action_id == hash("touch") then
local worldx, worldy = screen_to_world(action.x, action.y, 0, "/camera#camera")-- converts mouse/touch screen position to world position
local world = vmath.vector3(worldx, worldy, 500)-- updates cursor position to the world position of mouse/touch position
go.set_position(world)
end
if action_id == hash("up") then--these just ensure the cursor moves when the player moves
self.vel.y = 150
elseif action_id == hash("down") then
self.vel.y = -150
elseif action_id == hash("left") then
self.vel.x = -150
elseif action_id == hash("right") then
self.vel.x = 150
end
end
function update(self, dt)
local pos = go.get_position()
pos = pos + self.vel * dt
go.set_position(pos)
self.vel.x = 0
self.vel.y = 0
end
Aiming script:
function init(self)
msg.post(".", "acquire_input_focus")--ensures the script gets input
self.target_position = vmath.vector3()
end
local DISPLAY_WIDTH = sys.get_config_int("display.width")
local DISPLAY_HEIGHT = sys.get_config_int("display.height")
local function screen_to_world(x, y, z, camera)--function that converts screen coordinates into world coordinates
local projection = go.get("/camera#camera", "projection")
local view = go.get("/camera#camera", "view")
local w, h = window.get_size()
-- The window.get_size() function will return the scaled window size taking into account display scaling
w = w / (w / DISPLAY_WIDTH)
h = h / (h / DISPLAY_HEIGHT)
local inv = vmath.inv(projection * view)
x = (2 * x / w) - 1
y = (2 * y / h) - 1
z = (2 * z) - 1
local x1 = x * inv.m00 + y * inv.m01 + z * inv.m02 + inv.m03
local y1 = x * inv.m10 + y * inv.m11 + z * inv.m12 + inv.m13
local z1 = x * inv.m20 + y * inv.m21 + z * inv.m22 + inv.m23
return x1, y1, z1
end
local function look_at(target_position)
local my_position = go.get_position()--gets player's own position
local angle = math.atan2(my_position.x - target_position.x, target_position.y - my_position.y)-- calculates the angle that this object has to rotate to look at the target position
go.set_rotation(vmath.quat_rotation_z(angle))-- sets rotation as a quaternion
end
function update(self, dt)
local cursorpos = go.get_position("crosshair")
look_at(cursorpos)
end
Thank you britzl!