Good day, everyone.
I was out, looking for another engine to add to my option and saw the Leadwerks was on sale on Humble. Ended up picking up the core engine and since I come from App Game Kit C++ programming experience, I also picked up Pro Edition to start out with C++ right away.
My first question is does Leadwerks simply interpret Lua with C++ namespace commands or does it drive the engine directly? Just a side inquiry.
My main question right now is figuring out how to create a player class (or for now just the movement code, i'll move it into a class later). Correct me if I get any theory wrong, from what I've learned so far. When you create an object (like the player object) this way,
Model* player_box;
bool App::Start(){
player_box = Model::Box();
}
Then this player_box model class now uses Leadwerks engine physics?
In App Game Kit, we have to usually core our own player collisions using projection or ray casting or using sphere collisions. In Leadwerks documentation, there was a pre-made first person player class already in the game, where you can set up the properties like mass, movement, mouse speed, step height etc. Can this class till. be used via c++? As in, use the base player class that you're setting up in Leadwerks but add things like hands, weapons, etc, via C++ in visual studio?
Now the main issue is that, as I'm testing, I can't get the player_box to move using physics. I can make it move using SetPosition or Move no problem, however that voids collisions and use of physics which, I assume are built into the engine physics solver. So here's my set up code. There's no player control affecting or causing the movement, I just need to see the box move and collide, for the first time.
Perhaps I need to enable physics on the object before I can use it that way? Maybe because I created the box in code, there is more setup that needs to be done on it?
#include "App.h"
using namespace Leadwerks;
App::App() : window(NULL), context(NULL), world(NULL), camera(NULL) {}
App::~App() { delete world; delete window; }
float mouse_x;
float mouse_y;
float cam_x = 0;
float cam_y = 0;
float player_x = 0;
float player_y = 0;
float player_z = 0;
Model* player_box;
bool App::Start(){
// Create window
window = Window::Create("testgame", 100, 100, 1280, 720, Window::Titlebar);
//Create context
context = Context::Create(window);
// Create world
world = World::Create();
// create camera
main_cam = Camera::Create();
main_cam->SetRotation(cam_y, 0, 0);
// create player dummy object (CHANGE TO SPHERE LATER)
player_box = Model::Box();
// set default cursor position
window->SetMousePosition(window->GetWidth() / 2, window->GetHeight() / 2);
// load test map made in Level Editor
std::string map = "Maps/start_map.map";
if (!Map::Load(map)) return false;
return true;
}
bool App::Loop(){
// Camera movement with mouse.
Vec3 mouse_pos = window->GetMousePosition();
mouse_speed = 0.6;
movement_speed = 2;
// reset mouse values
mouse_x = 0;
mouse_y = 0;
mouse_x = mouse_pos[0] - window->GetWidth() / 2;
mouse_y = mouse_pos[1] - window->GetHeight() / 2;
// add to camera movement
cam_x += mouse_x* mouse_speed;
cam_y += mouse_y* mouse_speed;
//Add force to move player object (player_box)
if (window->KeyDown(Key::W)) { player_box->AddForce(0, 0, 10, true); }
if (window->KeyDown(Key::S)) {player_box->AddForce(0, 0, -10, true);}
if (window->KeyDown(Key::A)) {player_box->AddForce(10, 0, 0, true);}
if (window->KeyDown(Key::D)) {player_box->AddForce(-10, 0, 0, true);}
// rotate box to face forward motion.
player_box->SetRotation(0, cam_x, 0, true);
// Rotate and Position camera based on player_box;
main_cam->SetRotation(cam_y, 0, 0);
main_cam->SetPosition(player_box->GetPosition(true)[0], player_box->GetPosition(true)[1], player_box->GetPosition(true)[2], true);
// debug stuff
Print("cam_x: " + to_string(cam_x) + " cam_y:" + to_string(cam_y));
Print("mouse_x: " + to_string(mouse_x) + " mouse_y:" + to_string(mouse_y));
//reset mouse to center of screen
window->SetMousePosition(window->GetWidth() / 2, window->GetHeight() / 2);
Time::Update(); // update keyframe
world->Update();
world->Render();
context->Sync(false);
if (window->Closed() || window->KeyHit(Key::Escape)) {
return false;
}
return true;
}