Prechádzať zdrojové kódy

Add basic support to character controllers

Daniele Bartolini 12 rokov pred
rodič
commit
6d22fef367

+ 3 - 0
engine/CMakeLists.txt

@@ -364,6 +364,7 @@ set (LUA_SRC
 	lua/LuaMesh.cpp
 	lua/LuaSprite.cpp
 	lua/LuaActor.cpp
+	lua/LuaController.cpp
 	lua/LuaPhysicsWorld.cpp
 )
 
@@ -382,6 +383,7 @@ set (AUDIO_HEADERS
 set (PHYSICS_SRC
 	physics/PhysicsWorld.cpp
 	physics/Actor.cpp
+	physics/Controller.cpp
 )
 
 set (PHYSICS_HEADERS
@@ -389,6 +391,7 @@ set (PHYSICS_HEADERS
 	physics/Physics.h
 	physics/PhysicsWorld.h
 	physics/Actor.h
+	physics/Controller.cpp
 )
 
 set (COMPILER_SRC

+ 24 - 1
engine/Unit.cpp

@@ -31,6 +31,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "UnitResource.h"
 #include "SceneGraphManager.h"
 #include "Actor.h"
+#include "Controller.h"
 
 namespace crown
 {
@@ -38,6 +39,8 @@ namespace crown
 typedef Id CameraId;
 typedef Id SpriteId;
 
+static int count = 0;
+
 //-----------------------------------------------------------------------------
 Unit::Unit(World& w, SceneGraph& sg, const UnitResource* ur, const Matrix4x4& pose)
 	: m_world(w)
@@ -54,6 +57,7 @@ Unit::Unit(World& w, SceneGraph& sg, const UnitResource* ur, const Matrix4x4& po
 	Log::d("Num cameras           = %d", ur->num_cameras());
 	Log::d("Num actors            = %d", ur->num_actors());
 
+	m_controller.component.id = INVALID_ID;
 	create(pose);
 }
 
@@ -142,7 +146,9 @@ void Unit::create_renderable_objects()
 void Unit::create_physics_objects()
 {
 	const StringId32 name_hash = hash::murmur2_32("actor", string::strlen("actor"), 0);
-	add_actor(name_hash, m_world.create_actor(m_scene_graph, 0, ActorType::DYNAMIC_PHYSICAL));
+	if (count != 0)
+	add_actor(name_hash, m_world.create_actor(m_scene_graph, 0, (count == 1) ? ActorType::DYNAMIC_KINEMATIC : ActorType::DYNAMIC_PHYSICAL));
+	count++;
 }
 
 //-----------------------------------------------------------------------------
@@ -310,6 +316,13 @@ void Unit::add_actor(StringId32 name, ActorId actor)
 	add_component(name, actor, m_num_actors, m_actors);
 }
 
+//-----------------------------------------------------------------------------
+void Unit::set_controller(StringId32 name, ControllerId controller)
+{
+	m_controller.name = name;
+	m_controller.component = controller;
+}
+
 //-----------------------------------------------------------------------------
 Camera* Unit::camera(const char* name)
 {
@@ -390,5 +403,15 @@ Actor* Unit::actor(uint32_t i)
 	return m_world.lookup_actor(actor);
 }	
 
+//-----------------------------------------------------------------------------
+Controller* Unit::controller()
+{
+	if (m_controller.component.id != INVALID_ID)
+	{
+		return m_world.lookup_controller(m_controller.component);
+	}
+
+	return NULL;
+}
 
 } // namespace crown

+ 8 - 2
engine/Unit.h

@@ -62,13 +62,14 @@ typedef Id CameraId;
 typedef Id MeshId;
 typedef Id SpriteId;
 typedef Id ActorId;
-
+typedef Id ControllerId;
 typedef	Id ComponentId;
 
 class Camera;
 class Mesh;
 class Sprite;
 class Actor;
+class Controller;
 class World;
 class SceneGraphManager;
 class PhysicsGraphManager;
@@ -118,6 +119,7 @@ struct Unit
 	void				add_mesh(StringId32 name, MeshId mesh);
 	void				add_sprite(StringId32 name, SpriteId sprite);
 	void				add_actor(StringId32 name, ActorId actor);
+	void				set_controller(StringId32 name, ControllerId controller);
 
 	Camera*				camera(const char* name);
 	Camera*				camera(uint32_t i);
@@ -129,7 +131,9 @@ struct Unit
 	Sprite*				sprite(uint32_t i);
 
 	Actor*				actor(const char* name);
-	Actor*				actor(uint32_t i);	
+	Actor*				actor(uint32_t i);
+
+	Controller*			controller();
 
 private:
 
@@ -155,6 +159,8 @@ public:
 
 	uint32_t			m_num_actors;
 	Component 			m_actors[MAX_ACTOR_COMPONENTS];
+
+	Component			m_controller;
 };
 
 } // namespace crown

+ 53 - 0
engine/lua/LuaController.cpp

@@ -0,0 +1,53 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "Controller.h"
+#include "Vector3.h"
+#include "LuaStack.h"
+#include "LuaEnvironment.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+CE_EXPORT int controller_move(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Controller* controller = stack.get_controller(1);
+	const Vector3& pos = stack.get_vector3(2);
+
+	controller->move(pos);
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+void load_controller(LuaEnvironment& env)
+{
+	env.load_module_function("Controller", "move",    controller_move);
+}
+
+} // namespace crown

+ 1 - 0
engine/lua/LuaEnvironment.cpp

@@ -79,6 +79,7 @@ CE_EXPORT int luaopen_libcrown(lua_State* /*L*/)
 	load_mesh(*env);
 	load_sprite(*env);
 	load_actor(*env);
+	load_controller(*env);
 	load_physics_world(*env);
 
 	return 1;

+ 1 - 0
engine/lua/LuaEnvironment.h

@@ -112,6 +112,7 @@ void load_world(LuaEnvironment& env);
 void load_mesh(LuaEnvironment& env);
 void load_sprite(LuaEnvironment& env);
 void load_actor(LuaEnvironment& env);
+void load_controller(LuaEnvironment& env);
 void load_physics_world(LuaEnvironment& env);
 
 CE_EXPORT int32_t luaopen_libcrown(lua_State* L);

+ 22 - 3
engine/lua/LuaStack.h

@@ -44,6 +44,7 @@ class Mesh;
 class Sprite;
 class PhysicsWorld;
 class Actor;
+class Controller;
 class ResourcePackage;
 typedef Id SoundInstanceId;
 
@@ -73,6 +74,12 @@ public:
 		return lua_gettop(m_state);
 	}
 
+	//-----------------------------------------------------------------------------
+	void push_nil()
+	{
+		lua_pushnil(m_state);
+	}
+
 	//-----------------------------------------------------------------------------
 	void push_bool(bool value)
 	{
@@ -271,16 +278,28 @@ public:
 	Actor* get_actor(int32_t index)
 	{
 		return (Actor*) lua_touserdata(m_state, index);
-	}	
+	}
 
-	//-------------------------------------------------------------------------
+	//-----------------------------------------------------------------------------
+	void push_controller(Controller* controller)
+	{
+		lua_pushlightuserdata(m_state, controller);
+	}
+
+	//-----------------------------------------------------------------------------
+	Controller* get_controller(int32_t index)
+	{
+		return (Controller*) lua_touserdata(m_state, index);
+	}
+
+	//-----------------------------------------------------------------------------
 	void push_sound_instance_id(const SoundInstanceId id)
 	{
 		uintptr_t enc = id.encode();
 		lua_pushlightuserdata(m_state, (void*)enc);
 	}
 
-	//-------------------------------------------------------------------------
+	//-----------------------------------------------------------------------------
 	SoundInstanceId get_sound_instance_id(int32_t index)
 	{
 		uint32_t enc = (uintptr_t) lua_touserdata(m_state, index);

+ 13 - 0
engine/lua/LuaUnit.cpp

@@ -250,6 +250,18 @@ CE_EXPORT int unit_actor(lua_State* L)
 	return 1;
 }
 
+//-----------------------------------------------------------------------------
+CE_EXPORT int unit_controller(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Unit* unit = stack.get_unit(1);
+
+	Controller* controller = unit->controller();
+	controller ? stack.push_controller(controller) : stack.push_nil();
+	return 1;
+}
+
 //-----------------------------------------------------------------------------
 void load_unit(LuaEnvironment& env)
 {
@@ -273,6 +285,7 @@ void load_unit(LuaEnvironment& env)
 	env.load_module_function("Unit", "mesh",					unit_mesh);	
 	env.load_module_function("Unit", "sprite",					unit_sprite);
 	env.load_module_function("Unit", "actor",					unit_actor);
+	env.load_module_function("Unit", "controller",				unit_controller);
 }
 
 } // namespace crown

+ 73 - 0
engine/physics/Controller.cpp

@@ -0,0 +1,73 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "Controller.h"
+#include "MathUtils.h"
+#include "PhysicsResource.h"
+#include "Device.h"
+#include "Vector3.h"
+
+#include "PxCapsuleController.h"
+using physx::PxCapsuleController;
+using physx::PxCapsuleControllerDesc;
+using physx::PxVec3;
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+Controller::Controller(const PhysicsResource* pr, PxScene* scene, PxControllerManager* manager)
+	: m_resource(pr)
+	, m_scene(scene)
+	, m_manager(manager)
+	, m_controller(NULL)
+{
+	const PhysicsController contr = pr->controller();
+
+	PxCapsuleControllerDesc desc;
+	desc.radius = math::cos(contr.radius);
+	desc.height = contr.height;
+	desc.slopeLimit = contr.slope_limit;
+	desc.stepOffset = contr.step_offset;
+	desc.contactOffset = contr.contact_offset;
+	desc.upDirection = PxVec3(0.0, 1.0, 0.0);
+
+	m_controller = manager->createController(*device()->physx(), scene, desc);
+}
+
+//-----------------------------------------------------------------------------
+Controller::~Controller()
+{
+	// FIXME
+}
+
+//-----------------------------------------------------------------------------
+void Controller::move(const Vector3& pos)
+{
+	// FIXME
+}
+
+} // namespace crown

+ 56 - 0
engine/physics/Controller.h

@@ -0,0 +1,56 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "PxController.h"
+#include "PxControllerManager.h"
+
+using physx::PxController;
+using physx::PxControllerManager;
+using physx::PxScene;
+
+namespace crown
+{
+
+struct PhysicsResource;
+class Vector3;
+
+struct Controller
+{
+							Controller(const PhysicsResource* pr, PxScene* scene, PxControllerManager* manager);
+							~Controller();
+
+	void					move(const Vector3& pos);
+
+private:
+
+	const PhysicsResource*	m_resource;
+
+	PxScene*				m_scene;
+	PxControllerManager*	m_manager;
+	PxController*			m_controller;
+};
+
+} // namespace crown