diff --git a/Engine.cc b/Engine.cc index 4f710f6..54400ee 100644 --- a/Engine.cc +++ b/Engine.cc @@ -53,6 +53,18 @@ static void debug_hook(lua_State * L, lua_Debug * debug) g_engine->debug_hook(debug); } +static void normalize(dVector3 vec) +{ + float len = vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]; + if (! FP_EQ(len, 1.0)) /* normalize if necessary */ + { + len = sqrtf(len); + vec[0] /= len; + vec[1] /= len; + vec[2] /= len; + } +} + #define DEBUG_GL_ERROR #ifdef DEBUG_GL_ERROR #define checkGLError() checkGLErrorLine(__FUNCTION__, __LINE__) @@ -642,7 +654,20 @@ refptr< vector > Engine::pickObjects(int x, int y) initial_direction[2] = m_video.getHeight() / 2 - y; dMultiply0(rotated_direction, initial_direction, r, 1, 3, 3); + normalize(rotated_direction); /* TODO: finish */ + refptr< vector > args = new vector(); + args->push_back(0.5); + for (float d = 3.0; d < 10.0; d += 2.0) + { + int o = addObject(false, false, OdeWorld::SPHERE, args); + m_objects[o]->setPosition( + m_center[0] + d * rotated_direction[0], + m_center[1] + d * rotated_direction[1], + m_center[2] + d * rotated_direction[2]); + } + + return new vector(); } void Engine::debug_hook(lua_Debug * debug) diff --git a/ag.cc b/ag.cc index 481566e..8aab2f7 100644 --- a/ag.cc +++ b/ag.cc @@ -47,6 +47,7 @@ namespace ag { "loadModelSpecify", loadModelSpecify }, { "loadTexture", loadTexture }, { "next", next }, + { "pickObjects", pickObjects }, { "print", print }, { "println", println }, { "registerEventHandler", registerEventHandler }, @@ -82,6 +83,16 @@ namespace ag lua_pcall(L, 0, 0, 0); } + int pickObjects(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 2 && lua_isnumber(L, 1) && lua_isnumber(L, 2)) + { + g_engine->pickObjects(lua_tointeger(L, 1), lua_tointeger(L, 2)); + } + return 0; + } + static void print_val(lua_State * L, int index) { int type = lua_type(L, index); diff --git a/ag.h b/ag.h index b7f00b8..f763098 100644 --- a/ag.h +++ b/ag.h @@ -26,6 +26,7 @@ namespace ag int loadModelSpecify(lua_State * L); int loadTexture(lua_State * L); int next(lua_State * L); + int pickObjects(lua_State * L); int print(lua_State * L); int println(lua_State * L); int registerEventHandler(lua_State * L); diff --git a/tests/managed_objects.lua b/tests/managed_objects.lua index c8ccfb6..7450479 100644 --- a/tests/managed_objects.lua +++ b/tests/managed_objects.lua @@ -10,6 +10,7 @@ function init_event() ag.println("error importing rot_camera") ag.exit() end + ag.setCursorVisible(true) end function init_obj(obj) @@ -56,3 +57,9 @@ function key_down_event(key) ag.setCursorVisible(not ag.getCursorVisible()) end end + +function mousebutton_down_event(button, x, y) + if (button == 1) then + ag.pickObjects(x, y) + end +end