From 95ccf7af6bc1c257a3062bbf68de7f08654598b3 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Fri, 15 Jan 2021 20:33:25 -0600 Subject: [PATCH] Access to vector operators --- cmake-builds.sh | 2 +- core/Lua.cpp | 2 +- core/types.cpp | 19 +++++++++++++++++++ core/types.h | 3 +++ main.lua | 17 ++++++++++++----- scripting/couch.i | 11 ++++++++++- 6 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 core/types.cpp diff --git a/cmake-builds.sh b/cmake-builds.sh index 05282fe..e86b99f 100755 --- a/cmake-builds.sh +++ b/cmake-builds.sh @@ -4,7 +4,7 @@ set -e mkdir -p build cd build -cmake .. +cmake .. -DCMAKE_BUILD_TYPE=Debug make cd .. diff --git a/core/Lua.cpp b/core/Lua.cpp index 42fb161..93bc944 100644 --- a/core/Lua.cpp +++ b/core/Lua.cpp @@ -11,7 +11,7 @@ void Lua::Initialize() { language = this; int err; // Initialize Lua - luaopen_base(L); + luaL_openlibs(L); luaopen_couch(L); err = luaL_loadfile(L, "main.lua"); if (err == LUA_OK) { diff --git a/core/types.cpp b/core/types.cpp new file mode 100644 index 0000000..11c1bf8 --- /dev/null +++ b/core/types.cpp @@ -0,0 +1,19 @@ +#include "types.h" + +#include + +Vector3 operator+(const Vector3 &r, const Vector3 &l) { + Vector3 val(0.0f); + val.x = r.x + l.x; + val.y = r.y + l.y; + val.z = r.z + l.z; + return val; +} + +Vector3 operator*(const Vector3 &r, const cfloat &l) { + Vector3 val(0.0f); + val.x = r.x * l; + val.y = r.y * l; + val.z = r.z * l; + return val; +} diff --git a/core/types.h b/core/types.h index e4586d8..8d9c6a7 100644 --- a/core/types.h +++ b/core/types.h @@ -15,4 +15,7 @@ typedef std::string Name; typedef GLfloat cfloat; typedef GLuint Id; +Vector3 operator*(const Vector3 &r, const cfloat &l); +Vector3 operator+(const Vector3 &r, const Vector3 &l); + #endif /* TYPES_H */ diff --git a/main.lua b/main.lua index c584541..827dc35 100644 --- a/main.lua +++ b/main.lua @@ -1,3 +1,5 @@ +local min = math.min +local max = math.max local ball local ball1 local camera @@ -16,6 +18,8 @@ local ballvy = -1.0 local cam_rot_x = 0.0 local cam_rot_y = 0.0 +local SPEED = 30 + function init() camera = couch.Camera() camera:MakeCurrent() @@ -32,12 +36,15 @@ end function update(delta) local cam_forwards = camera.transform:Forward() - --local vec1 = couch.Vector3() + couch.Vector3() - --camera.transform.position = camera.transform.position + cam_forwards - --camera.transform:Translate(vx * delta, 0.0, vz * delta) + + local move_vec = couch.Vector3() + move_vec = camera.transform.position + cam_forwards * delta * vz * SPEED + camera.transform.position = move_vec + camera.transform.rotation.y = camera.transform.rotation.y - cam_rot_x * delta cam_rot_x = 0.0 camera.transform.rotation.x = camera.transform.rotation.x - cam_rot_y * delta + camera.transform.rotation.x = min(max(camera.transform.rotation.x, -3.14), 3.14) cam_rot_y = 0.0 local loc = ball1.transform.position @@ -63,9 +70,9 @@ function onkey(key, code, action, mod) end if key == UP and action == 1 then - vz = -1.0 - elseif key == DOWN and action == 1 then vz = 1.0 + elseif key == DOWN and action == 1 then + vz = -1.0 elseif (key == DOWN or key == UP) and action == 0 then vz = 0.0 end diff --git a/scripting/couch.i b/scripting/couch.i index 52884a1..cc39296 100644 --- a/scripting/couch.i +++ b/scripting/couch.i @@ -11,15 +11,24 @@ #include "Mesh.h" #include "Ball.h" #include "Camera.h" - %} +%} typedef float cfloat; %ignore "cfloat"; class Vector3 { public: + Vector3(); cfloat x, y, z; }; +%extend Vector3 { + Vector3 operator+(const Vector3 &o) const { + return *$self + o; + } + Vector3 operator*(const cfloat &o) const { + return *$self * o; + } +} %ignore "Vector3"; %include "types.h"