summaryrefslogtreecommitdiff
path: root/src/camera_controller
diff options
context:
space:
mode:
authorAnson Bridges <bridges.anson@gmail.com>2022-09-19 17:44:22 -0400
committerAnson Bridges <bridges.anson@gmail.com>2022-09-19 17:44:22 -0400
commit1d347e770fddcdd051890cdf070fd2779ab113bf (patch)
tree6501fb000509819b7ba58df15c9a831c8bdc44fb /src/camera_controller
parent2fd755132f526c48fed2c1867530526971e1cf19 (diff)
problems: player controller perf, boat perf, NAVSERVER PERF
Diffstat (limited to 'src/camera_controller')
-rw-r--r--src/camera_controller/.sconsign.dblitebin0 -> 25088 bytes
-rw-r--r--src/camera_controller/PlayerCam.gd79
-rw-r--r--src/camera_controller/SConstruct109
-rw-r--r--src/camera_controller/gdlibrary.cpp15
-rw-r--r--src/camera_controller/gdlibrary.osbin0 -> 185848 bytes
-rw-r--r--src/camera_controller/playercam.cpp107
-rw-r--r--src/camera_controller/playercam.h43
-rw-r--r--src/camera_controller/playercam.osbin0 -> 248864 bytes
8 files changed, 353 insertions, 0 deletions
diff --git a/src/camera_controller/.sconsign.dblite b/src/camera_controller/.sconsign.dblite
new file mode 100644
index 0000000..f7491e9
--- /dev/null
+++ b/src/camera_controller/.sconsign.dblite
Binary files differ
diff --git a/src/camera_controller/PlayerCam.gd b/src/camera_controller/PlayerCam.gd
new file mode 100644
index 0000000..382ec06
--- /dev/null
+++ b/src/camera_controller/PlayerCam.gd
@@ -0,0 +1,79 @@
+extends ClippedCamera
+
+var _modes: PoolStringArray = ["STATIC", "FIRSTPERSON", "THIRDPERSON", "ARM", "FREECAM"]
+var mode: String = "STATIC" #STATIC, FIRSTPERSON, THIRDPERSON, ARM, FREECAM
+
+#first/third person variables
+var head: Spatial = null
+var neck: Spatial = null
+var player: RigidBody = null
+
+#third person/arm variables
+var arm: SpringArm = null
+
+var mouse_axis: Vector2
+var mouse_sensitivity: float = 12.0
+
+func _ready():
+ Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
+ current = true
+
+func _input(event: InputEvent):
+ if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
+ mouse_axis = event.relative
+ match mode:
+ "FIRSTPERSON":
+ mouse_firstperson()
+ "THIRDPERSON":
+ mouse_thirdperson()
+ "STATIC":
+ pass
+ "ARM":
+ mouse_arm()
+ "FREECAM":
+ mouse_freecam()
+ _:
+ pass
+
+func attach(new_parent: Node, c_mode: String, extra_path: String = "."):
+ if get_parent():
+ get_parent().remove_child(self)
+ if c_mode in _modes:
+ mode = c_mode
+ if mode == "FIRSTPERSON":
+ head = new_parent.head; neck = new_parent.neck;
+ arm = null;
+ elif mode == "THIRDPERSON":
+ head = new_parent.head; neck = new_parent.neck;
+ arm = new_parent.arm
+ elif mode == "ARM":
+ head = null; neck = null;
+ arm = new_parent.arm
+ else:
+ head = null; neck = null; arm = null;
+ new_parent.get_node(extra_path).add_child(self)
+ transform = Transform.IDENTITY
+
+func mouse_firstperson() -> void:
+ if mouse_axis.length_squared() > 0:
+ var horizontal: float = -mouse_axis.x * (mouse_sensitivity / 100)
+ var vertical: float = -mouse_axis.y * (mouse_sensitivity / 100)
+
+ neck.rotate_y(deg2rad(horizontal))
+ head.rotate_x(deg2rad(vertical))
+
+ #vertical clamp
+ head.rotation_degrees.x = clamp(head.rotation_degrees.x, -90, 90)
+
+func mouse_thirdperson() -> void:
+ arm.rotation_degrees.x = clamp(rotation_degrees.x-mouse_axis.y*(mouse_sensitivity / 100),-90,90)
+ arm.rotation_degrees.y -= mouse_axis.x*(mouse_sensitivity / 100)
+ head.rotation_degrees.x = arm.rotation_degrees.x
+ neck.rotation_degrees.y = arm.rotation_degrees.y
+
+func mouse_arm() -> void:
+ arm.rotation_degrees.x = clamp(rotation_degrees.x-mouse_axis.y*(mouse_sensitivity / 100),-70,70)
+ arm.rotation_degrees.y -= mouse_axis.x*(mouse_sensitivity / 100)
+
+func mouse_freecam() -> void:
+ pass
diff --git a/src/camera_controller/SConstruct b/src/camera_controller/SConstruct
new file mode 100644
index 0000000..d68c103
--- /dev/null
+++ b/src/camera_controller/SConstruct
@@ -0,0 +1,109 @@
+#!python
+import os
+
+opts = Variables([], ARGUMENTS)
+
+# Gets the standard flags CC, CCX, etc.
+env = DefaultEnvironment()
+
+# Define our options
+opts.Add(EnumVariable('target', "Compilation target", 'debug', ['d', 'debug', 'r', 'release']))
+opts.Add(EnumVariable('platform', "Compilation platform", '', ['', 'windows', 'x11', 'linux', 'osx']))
+opts.Add(EnumVariable('p', "Compilation target, alias for 'platform'", '', ['', 'windows', 'x11', 'linux', 'osx']))
+opts.Add(BoolVariable('use_llvm', "Use the LLVM / Clang compiler", 'no'))
+opts.Add(PathVariable('target_path', 'The path where the lib is installed.', '../../godot/bin/'))
+opts.Add(PathVariable('target_name', 'The library name.', 'libplayercam', PathVariable.PathAccept))
+
+# Local dependency paths, adapt them to your setup
+godot_headers_path = "../../godot-cpp/godot-headers/"
+cpp_bindings_path = "../../godot-cpp/"
+cpp_library = "libgodot-cpp"
+
+# only support 64 at this time..
+bits = 64
+
+# Updates the environment with the option variables.
+opts.Update(env)
+
+# Process some arguments
+if env['use_llvm']:
+ env['CC'] = 'clang'
+ env['CXX'] = 'clang++'
+
+if env['p'] != '':
+ env['platform'] = env['p']
+
+if env['platform'] == '':
+ print("No valid target platform selected.")
+ quit();
+
+# For the reference:
+# - CCFLAGS are compilation flags shared between C and C++
+# - CFLAGS are for C-specific compilation flags
+# - CXXFLAGS are for C++-specific compilation flags
+# - CPPFLAGS are for pre-processor flags
+# - CPPDEFINES are for pre-processor defines
+# - LINKFLAGS are for linking flags
+
+# Check our platform specifics
+if env['platform'] == "osx":
+ env['target_path'] += 'osx/'
+ cpp_library += '.osx'
+ env.Append(CCFLAGS=['-arch', 'x86_64'])
+ env.Append(CXXFLAGS=['-std=c++17'])
+ env.Append(LINKFLAGS=['-arch', 'x86_64'])
+ if env['target'] in ('debug', 'd'):
+ env.Append(CCFLAGS=['-g', '-O2'])
+ else:
+ env.Append(CCFLAGS=['-g', '-O3'])
+
+elif env['platform'] in ('x11', 'linux'):
+ env['target_path'] += 'x11/'
+ cpp_library += '.linux'
+ env.Append(CCFLAGS=['-fPIC'])
+ env.Append(CXXFLAGS=['-std=c++17'])
+ if env['target'] in ('debug', 'd'):
+ env.Append(CCFLAGS=['-g3', '-Og'])
+ else:
+ env.Append(CCFLAGS=['-g', '-O3'])
+
+elif env['platform'] == "windows":
+ env['target_path'] += 'win64/'
+ cpp_library += '.windows'
+ # This makes sure to keep the session environment variables on windows,
+ # that way you can run scons in a vs 2017 prompt and it will find all the required tools
+ env.Append(ENV=os.environ)
+
+ env.Append(CPPDEFINES=['WIN32', '_WIN32', '_WINDOWS', '_CRT_SECURE_NO_WARNINGS'])
+ env.Append(CCFLAGS=['-W3', '-GR'])
+ env.Append(CXXFLAGS='/std:c++17')
+ if env['target'] in ('debug', 'd'):
+ env.Append(CPPDEFINES=['_DEBUG'])
+ env.Append(CCFLAGS=['-EHsc', '-MDd', '-ZI'])
+ env.Append(LINKFLAGS=['-DEBUG'])
+ else:
+ env.Append(CPPDEFINES=['NDEBUG'])
+ env.Append(CCFLAGS=['-O2', '-EHsc', '-MD'])
+
+if env['target'] in ('debug', 'd'):
+ cpp_library += '.debug'
+else:
+ cpp_library += '.release'
+
+cpp_library += '.' + str(bits)
+
+# make sure our binding library is properly includes
+env.Append(CPPPATH=['.', godot_headers_path, cpp_bindings_path + 'include/', cpp_bindings_path + 'include/core/', cpp_bindings_path + 'include/gen/'])
+env.Append(LIBPATH=[cpp_bindings_path + 'bin/'])
+env.Append(LIBS=[cpp_library])
+
+# tweak this if you want to use different folders, or more folders, to store your source code in.
+env.Append(CPPPATH=['./'])
+sources = Glob('*.cpp')
+
+library = env.SharedLibrary(target=env['target_path'] + env['target_name'] , source=sources)
+
+Default(library)
+
+# Generates help for the -h scons option.
+Help(opts.GenerateHelpText(env))
diff --git a/src/camera_controller/gdlibrary.cpp b/src/camera_controller/gdlibrary.cpp
new file mode 100644
index 0000000..3311441
--- /dev/null
+++ b/src/camera_controller/gdlibrary.cpp
@@ -0,0 +1,15 @@
+#include "playercam.h"
+
+extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) {
+ godot::Godot::gdnative_init(o);
+}
+
+extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) {
+ godot::Godot::gdnative_terminate(o);
+}
+
+extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) {
+ godot::Godot::nativescript_init(handle);
+
+ godot::register_class<godot::PlayerCam>();
+}
diff --git a/src/camera_controller/gdlibrary.os b/src/camera_controller/gdlibrary.os
new file mode 100644
index 0000000..ade72b5
--- /dev/null
+++ b/src/camera_controller/gdlibrary.os
Binary files differ
diff --git a/src/camera_controller/playercam.cpp b/src/camera_controller/playercam.cpp
new file mode 100644
index 0000000..f321c09
--- /dev/null
+++ b/src/camera_controller/playercam.cpp
@@ -0,0 +1,107 @@
+#include "playercam.h"
+#include "InputEventMouseMotion.hpp"
+
+
+using namespace godot;
+
+PlayerCam::PlayerCam () { }
+PlayerCam::~PlayerCam () { }
+
+void PlayerCam::_register_methods() {
+ register_method("_ready", &PlayerCam::_ready);
+ register_method("_input", &PlayerCam::_input);
+ register_method("attach", &PlayerCam::attach);
+ register_method("mouse_firstperson", &PlayerCam::mouse_firstperson);
+ register_method("mouse_thirdperson", &PlayerCam::mouse_thirdperson);
+ register_method("mouse_arm", &PlayerCam::mouse_arm);
+ register_method("mouse_freecam", &PlayerCam::mouse_freecam);
+}
+
+void PlayerCam::_init() {
+ mode = String("STATIC");
+ head = nullptr;
+ neck = nullptr;
+ player = nullptr;
+ arm = nullptr;
+ mouse_axis = Vector2::ZERO;
+ mouse_sensitivity = 12.0;
+}
+
+void PlayerCam::_ready() {
+ Input *input_singleton = Input::get_singleton();
+ input_singleton->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
+ set_current(true);
+}
+
+void PlayerCam::_input(const Ref<InputEvent> event) {
+ Input const *input_singleton = Input::get_singleton();
+ Ref<InputEventMouseMotion> event_m(event);
+ if(event_m.is_valid() && input_singleton->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED){
+ mouse_axis = event_m->get_relative();
+ if(mode == "FIRSTPERSON")mouse_firstperson();
+ else if(mode == "THIRDPERSON")mouse_thirdperson();
+ else if(mode == "STATIC");
+ else if(mode == "ARM")mouse_arm();
+ else if(mode == "FREECAM")mouse_freecam();
+ }
+}
+
+void PlayerCam::attach(Node* new_parent, String c_mode, String extra_path) {
+ if(get_parent() != nullptr)
+ get_parent()->remove_child(this);
+ if(c_mode == "FIRSTPERSON") {
+ head = (Spatial*)new_parent->find_node("Head", true, false); neck = (Spatial*)new_parent->find_node("Neck", true, false);
+ arm = nullptr; mode = c_mode;
+ }
+ else if (c_mode == "THIRDPERSON") {
+ head = (Spatial*)new_parent->find_node("Head", true, false); neck = (Spatial*)new_parent->find_node("Neck", true, false);
+ arm = (SpringArm*)new_parent->find_node("SpringArm", true, false); mode = c_mode;
+ }
+ else if(c_mode == "ARM") {
+ head = nullptr; neck = nullptr;
+ arm = (SpringArm*)new_parent->find_node("SpringArm", true, false); mode = c_mode;
+ }
+ else {
+ head = nullptr; neck = nullptr; arm = nullptr;
+ }
+ new_parent->get_node(NodePath(extra_path))->add_child(this);
+ set_transform(Transform::IDENTITY);
+}
+
+void PlayerCam::mouse_firstperson() {
+ if(mouse_axis.length_squared() > 0.0){
+ float horizontal = -mouse_axis.x * (mouse_sensitivity / 100.0);
+ float vertical = -mouse_axis.y * (mouse_sensitivity / 100.0);
+
+ neck->rotate_y(Math::deg2rad(horizontal));
+ head->rotate_x(Math::deg2rad(vertical));
+
+ //vertical clamp
+ Vector3 new_rot = head->get_rotation_degrees();
+ new_rot.x = Math::clamp((double)new_rot.x, -90.0, 90.0);
+ head->set_rotation_degrees(new_rot);
+ }
+}
+
+void PlayerCam::mouse_thirdperson() {
+ Vector3 new_arm_rot = arm->get_rotation_degrees();
+ new_arm_rot.x = Math::clamp((double)(get_rotation_degrees().x-mouse_axis.y*(mouse_sensitivity / 100)),-90.0,90.0);
+ new_arm_rot.y -= mouse_axis.x*(mouse_sensitivity / 100.0);
+ arm->set_rotation_degrees(new_arm_rot);
+ Vector3 new_head_rot = Vector3(arm->get_rotation_degrees().x, 0, 0);
+ Vector3 new_neck_rot = Vector3(0, arm->get_rotation_degrees().y, 0);
+
+ head->set_rotation_degrees(new_head_rot);
+ neck->set_rotation_degrees(new_neck_rot);
+}
+
+void PlayerCam::mouse_arm() {
+ //arm->rotation_degrees.x = Math::clamp(rotation_degrees.x-mouse_axis.y*(mouse_sensitivity / 100),-70,70);
+ //arm->rotation_degrees.y -= mouse_axis.x*(mouse_sensitivity / 100);
+}
+
+void PlayerCam::mouse_freecam() {
+// pass
+}
+
+
diff --git a/src/camera_controller/playercam.h b/src/camera_controller/playercam.h
new file mode 100644
index 0000000..dd089de
--- /dev/null
+++ b/src/camera_controller/playercam.h
@@ -0,0 +1,43 @@
+#ifndef PLAYERCAMGDS_H
+#define PLAYERCAMGDS_H
+
+#include <Godot.hpp>
+#include <ClippedCamera.hpp>
+#include <RigidBody.hpp>
+#include <SpringArm.hpp>
+#include <Input.hpp>
+
+namespace godot {
+
+class PlayerCam : public ClippedCamera {
+ GODOT_CLASS(PlayerCam, ClippedCamera)
+
+private:
+ String mode;
+ Spatial* head;
+ Spatial* neck;
+ RigidBody* player;
+ SpringArm* arm;
+ Vector2 mouse_axis;
+ float mouse_sensitivity;
+
+public:
+ static void _register_methods();
+
+ PlayerCam();
+ ~PlayerCam();
+
+ void _init();
+
+ void _ready();
+ void _input(const Ref<InputEvent> event);
+ void attach(Node* new_parent, String c_mode, String extra_path = ".");
+ void mouse_firstperson();
+ void mouse_thirdperson();
+ void mouse_arm();
+ void mouse_freecam();
+
+};
+
+}
+#endif
diff --git a/src/camera_controller/playercam.os b/src/camera_controller/playercam.os
new file mode 100644
index 0000000..29ea3c4
--- /dev/null
+++ b/src/camera_controller/playercam.os
Binary files differ