mikymod пре 12 година
родитељ
комит
678e8d81be

+ 132 - 0
engine/physics/Actor.cpp

@@ -0,0 +1,132 @@
+/*
+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 "Actor.h"
+#include "Vector3.h"
+#include "Quaternion.h"
+#include "Matrix4x4.h"
+#include "Unit.h"
+#include "PhysicsGraph.h"
+#include "Device.h"
+#include "Physics.h"
+
+#include "PxPhysicsAPI.h"
+
+namespace crown
+{
+	
+//-----------------------------------------------------------------------------
+Actor::Actor(PhysicsGraph& pg, int32_t node, ActorType::Enum type)
+	: m_physics_graph(pg)
+	, m_node(node)
+	, m_type(type)
+{
+	Vector3 pos = m_physics_graph.m_local_poses[node].translation();
+	physx::PxVec3 position(pos.x, pos.y, pos.z);
+
+	switch (type)
+	{
+		case ActorType::STATIC:
+		{
+			m_actor = device()->physx()->createRigidStatic(physx::PxTransform(position));
+			break;
+		}
+		case ActorType::DYNAMIC:
+		{
+			m_actor = device()->physx()->createRigidDynamic(physx::PxTransform(position));
+			break;
+		}
+		default:
+		{
+			CE_FATAL("Unable to recognize actor type");
+		}
+	}
+}
+
+//-----------------------------------------------------------------------------
+Vector3	Actor::local_position() const
+{
+	return m_physics_graph.local_position(m_node);
+}
+
+//-----------------------------------------------------------------------------
+Quaternion Actor::local_rotation() const
+{
+	return m_physics_graph.local_rotation(m_node);
+}
+
+//-----------------------------------------------------------------------------
+Matrix4x4 Actor::local_pose() const
+{
+	return m_physics_graph.local_pose(m_node);
+}
+
+//-----------------------------------------------------------------------------
+Vector3	Actor::world_position() const
+{
+	return m_physics_graph.world_position(m_node);
+}
+
+//-----------------------------------------------------------------------------
+Quaternion Actor::world_rotation() const
+{
+	return m_physics_graph.world_rotation(m_node);
+}
+
+//-----------------------------------------------------------------------------
+Matrix4x4 Actor::world_pose() const
+{
+	return m_physics_graph.world_pose(m_node);
+}
+
+//-----------------------------------------------------------------------------
+void Actor::set_local_position(Unit* unit, const Vector3& pos)
+{
+	unit->set_local_position(m_node, pos);
+}
+
+//-----------------------------------------------------------------------------
+void Actor::set_local_rotation(Unit* unit, const Quaternion& rot)
+{
+	unit->set_local_rotation(m_node, rot);
+}
+
+//-----------------------------------------------------------------------------
+void Actor::set_local_pose(Unit* unit, const Matrix4x4& pose)
+{
+	unit->set_local_pose(m_node, pose);
+}
+
+//-----------------------------------------------------------------------------
+void Actor::create_sphere(Vector3& position, float radius)
+{
+	// FIXME FIXME FIXME
+	physx::PxMaterial* mat = device()->physx()->createMaterial(0.5f, 0.5f, 0.1f);
+	m_actor->createShape(physx::PxSphereGeometry(radius), *mat);
+	// physx::PxRigidBodyExt::updateMassAndInertia(*m_actor, 1.0f);
+}
+
+} // namespace crown

+ 72 - 0
engine/physics/Actor.h

@@ -0,0 +1,72 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "PhysicsTypes.h"
+
+#include "PxPhysics.h"
+#include "PxScene.h"
+#include "PxRigidActor.h"
+
+namespace crown
+{
+
+class Vector3;
+class Quaternion;
+class Matrix4x4;
+class Unit;
+class PhysicsGraph;
+
+struct Actor
+{
+					Actor(PhysicsGraph& pg, int32_t node, ActorType::Enum type);
+
+	Vector3			local_position() const;
+	Quaternion		local_rotation() const;
+	Matrix4x4		local_pose() const;
+
+	Vector3			world_position() const;
+	Quaternion		world_rotation() const;
+	Matrix4x4		world_pose() const;
+
+	void			set_local_position(Unit* unit, const Vector3& pos);
+	void			set_local_rotation(Unit* unit, const Quaternion& rot);
+	void			set_local_pose(Unit* unit, const Matrix4x4& pose);
+
+	void			create_sphere(Vector3& position, float radius);
+
+public:
+
+	physx::PxRigidActor* m_actor;
+
+	PhysicsGraph& m_physics_graph;
+	int32_t m_node;
+	ActorType::Enum m_type;
+};
+
+} // namespace crown

+ 76 - 0
engine/physics/Physics.h

@@ -0,0 +1,76 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "PxFoundation.h"
+#include "PxPhysics.h"
+#include "PxCooking.h"
+#include "PxDefaultAllocator.h"
+#include "PxDefaultErrorCallback.h"
+
+namespace crown
+{
+
+static physx::PxDefaultErrorCallback 	g_physx_error_callback;
+static physx::PxDefaultAllocator 		g_physx_allocator_callback;
+
+struct Physics
+{
+	Physics();
+	~Physics();
+
+public:
+
+	physx::PxFoundation* m_foundation;
+	physx::PxPhysics* m_physics;
+	physx::PxCooking* m_cooking;
+};
+
+//-----------------------------------------------------------------------------
+inline Physics::Physics()
+	: m_foundation(NULL)
+	, m_physics(NULL)
+	, m_cooking(NULL)
+{
+	m_foundation = PxCreateFoundation(PX_PHYSICS_VERSION, g_physx_allocator_callback, g_physx_error_callback);
+	CE_ASSERT_NOT_NULL(m_foundation);
+
+	m_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *m_foundation, physx::PxTolerancesScale());
+	CE_ASSERT_NOT_NULL(m_physics);
+
+	m_cooking = PxCreateCooking(PX_PHYSICS_VERSION, *m_foundation, physx::PxCookingParams());
+	CE_ASSERT_NOT_NULL(m_cooking);
+}
+
+//-----------------------------------------------------------------------------
+inline Physics::~Physics()
+{
+	m_physics->release();
+	m_foundation->release();
+}
+
+} // namespace crown

+ 135 - 0
engine/physics/PhysicsGraph.cpp

@@ -0,0 +1,135 @@
+/*
+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 "PhysicsGraph.h"
+#include "Vector3.h"
+#include "Quaternion.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+PhysicsGraph::PhysicsGraph(int32_t index)
+	: m_index(index)
+	, m_local_poses(default_allocator()) 
+	, m_world_poses(default_allocator())
+	, m_sg_nodes(default_allocator())
+{
+}
+
+//-----------------------------------------------------------------------------
+int32_t	PhysicsGraph::create_node(int32_t node, const Vector3& pos, const Quaternion& rot)
+{
+	Matrix4x4 pose(rot, pos);
+	return create_node(node, pose);	
+}
+
+//-----------------------------------------------------------------------------
+int32_t PhysicsGraph::create_node(int32_t node, const Matrix4x4& pose)
+{
+	CE_ASSERT(node >= -1, "node must be >= -1");
+
+	m_world_poses.push_back(pose);
+	m_sg_nodes.push_back(node);
+
+	return m_world_poses.size() - 1;
+}
+
+//-----------------------------------------------------------------------------
+Vector3	PhysicsGraph::local_position(int32_t node) const
+{
+	return m_local_poses[node].translation();
+}
+
+//-----------------------------------------------------------------------------
+Quaternion PhysicsGraph::local_rotation(int32_t node) const
+{
+	return m_local_poses[node].to_quaternion();
+
+}
+
+//-----------------------------------------------------------------------------
+Matrix4x4 PhysicsGraph::local_pose(int32_t node) const
+{
+	return m_local_poses[node];
+}
+
+
+//-----------------------------------------------------------------------------
+void PhysicsGraph::set_local_position(int32_t node, const Vector3& pos)
+{
+	Matrix4x4& local_pose = m_local_poses[node];
+	local_pose.set_translation(pos);
+}
+
+//-----------------------------------------------------------------------------
+void PhysicsGraph::set_local_rotation(int32_t node, const Quaternion& rot)
+{
+	Matrix4x4& local_pose = m_local_poses[node];
+
+	Vector3 local_translation = local_pose.translation();
+	local_pose = rot.to_matrix4x4();
+	local_pose.set_translation(local_translation);
+}
+
+//-----------------------------------------------------------------------------
+void PhysicsGraph::set_local_pose(int32_t node, const Matrix4x4& pose)
+{
+	m_local_poses[node] = pose;
+}
+
+//-----------------------------------------------------------------------------
+Vector3	PhysicsGraph::world_position(int32_t node) const
+{
+	return m_world_poses[node].translation();
+}
+
+//-----------------------------------------------------------------------------
+Quaternion PhysicsGraph::world_rotation(int32_t node) const
+{
+	return m_world_poses[node].to_quaternion();
+}
+
+//-----------------------------------------------------------------------------
+Matrix4x4 PhysicsGraph::world_pose(int32_t node) const
+{
+	return m_world_poses[node];
+}
+
+//-----------------------------------------------------------------------------
+void PhysicsGraph::clear()
+{
+	m_world_poses.clear();
+	m_sg_nodes.clear();
+}
+
+//-----------------------------------------------------------------------------
+void PhysicsGraph::update()
+{
+	// TODO
+}
+
+} // namespace crown

+ 68 - 0
engine/physics/PhysicsGraph.h

@@ -0,0 +1,68 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "Matrix4x4.h"
+#include "List.h"
+
+namespace crown
+{
+
+struct PhysicsGraph
+{
+					PhysicsGraph(int32_t index);
+
+	int32_t			create_node(int32_t node, const Vector3& pos, const Quaternion& rot);
+	int32_t			create_node(int32_t node, const Matrix4x4& pose);
+
+	Vector3			local_position(int32_t node) const;
+	Quaternion		local_rotation(int32_t node) const;
+	Matrix4x4		local_pose(int32_t node) const;
+
+	void			set_local_position(int32_t node, const Vector3& pos);
+	void			set_local_rotation(int32_t node, const Quaternion& rot);
+	void			set_local_pose(int32_t node, const Matrix4x4& pose);
+
+	Vector3			world_position(int32_t node) const;
+	Quaternion		world_rotation(int32_t node) const;
+	Matrix4x4		world_pose(int32_t node) const;
+
+	void			clear();
+
+	void			update();
+
+public:
+
+	uint32_t		m_index;
+
+	List<Matrix4x4> m_local_poses;
+	List<Matrix4x4>	m_world_poses;
+	List<uint32_t> 	m_sg_nodes;
+};
+
+} // namespace crown

+ 74 - 0
engine/physics/PhysicsGraphManager.cpp

@@ -0,0 +1,74 @@
+/*
+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 "PhysicsGraphManager.h"
+#include "PhysicsGraph.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+PhysicsGraphManager::PhysicsGraphManager()
+	: m_graphs(default_allocator())
+{
+
+}
+
+//-----------------------------------------------------------------------------
+PhysicsGraphManager::~PhysicsGraphManager()
+{
+}
+
+//-----------------------------------------------------------------------------
+PhysicsGraph* PhysicsGraphManager::create_physics_graph()
+{
+	uint32_t index = m_graphs.size();
+	PhysicsGraph* pg = CE_NEW(default_allocator(), PhysicsGraph)(index);
+	m_graphs.push_back(pg);
+	return pg;
+}
+
+//-----------------------------------------------------------------------------
+void PhysicsGraphManager::destroy_physics_graph(PhysicsGraph* pg)
+{
+	CE_ASSERT_NOT_NULL(pg);
+
+	m_graphs[pg->m_index] = m_graphs[m_graphs.size() - 1];
+	m_graphs.pop_back();
+
+	CE_DELETE(default_allocator(), pg);
+}
+
+//-----------------------------------------------------------------------------
+void PhysicsGraphManager::update()
+{
+	for (uint32_t i = 0; i < m_graphs.size(); i++)
+	{
+		m_graphs[i]->update();
+	}
+}
+
+};

+ 58 - 0
engine/physics/PhysicsGraphManager.h

@@ -0,0 +1,58 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "List.h"
+
+namespace crown
+{
+
+class PhysicsGraph;
+
+/// Manages a collection of physics graphs.
+class PhysicsGraphManager
+{
+public:
+
+						PhysicsGraphManager();
+						~PhysicsGraphManager();
+
+	/// Creates a new physics graph
+	PhysicsGraph*		create_physics_graph();
+
+	/// Destroys the @a sg physics graph
+	void				destroy_physics_graph(PhysicsGraph* pg);
+
+	/// Updates all the physics graphs
+	void				update();
+
+private:
+
+	List<PhysicsGraph*>	m_graphs;
+};
+
+} // namespace crown

+ 48 - 0
engine/physics/PhysicsTypes.h

@@ -0,0 +1,48 @@
+/*
+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.
+*/
+
+#pragma once
+
+//-----------------------------------------------------------------------------
+struct ActorType
+{
+	enum Enum
+	{
+		STATIC,
+		DYNAMIC
+	};
+};
+
+//-----------------------------------------------------------------------------
+struct ShapeType
+{
+	enum Enum
+	{
+		SPHERE,
+		BOX,
+		PLANE
+	};
+};

+ 92 - 0
engine/physics/PhysicsWorld.cpp

@@ -0,0 +1,92 @@
+/*
+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 "PhysicsWorld.h"
+#include "Vector3.h"
+#include "Actor.h"
+#include "Device.h"
+#include "Physics.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+PhysicsWorld::PhysicsWorld()
+	: m_scene(NULL)
+	, m_actor_pool(default_allocator(), MAX_ACTORS, sizeof(Actor), CE_ALIGNOF(Actor))
+{
+	physx::PxSceneDesc scene_desc(device()->physx()->getTolerancesScale());
+	scene_desc.gravity = physx::PxVec3(0.0f, -9.81f, 0.0f);
+	m_scene = device()->physx()->createScene(scene_desc);
+}
+
+//-----------------------------------------------------------------------------
+PhysicsWorld::~PhysicsWorld()
+{
+	m_scene->release();
+}
+
+//-----------------------------------------------------------------------------
+ActorId	PhysicsWorld::create_actor(PhysicsGraph& pg, int32_t node, ActorType::Enum type)
+{
+	Actor* actor = CE_NEW(m_actor_pool, Actor)(pg, node, type);
+
+	m_scene->addActor(*actor->m_actor);
+
+	return m_actor.create(actor);
+}
+
+//-----------------------------------------------------------------------------
+void PhysicsWorld::destroy_actor(ActorId id)
+{
+	CE_ASSERT(m_actor.has(id), "Actor does not exist");
+
+	Actor* actor = m_actor.lookup(id);
+	CE_DELETE(m_actor_pool, actor);
+
+	m_scene->removeActor(*actor->m_actor);
+
+	m_actor.destroy(id);
+}
+
+//-----------------------------------------------------------------------------
+Actor* PhysicsWorld::lookup_actor(ActorId id)
+{
+	CE_ASSERT(m_actor.has(id), "Actor does not exist");
+
+	return m_actor.lookup(id);
+}
+
+
+//-----------------------------------------------------------------------------
+void PhysicsWorld::update(float dt)
+{
+	m_scene->simulate(dt);
+
+	while (!m_scene->fetchResults());
+}
+
+} // namespace crown

+ 67 - 0
engine/physics/PhysicsWorld.h

@@ -0,0 +1,67 @@
+/*
+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.
+*/
+
+#pragma once
+
+#include "IdArray.h"
+#include "PoolAllocator.h"
+#include "PhysicsTypes.h"
+
+#include "PxScene.h"
+
+#define MAX_ACTORS 1024
+
+namespace crown
+{
+
+typedef Id ActorId;
+
+class Vector3;
+class Actor;
+class PhysicsGraph;
+
+//-----------------------------------------------------------------------------
+class PhysicsWorld
+{
+public:
+
+				PhysicsWorld();
+				~PhysicsWorld();
+
+	ActorId		create_actor(PhysicsGraph& pg, int32_t node, ActorType::Enum type);
+	void		destroy_actor(ActorId id);
+	Actor*		lookup_actor(ActorId id);
+
+	void		update(float dt);
+
+public:
+
+	physx::PxScene* m_scene;
+
+	PoolAllocator m_actor_pool;
+	IdArray<MAX_ACTORS, Actor*> m_actor;
+};
+}