defos icon indicating copy to clipboard operation
defos copied to clipboard

dx / dy issue

Open subsoap opened this issue 7 years ago • 6 comments

I'm working on a First Person Shooter style camera setup, and am using action.dx and action.dy - only problem is that the cursor lock / set functions of defos interferes with the build in dx/dy so that the dx/dy movement gets reversed after a mouse cursor moves and then is returned to where it is.

Then do we need to be able to get mouse x/y position and calculate dx/dy ourselves or should I be going about this another way?

subsoap avatar Jul 03 '18 14:07 subsoap

I've been having the same issue in #64. Unfortunately there's very little that we can do without input from the Defold devs.

dapetcu21 avatar Jul 03 '18 14:07 dapetcu21

What platform are you on?

dapetcu21 avatar Jul 03 '18 14:07 dapetcu21

I think then we'll have to be able to get the current mouse position within the view as DefOS function and calculate the dx/dy of the frame ourselves before re-positioning.

subsoap avatar Jul 03 '18 14:07 subsoap

I tested collecting the screen_x and screen_y values from on_input and then calculating dx/dy myself in a function called in on update based on them but had same issue as using the dx/dy from action.

subsoap avatar Jul 03 '18 15:07 subsoap

Ugh... I added get_cursor_pos to a local copy of defos to test with and it is still having the same issue with dx,dy.

Then I realized I was doing something wrong in the script with what I was setting the last frame's screen x and screen y to and now it works. I'll still finish up getting cursor pos on Windows and get a pr up today or tomorrow because it will be useful, and then you or someone else can make Mac version and then maybe it can solve the dx/dy issue there.

subsoap avatar Jul 03 '18 22:07 subsoap

The issue can be solved. Since the source code of Defold has been opened, you can do dirty hacks like below. So, we will be able to use builtin deltas action.dx and action.dy for first person mouse look.

#define GLFW_MOUSE_CURSOR 0x00030001

extern "C" {
    void glfwDisable(int token);
    void glfwEnable(int token);
}

static int mouse_lock(lua_State *L) {
    glfwDisable(GLFW_MOUSE_CURSOR);
    return 0;
}

static const luaL_reg Module_methods[] = {{"mouse_lock", mouse_lock}, {0, 0}};

static void lua_init(lua_State *L) {
    int top = lua_gettop(L);

    // Register lua names
    luaL_register(L, "glfw", Module_methods);

    lua_pop(L, 1);
    assert(top == lua_gettop(L));
}
-- mouse_look.script for rendercam

function init(self)
    msg.post(".", "acquire_input_focus")
    glfw.mouse_lock()

    self.look_speed = 0.25
    self.euler = vmath.vector3(0)
end

function update(self, dt)
    go.set(".", "euler.x", self.euler.y)
    go.set(".", "euler.y", self.euler.x)
end

function on_input(self, action_id, action)
    if action_id == nil then
        self.euler.x = self.euler.x - (self.look_speed * action.dx)
        self.euler.y = self.euler.y - (self.look_speed * -action.dy)
    end
end

Ref: https://github.com/defold/defold/blob/dev/engine/glfw/include/GL/glfw.h

aglitchman avatar Jul 16 '20 20:07 aglitchman