Jump to content

[C++] AddForce not moving object (making a player class).


TheConceptBoy
 Share

Recommended Posts

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;
}

 

Link to comment
Share on other sites

Ah, I see what needs to be done.

Each object needs to have physics enabled:

	// create player dummy object (CHANGE TO SPHERE LATER)
	player_box = Model::Sphere();
	player_box->SetPhysicsMode(Entity::CharacterPhysics.);
	player_box->SetMass(1);
	//player_box->SetFriction(0, 0

 

The only issues is that with these collisions, it seems that the player object is dragging along the floor, the interaction is slowing the player down, making it hard to move.

 

I figured I might have to be using Entity::RigidBodyPhysics instead of Entity::CharacterPhysics but that just makes the object not interact with the floor shapes I've put in the map via the Leadwerks Level Editor.

 

EDIT:

Well I had to play around with some properties but in the end I got the object to move without grinding on the floor using Entity::CharacterPhysics. What is the difference between the two? The docs for the function don't mention it?

 

Also is there anything I'm missing to get the character movement to be more resembling of a walk and strafe and less like a deer on ice? 

Link to comment
Share on other sites

A rigid body needs a shape and mass. The character controller...well, there are examples in the API documentation. Please let us know if you have more questions as you advance.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...