feat: added basic player movement and gravity

main
Sara 2024-02-15 00:27:03 +01:00
parent 65cf4cb9bc
commit 1f742c0a58
4 changed files with 111 additions and 26 deletions

View File

@ -399,17 +399,17 @@ shape = SubResource("CapsuleShape3D_hqeid")
[node name="GeneralSkeleton" parent="Model/RootNode" index="0"] [node name="GeneralSkeleton" parent="Model/RootNode" index="0"]
bones/0/rotation = Quaternion(-2.98023e-08, 5.55112e-17, 5.55112e-17, 1) bones/0/rotation = Quaternion(-2.98023e-08, 5.55112e-17, 5.55112e-17, 1)
bones/1/position = Vector3(0.0088738, 0.823063, -0.0171585) bones/1/position = Vector3(0.00799814, 0.822939, -0.0170919)
bones/1/rotation = Quaternion(-0.0172656, -0.0722843, 0.00632022, 0.997215) bones/1/rotation = Quaternion(-0.0173024, -0.0769098, 0.00854839, 0.996851)
bones/2/rotation = Quaternion(0.00906533, 0.00637064, 0.000263158, 0.999939) bones/2/rotation = Quaternion(0.00896411, 0.00637054, 0.000265095, 0.99994)
bones/3/rotation = Quaternion(0.00894442, 0.00637058, 0.000265375, 0.99994) bones/3/rotation = Quaternion(0.00882963, 0.00637047, 0.000267564, 0.999941)
bones/4/rotation = Quaternion(0.00916765, 0.0063708, 0.00026108, 0.999938) bones/4/rotation = Quaternion(0.00907812, 0.00637071, 0.000262791, 0.999939)
bones/5/rotation = Quaternion(-0.0122992, 0.0265571, -0.00409385, 0.999563) bones/5/rotation = Quaternion(-0.0130848, 0.0288738, -0.00513406, 0.999484)
bones/6/rotation = Quaternion(-0.0123002, 0.0265571, -0.00409374, 0.999563) bones/6/rotation = Quaternion(-0.0130942, 0.0288758, -0.00513693, 0.999484)
bones/10/rotation = Quaternion(0.519859, 0.470118, 0.477057, -0.530238) bones/10/rotation = Quaternion(0.519859, 0.470118, 0.477057, -0.530238)
bones/11/rotation = Quaternion(0.238677, 0.843867, -0.452169, -0.162682) bones/11/rotation = Quaternion(0.24205, 0.842133, -0.4545, -0.160169)
bones/12/rotation = Quaternion(-0.0217123, -0.701667, -0.0218952, 0.711837) bones/12/rotation = Quaternion(-0.0282314, -0.699896, -0.0284688, 0.713119)
bones/13/rotation = Quaternion(-0.0456982, 0.569854, -0.146863, 0.807224) bones/13/rotation = Quaternion(-0.0439467, 0.565119, -0.143898, 0.811174)
bones/14/rotation = Quaternion(-0.256269, 0.776338, 0.203807, 0.538599) bones/14/rotation = Quaternion(-0.256269, 0.776338, 0.203807, 0.538599)
bones/15/rotation = Quaternion(0.14485, 0.51961, 0.145065, 0.829446) bones/15/rotation = Quaternion(0.14485, 0.51961, 0.145065, 0.829446)
bones/16/rotation = Quaternion(0.159353, 0.516644, 0.140377, 0.829446) bones/16/rotation = Quaternion(0.159353, 0.516644, 0.140377, 0.829446)
@ -420,9 +420,9 @@ bones/21/rotation = Quaternion(0.494946, 0.0399225, 0.106743, 0.861418)
bones/22/rotation = Quaternion(0.554517, 0.0271624, 0.0237916, 0.831389) bones/22/rotation = Quaternion(0.554517, 0.0271624, 0.0237916, 0.831389)
bones/23/rotation = Quaternion(0.554045, 0.0389721, 0.0175394, 0.831389) bones/23/rotation = Quaternion(0.554045, 0.0389721, 0.0175394, 0.831389)
bones/27/rotation = Quaternion(0.5168, -0.475173, -0.481158, -0.52499) bones/27/rotation = Quaternion(0.5168, -0.475173, -0.481158, -0.52499)
bones/28/rotation = Quaternion(-0.132163, 0.858452, -0.457619, 0.190205) bones/28/rotation = Quaternion(-0.125884, 0.859315, -0.455472, 0.195646)
bones/29/rotation = Quaternion(0.00506691, 0.708274, -0.00510919, 0.705901) bones/29/rotation = Quaternion(0.0109876, 0.709589, -0.0110795, 0.704443)
bones/30/rotation = Quaternion(0.0116492, -0.584682, 0.0637451, 0.808671) bones/30/rotation = Quaternion(0.0112271, -0.589384, 0.0652025, 0.805139)
bones/31/rotation = Quaternion(-0.256268, -0.776338, -0.203807, 0.538599) bones/31/rotation = Quaternion(-0.256268, -0.776338, -0.203807, 0.538599)
bones/32/rotation = Quaternion(0.14485, -0.51961, -0.145065, 0.829446) bones/32/rotation = Quaternion(0.14485, -0.51961, -0.145065, 0.829446)
bones/33/rotation = Quaternion(0.159352, -0.516644, -0.140377, 0.829446) bones/33/rotation = Quaternion(0.159352, -0.516644, -0.140377, 0.829446)
@ -432,19 +432,19 @@ bones/36/rotation = Quaternion(0.49701, -0.238709, -0.0692157, 0.831389)
bones/38/rotation = Quaternion(0.494946, -0.0399222, -0.106743, 0.861418) bones/38/rotation = Quaternion(0.494946, -0.0399222, -0.106743, 0.861418)
bones/39/rotation = Quaternion(0.554517, -0.0271621, -0.0237913, 0.831389) bones/39/rotation = Quaternion(0.554517, -0.0271621, -0.0237913, 0.831389)
bones/40/rotation = Quaternion(0.554045, -0.0389717, -0.0175393, 0.831389) bones/40/rotation = Quaternion(0.554045, -0.0389717, -0.0175393, 0.831389)
bones/50/rotation = Quaternion(-0.0963603, 0.0440981, 0.990983, 0.0819873) bones/50/rotation = Quaternion(-0.0911938, 0.0463263, 0.991272, 0.0831711)
bones/50/scale = Vector3(1.00002, 0.999999, 1) bones/50/scale = Vector3(1.00002, 0.999999, 1)
bones/51/rotation = Quaternion(0.00959858, 0.960804, -0.277045, -0.00295932) bones/51/rotation = Quaternion(0.00970492, 0.959913, -0.280113, -0.0029922)
bones/51/scale = Vector3(1.00002, 0.999999, 1.00002) bones/51/scale = Vector3(1.00002, 0.999999, 1.00002)
bones/52/rotation = Quaternion(0.0800893, 0.537878, -0.839045, -0.0166244) bones/52/rotation = Quaternion(0.0792991, 0.536526, -0.839989, -0.0164049)
bones/52/scale = Vector3(0.999948, 0.999973, 1.00001) bones/52/scale = Vector3(0.999948, 0.999973, 1.00001)
bones/54/rotation = Quaternion(9.7728e-07, 1, -1.50204e-05, 3.50609e-06) bones/54/rotation = Quaternion(9.7728e-07, 1, -1.50204e-05, 3.50609e-06)
bones/54/scale = Vector3(0.999999, 0.999986, 1.00002) bones/54/scale = Vector3(0.999999, 0.999986, 1.00002)
bones/56/rotation = Quaternion(0.174308, 0.140952, 0.971107, -0.081863) bones/56/rotation = Quaternion(0.179453, 0.139217, 0.970475, -0.0811989)
bones/56/scale = Vector3(1.00002, 0.999999, 1) bones/56/scale = Vector3(1.00002, 0.999999, 1)
bones/57/rotation = Quaternion(-0.0102774, 0.954933, -0.296628, 0.00316872) bones/57/rotation = Quaternion(-0.0102556, 0.955128, -0.295999, 0.00316205)
bones/57/scale = Vector3(1.00001, 0.999999, 1.00001) bones/57/scale = Vector3(1.00001, 0.999999, 1.00001)
bones/58/rotation = Quaternion(-0.0667033, 0.607529, -0.79119, 0.0218499) bones/58/rotation = Quaternion(-0.0675913, 0.607901, -0.790822, 0.0220936)
bones/58/scale = Vector3(0.999956, 0.999987, 1.00001) bones/58/scale = Vector3(0.999956, 0.999987, 1.00001)
bones/60/rotation = Quaternion(-6.79618e-07, 1, -9.98378e-06, -2.30227e-06) bones/60/rotation = Quaternion(-6.79618e-07, 1, -9.98378e-06, -2.30227e-06)
bones/60/scale = Vector3(0.999999, 0.999991, 1.00001) bones/60/scale = Vector3(0.999999, 0.999991, 1.00001)

View File

@ -10,7 +10,37 @@ config_version=5
[application] [application]
config/name="godot cpp template" config/name="Slash-n-Bash"
run/main_scene="res://boot.tscn" run/main_scene="res://boot.tscn"
config/features=PackedStringArray("4.2", "Forward Plus") config/features=PackedStringArray("4.2", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[display]
window/size/viewport_width=1920
window/size/viewport_height=1080
window/size/mode=3
window/stretch/mode="viewport"
[input]
move_forward={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"echo":false,"script":null)
]
}
move_back={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"echo":false,"script":null)
]
}
move_left={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"echo":false,"script":null)
]
}
move_right={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null)
]
}

View File

@ -2,9 +2,13 @@
#include <utility> #include <utility>
#include "game_mode.hpp" #include "game_mode.hpp"
#include "godot_cpp/classes/animation_tree.hpp" #include "godot_cpp/classes/animation_tree.hpp"
#include "godot_cpp/classes/engine.hpp"
#include "godot_cpp/classes/node3d.hpp" #include "godot_cpp/classes/node3d.hpp"
#include "godot_cpp/classes/skeleton3d.hpp" #include "godot_cpp/classes/skeleton3d.hpp"
#include "godot_cpp/variant/transform3d.hpp"
#include "godot_cpp/variant/utility_functions.hpp" #include "godot_cpp/variant/utility_functions.hpp"
#include "godot_cpp/classes/viewport.hpp"
#include "godot_cpp/classes/camera3d.hpp"
#include "godot_macros.h" #include "godot_macros.h"
namespace godot { namespace godot {
@ -16,6 +20,7 @@ void Player::_bind_methods() {
GDFUNCTION_ARGS(on_horizontal, "event", "value"); GDFUNCTION_ARGS(on_horizontal, "event", "value");
GDFUNCTION_ARGS(on_vertical, "event", "value"); GDFUNCTION_ARGS(on_vertical, "event", "value");
} }
void Player::_enter_tree() { void Player::_enter_tree() {
this->model = this->get_node<Node3D>("Model"); this->model = this->get_node<Node3D>("Model");
this->animTree = this->get_node<AnimationTree>("AnimationTree"); this->animTree = this->get_node<AnimationTree>("AnimationTree");
@ -26,12 +31,28 @@ void Player::_enter_tree() {
void Player::_ready() { void Player::_ready() {
this->customization_init(); this->customization_init();
GDGAMEONLY(); GDGAMEONLY();
this->flat_view_init();
this->input->listen_to(PlayerInput::Listener("move_left", "move_right", this, "on_horizontal")); this->input->listen_to(PlayerInput::Listener("move_left", "move_right", this, "on_horizontal"));
this->input->listen_to(PlayerInput::Listener("move_forward", "move_back", this, "on_vertical")); this->input->listen_to(PlayerInput::Listener("move_forward", "move_back", this, "on_vertical"));
} }
void Player::_process(double deltaTime) {} void Player::_process(double deltaTime) {
void Player::_physics_process(double deltaTime) {} GDGAMEONLY();
Vector3 const worldInput = this->get_world_move_input() * this->walkSpeed;
Vector3 const velocity = this->get_velocity();
this->set_velocity({worldInput.x, velocity.y, worldInput.z});
this->rotate_to_movement();
}
void Player::_physics_process(double deltaTime) {
GDGAMEONLY();
if(!this->is_on_floor()) {
Vector3 const velocity = this->get_velocity();
this->set_velocity({velocity.x, velocity.y - 9.10f * float(deltaTime), velocity.z});
}
this->move_and_slide();
}
void Player::set_customization_active(Node3D *node, bool active) { void Player::set_customization_active(Node3D *node, bool active) {
node->set_visible(active); node->set_visible(active);
@ -39,10 +60,12 @@ void Player::set_customization_active(Node3D *node, bool active) {
} }
void Player::on_horizontal(Ref<InputEvent> event, float value) { void Player::on_horizontal(Ref<InputEvent> event, float value) {
UtilityFunctions::print("horizontal ", value);
this->moveInput.x = value; this->moveInput.x = value;
} }
void Player::on_vertical(Ref<InputEvent> event, float value) { void Player::on_vertical(Ref<InputEvent> event, float value) {
UtilityFunctions::print("vertical ", value);
this->moveInput.y = value; this->moveInput.y = value;
} }
@ -54,7 +77,7 @@ void Player::select_customization(String category,
// invalid keys are stored as the size of the array // invalid keys are stored as the size of the array
if (index.has_value() && index.value() > pair.options.size()) if (index.has_value() && index.value() > pair.options.size())
index = std::nullopt; index = std::nullopt;
// nothing changes // nothing changesw
if (index == pair.currentSelected) if (index == pair.currentSelected)
return; return;
// disable previous chosen option, if any // disable previous chosen option, if any
@ -92,11 +115,38 @@ void Player::set_active_customization(Dictionary value) {
else if (!exists) else if (!exists)
return; return;
else else
this->select_customization( this->select_customization(key, selection >= 0
key, selection >= 0 ? std::optional{selection} : std::nullopt); ? std::optional{selection}
: std::nullopt);
} }
} }
Vector3 Player::get_world_move_input() const {
Vector2 n = this->moveInput.normalized();
return flatViewBasis.get_column(0) * n.x
+ flatViewBasis.get_column(2) * n.y;
}
void Player::rotate_to_movement() {
Vector3 const forward = this->get_world_move_input();
if(forward.is_zero_approx())
return;
Vector3 const up = {0, 1, 0};
Vector3 const right = forward.cross(up);
Transform3D trans = this->get_global_transform();
trans.basis = Basis(right, up, forward);
this->set_global_transform(trans);
}
void Player::flat_view_init() {
Basis basis = this->get_viewport()->get_camera_3d()->get_global_transform().get_basis();
Vector3 right = basis.get_column(0);
right = Vector3{-right.x, 0, -right.z}.normalized();
Vector3 forward = basis.get_column(2);
forward = Vector3{-forward.x, 0, -forward.y}.normalized();
this->flatViewBasis = Basis{right, right.cross(forward), forward};
}
void Player::customization_init() { void Player::customization_init() {
this->customizationParent = this->customizationParent =
this->get_node<Skeleton3D>("Model/RootNode/GeneralSkeleton"); this->get_node<Skeleton3D>("Model/RootNode/GeneralSkeleton");

View File

@ -29,8 +29,9 @@ protected:
// the skeleton of the customizable character model // the skeleton of the customizable character model
Skeleton3D *customizationParent{nullptr}; Skeleton3D *customizationParent{nullptr};
PlayerInput *input{nullptr}; PlayerInput *input{nullptr};
Basis flatViewBasis{};
float walkSpeed{10.f}; float walkSpeed{4.f};
Vector2 moveInput{0.f, 0.f}; Vector2 moveInput{0.f, 0.f};
@ -55,8 +56,12 @@ public:
// every element in a [String, int] dictionary, where key is the category // every element in a [String, int] dictionary, where key is the category
// to assign to and the value is the index of the element to show. // to assign to and the value is the index of the element to show.
void set_active_customization(Dictionary value); void set_active_customization(Dictionary value);
// returns moveInput transformed to world space
Vector3 get_world_move_input() const;
private: private:
void rotate_to_movement();
void flat_view_init();
void customization_init(); void customization_init();
}; };
} // namespace godot } // namespace godot